Repository navigation
fix: merge chat and completion provider routing instead of replacing it #605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@openrouter/ai-sdk-provider': patch | ||
| --- | ||
|
|
||
| Chat and completion models now shallow merge `provider` routing preferences instead of replacing them, matching the image model. Precedence, lowest to highest, is model `provider` settings (chat only), provider `extraBody.provider`, model `extraBody.provider`, then `providerOptions.openrouter.provider`. Non-object values such as `null` are ignored. Behavior change: a runtime `providerOptions.openrouter.provider` no longer drops configured keys it does not set. To remove a configured key, set it explicitly in the runtime object. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Regression test for GitHub issue #587 | ||
| * https://github.com/OpenRouterTeam/ai-sdk-provider/issues/587 | ||
| * | ||
| * Issue: "Chat model replaces configured provider routing instead of merging | ||
| * (consistency with image model, #551)" | ||
| * | ||
| * This test verifies that a call-level `providerOptions.openrouter.provider` | ||
| * is shallow merged into the model's configured provider routing, so keys it | ||
| * does not set (`allow_fallbacks`, `sort`) are still sent. | ||
| */ | ||
| import { generateText } from 'ai'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createOpenRouter } from '@/src'; | ||
|
|
||
| describe('Issue #587: chat provider routing is merged, not replaced', () => { | ||
| it('keeps configured routing keys when the call overrides order', async () => { | ||
| const bodies: Array<Record<string, unknown>> = []; | ||
| const openrouter = createOpenRouter({ | ||
| apiKey: 'test-key', | ||
| fetch: async (_input, init) => { | ||
| bodies.push(JSON.parse(String(init?.body))); | ||
| return new Response( | ||
| JSON.stringify({ | ||
| id: 'chatcmpl-587', | ||
| model: 'google/gemini-2.5-flash', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { role: 'assistant', content: 'Hello' }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| }), | ||
| { status: 200, headers: { 'content-type': 'application/json' } }, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| await generateText({ | ||
| model: openrouter.chat('google/gemini-2.5-flash', { | ||
| provider: { order: ['google'], allow_fallbacks: false, sort: 'price' }, | ||
| }), | ||
| prompt: 'Hello', | ||
| providerOptions: { openrouter: { provider: { order: ['anthropic'] } } }, | ||
| }); | ||
|
|
||
| expect(bodies[0]?.provider).toStrictEqual({ | ||
| order: ['anthropic'], | ||
| allow_fallbacks: false, | ||
| sort: 'price', | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { mergeProviderRouting } from './merge-provider-routing'; | ||
|
|
||
| describe('mergeProviderRouting', () => { | ||
| it('shallow merges sources, later sources winning', () => { | ||
| expect( | ||
| mergeProviderRouting( | ||
| { order: ['google'], sort: 'price' }, | ||
| { sort: 'latency' }, | ||
| { order: ['anthropic'] }, | ||
| ), | ||
| ).toStrictEqual({ order: ['anthropic'], sort: 'latency' }); | ||
| }); | ||
|
|
||
| it('ignores null, arrays and primitives', () => { | ||
| expect( | ||
| mergeProviderRouting({ sort: 'price' }, null, ['x'], 'y', undefined), | ||
| ).toStrictEqual({ sort: 'price' }); | ||
| }); | ||
|
|
||
| it('returns undefined when no source is an object', () => { | ||
| expect(mergeProviderRouting(undefined, null, [])).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * Shallow merges `provider` routing preferences instead of replacing them. | ||
| * Pass the sources from lowest to highest precedence (later wins). Values | ||
| * that are not plain objects (null, arrays, primitives) are ignored. Returns | ||
| * undefined when no source is an object. | ||
| */ | ||
| export function mergeProviderRouting( | ||
| ...sources: unknown[] | ||
| ): Record<string, unknown> | undefined { | ||
| const objects = sources.filter( | ||
| (source): source is Record<string, unknown> => | ||
| typeof source === 'object' && source !== null && !Array.isArray(source), | ||
| ); | ||
| return objects.length > 0 ? Object.assign({}, ...objects) : undefined; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.