Skip to content

Commit f1a8734

Browse files
Backport: feat (provider/gateway): add models provider option for fallbacks (#9758)
This is an automated backport of #9734 to the release-v5.0 branch. Co-authored-by: Walter Korman <[email protected]>
1 parent 8546087 commit f1a8734

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.changeset/four-papayas-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/gateway': patch
3+
---
4+
5+
feat (provider/gateway): add models provider option for model routing

content/providers/01-ai-sdk-providers/00-ai-gateway.mdx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ The following gateway provider options are available:
348348

349349
Example: `only: ['anthropic', 'vertex']` will only allow routing to Anthropic or Vertex AI.
350350

351+
- **models** _string[]_
352+
353+
Specifies fallback models to use when the primary model fails or is unavailable. The gateway will try the primary model first (specified in the `model` parameter), then try each model in this array in order until one succeeds.
354+
355+
Example: `models: ['openai/gpt-5-nano', 'gemini-2.0-flash']` will try the fallback models in order if the primary model fails.
356+
351357
- **user** _string_
352358

353359
Optional identifier for the end user on whose behalf the request is being made. This is used for spend tracking and attribution purposes, allowing you to track usage per end-user in your application.
@@ -378,6 +384,31 @@ const { text } = await generateText({
378384
});
379385
```
380386

387+
#### Model Fallbacks Example
388+
389+
The `models` option enables automatic fallback to alternative models when the primary model fails:
390+
391+
```ts
392+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
393+
import { generateText } from 'ai';
394+
395+
const { text } = await generateText({
396+
model: 'openai/gpt-4o', // Primary model
397+
prompt: 'Write a TypeScript haiku',
398+
providerOptions: {
399+
gateway: {
400+
models: ['openai/gpt-5-nano', 'gemini-2.0-flash'], // Fallback models
401+
} satisfies GatewayProviderOptions,
402+
},
403+
});
404+
405+
// This will:
406+
// 1. Try openai/gpt-4o first
407+
// 2. If it fails, try openai/gpt-5-nano
408+
// 3. If that fails, try gemini-2.0-flash
409+
// 4. Return the result from the first model that succeeds
410+
```
411+
381412
### Provider-Specific Options
382413

383414
When using provider-specific options through AI Gateway, use the actual provider name (e.g. `anthropic`, `openai`, not `gateway`) as the key:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { GatewayProviderOptions } from '@ai-sdk/gateway';
2+
import { streamText } from 'ai';
3+
import 'dotenv/config';
4+
5+
async function main() {
6+
const result = streamText({
7+
headers: {
8+
'X-Simulate-Model-Failures': 'anthropic/claude-4-sonnet',
9+
},
10+
model: 'anthropic/claude-4-sonnet',
11+
prompt: 'Tell me a short tale of the krakens of the deep.',
12+
providerOptions: {
13+
gateway: {
14+
models: ['openai/gpt-5-nano', 'zai/glm-4.6'],
15+
} satisfies GatewayProviderOptions,
16+
},
17+
});
18+
19+
for await (const textPart of result.textStream) {
20+
process.stdout.write(textPart);
21+
}
22+
23+
console.log();
24+
console.log(
25+
'Provider metadata:',
26+
JSON.stringify(await result.providerMetadata, null, 2),
27+
);
28+
console.log('Token usage:', await result.usage);
29+
console.log('Finish reason:', await result.finishReason);
30+
}
31+
32+
main().catch(console.error);

packages/gateway/src/gateway-provider-options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ const gatewayProviderOptions = lazyValidator(() =>
3535
* Example: `['chat', 'v2']`
3636
*/
3737
tags: z.array(z.string()).optional(),
38+
/**
39+
* Array of model slugs specifying fallback models to use in order.
40+
*
41+
* Example: `['openai/gpt-5-nano', 'zai/glm-4.6']` will try `openai/gpt-5-nano` first, then `zai/glm-4.6` as fallback.
42+
*/
43+
models: z.array(z.string()).optional(),
3844
}),
3945
),
4046
);

0 commit comments

Comments
 (0)