Skip to content

Comments

feat: add web search support#144

Merged
louisgv merged 8 commits intomainfrom
devin/1754544265-web-search
Aug 7, 2025
Merged

feat: add web search support#144
louisgv merged 8 commits intomainfrom
devin/1754544265-web-search

Conversation

@louisgv
Copy link
Member

@louisgv louisgv commented Aug 7, 2025

feat: add web search support

Summary

This PR adds comprehensive web search functionality to the OpenRouter AI SDK provider, enabling developers to use OpenRouter's web search capabilities through the AI SDK. The implementation includes:

  • Web search configuration: Added plugins array and web_search_options to OpenRouterChatSettings for configuring web search behavior
  • Request parameter handling: Updated the getArgs method to pass web search settings to the OpenRouter API
  • Citation processing: Implemented web search citation handling using the AI SDK's LanguageModelV2Source content type for proper source attribution
  • Schema updates: Added url_citation schema structure based on Louis's feedback with required end_index, start_index, title, url fields and optional content

The changes maintain backward compatibility by making all new fields optional and follow the established patterns in the codebase for request/response handling.

Review & Testing Checklist for Human

  • Verify schema structure matches OpenRouter API: The url_citation schema structure should be validated against actual OpenRouter web search API responses to ensure field names and types are correct
  • Test LanguageModelV2Source citation rendering: Confirm that web search citations using the AI SDK's source content type display correctly in applications and don't break existing citation workflows
  • Validate web search parameter passing: Test that both plugins and web_search_options parameters are correctly transmitted to the OpenRouter API and trigger web search functionality
  • End-to-end web search testing: Run a complete test with a model that supports web search to verify the entire flow from request configuration to citation processing works correctly

Recommended test plan: Use a web search capable model (like openai/gpt-4o with web search enabled) to make a query that would benefit from web search, then verify that citations are returned and properly formatted as LanguageModelV2Source objects.


Diagram

%%{ init : { "theme" : "default" }}%%
flowchart TD
    Settings["src/types/<br/>openrouter-chat-settings.ts"]:::major-edit
    ChatIndex["src/chat/index.ts"]:::major-edit  
    Schemas["src/chat/schemas.ts"]:::major-edit
    SDK["@ai-sdk/provider<br/>LanguageModelV2Source"]:::context
    
    Settings --> |"web search types"| ChatIndex
    ChatIndex --> |"getArgs method"| OpenRouterAPI["OpenRouter API"]:::context
    OpenRouterAPI --> |"web search response"| Schemas
    Schemas --> |"url_citation schema"| ChatIndex
    ChatIndex --> |"LanguageModelV2Source"| SDK
    
    subgraph Legend
        L1["Major Edit"]:::major-edit
        L2["Minor Edit"]:::minor-edit  
        L3["Context/No Edit"]:::context
    end

classDef major-edit fill:#90EE90
classDef minor-edit fill:#87CEEB  
classDef context fill:#FFFFFF
Loading

Notes

  • This PR addresses Louis's feedback on the annotation schema structure, changing from an array-based approach to a direct url_citation object
  • The implementation uses the AI SDK's proper content types (LanguageModelV2Source) instead of appending citation text, providing better integration with downstream applications
  • All tests pass locally, but end-to-end web search functionality requires testing with actual API calls
  • Session requested by Louis (@louisgv) - Devin run: https://app.devin.ai/sessions/1eae218929d5447b867ff4fd78634672

- Add plugins and web_search_options for web search configuration
- Add web search annotations with corrected url_citation schema
- Support web search citation processing in responses
- Use proper AI SDK LanguageModelV2Source content types for citations

Co-Authored-By: Louis   <louis@openrouter.ai>
@devin-ai-integration
Copy link
Contributor

Original prompt from Louis
@Devin look at the docs for <https://openrouter.ai/docs/features/web-search> and add support for it to the openrouter ai-sdk-provider

@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

- Test plugins array configuration with web search plugin
- Test web_search_options for models with built-in web search
- Validate LanguageModelV2Source content in streaming responses
- Ensure proper citation handling with url, title, and sourceType

Co-Authored-By: Louis   <louis@openrouter.ai>
],
});

const streamParts: any[] = [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use any!

devin-ai-integration bot and others added 2 commits August 7, 2025 05:59
Co-Authored-By: Louis   <louis@openrouter.ai>
…rch e2e test

Co-Authored-By: Louis   <louis@openrouter.ai>
@devin-ai-integration
Copy link
Contributor

I've fixed the types in the e2e test file by replacing them with proper type definitions:

// Before
const streamParts: any[] = [];
const sources: any[] = [];

// After
const streamParts: Array<{ type: string; sourceType?: string; id?: string; url?: string; title?: string; text?: string; }> = [];
const sources: Array<{ type: string; sourceType?: string; id?: string; url?: string; title?: string; }> = [];

All tests continue to pass with the proper type definitions.

@devin-ai-integration
Copy link
Contributor

I've fixed the any[] types in the e2e test file by replacing them with proper type definitions:

All tests continue to pass with the proper type definitions.

@devin-ai-integration
Copy link
Contributor

I've fixed the any[] types in the e2e test file by replacing them with proper type definitions:

// Before
const streamParts: any[] = [];
const sources: any[] = [];

// After
const streamParts: Array<{ type: string; sourceType?: string; id?: string; url?: string; title?: string; text?: string; }> = [];
const sources: Array<{ type: string; sourceType?: string; id?: string; url?: string; title?: string; }> = [];

All tests continue to pass with the proper type definitions.

@louisgv louisgv requested a review from Copilot August 7, 2025 06:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive web search functionality to the OpenRouter AI SDK provider, enabling developers to use OpenRouter's web search capabilities through the AI SDK's standardized interfaces.

  • Adds web search configuration options (plugins and web_search_options) to OpenRouterChatSettings
  • Implements proper citation handling using AI SDK's LanguageModelV2Source content type for web search results
  • Updates schemas to support url_citation annotations with required fields for proper source attribution

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/types/openrouter-chat-settings.ts Adds web search configuration types and reformats quantizations array
src/chat/schemas.ts Adds url_citation annotation schema and provider field support
src/chat/index.ts Implements web search parameter passing and citation processing logic
package.json Bumps version to 1.1.0 for new feature release
e2e/web-search/index.test.ts Adds end-to-end test for web search functionality
e2e/usage-accounting.test.ts Updates test to include provider field validation

warnings: [],
providerMetadata: {
openrouter: {
provider: response.provider ?? '',
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provider field is set to an empty string when response.provider is undefined, but the schema defines provider as optional. This could cause issues downstream. Consider keeping it undefined when not present: provider: response.provider,

Suggested change
provider: response.provider ?? '',
provider: response.provider,

Copilot uses AI. Check for mistakes.
usage: openrouterUsage,
};

// Only include provider if it's actually set
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The streaming response handles optional provider field correctly by conditionally including it, but the non-streaming response at line 354 always sets it to an empty string. This inconsistency could lead to confusion and different behavior between streaming and non-streaming responses.

Copilot uses AI. Check for mistakes.
@louisgv louisgv merged commit 099bd59 into main Aug 7, 2025
3 checks passed
@louisgv louisgv deleted the devin/1754544265-web-search branch August 7, 2025 07:07
kesavan-byte pushed a commit to osm-API/ai-sdk-provider that referenced this pull request Feb 13, 2026
* feat: add web search support

- Add plugins and web_search_options for web search configuration
- Add web search annotations with corrected url_citation schema
- Support web search citation processing in responses
- Use proper AI SDK LanguageModelV2Source content types for citations

Co-Authored-By: Louis   <louis@openrouter.ai>

* test: add e2e tests for web search functionality

- Test plugins array configuration with web search plugin
- Test web_search_options for models with built-in web search
- Validate LanguageModelV2Source content in streaming responses
- Ensure proper citation handling with url, title, and sourceType

Co-Authored-By: Louis   <louis@openrouter.ai>

* refactor: replace any[] with proper type in web search e2e test

Co-Authored-By: Louis   <louis@openrouter.ai>

* refactor: replace any[] with proper type for sources array in web search e2e test

Co-Authored-By: Louis   <louis@openrouter.ai>

* update tests and schema

* bump version

* fix

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant