Skip to content

Commit 23571c9

Browse files
authored
feat(providers/xai): add reasoningEffort option (#5926)
1 parent b217142 commit 23571c9

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

.changeset/hungry-frogs-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/openai-compatible': patch
3+
---
4+
5+
feat(providers/xai): add reasoningEffort provider option

content/providers/01-ai-sdk-providers/01-xai.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ The following optional settings are available for xAI chat models:
111111
A unique identifier representing your end-user, which can help xAI to
112112
monitor and detect abuse.
113113

114+
xAI chat models also support some model specific provider options. You can pass them in `providerOptions` argument:
115+
116+
```ts
117+
const model = xai('grok-3');
118+
119+
await generateText({
120+
model,
121+
providerOptions: {
122+
xai: {
123+
reasoningEffort: 'high',
124+
},
125+
},
126+
});
127+
```
128+
129+
The following optional provider options are available for xAI chat models:
130+
131+
- **reasoningEffort** _'low' | 'medium' | 'high'_
132+
133+
Reasoning effort for reasoning models. Defaults to `medium`. If you use
134+
`providerOptions` to set the `reasoningEffort` option, this
135+
model setting will be ignored.
136+
114137
## Model Capabilities
115138

116139
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |

packages/openai-compatible/src/openai-compatible-chat-language-model.test.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ describe('doGenerate', () => {
646646
expect(warnings).toEqual([]);
647647
});
648648

649-
it('should respect the includeUsage option', async () => {
649+
it('should respect the reasoningEffort provider option', async () => {
650650
prepareJsonResponse({ content: '{"value":"test"}' });
651651

652652
const model = new OpenAICompatibleChatLanguageModel(
@@ -656,22 +656,23 @@ describe('doGenerate', () => {
656656
provider: 'test-provider',
657657
url: () => 'https://my.api.com/v1/chat/completions',
658658
headers: () => ({}),
659-
includeUsage: true,
660659
},
661660
);
662661

663-
const { warnings } = await model.doStream({
662+
await model.doGenerate({
664663
inputFormat: 'prompt',
665664
mode: { type: 'regular' },
666665
prompt: TEST_PROMPT,
666+
providerMetadata: {
667+
'openai-compatible': {
668+
reasoningEffort: 'low',
669+
},
670+
},
667671
});
668672

669673
const body = await server.calls[0].requestBody;
670674

671-
expect(body.stream).toBe(true);
672-
expect(body.stream_options).toStrictEqual({ include_usage: true });
673-
674-
expect(warnings).toEqual([]);
675+
expect(body.reasoning_effort).toBe('low');
675676
});
676677

677678
it('should use json_schema & strict in object-json mode when structuredOutputs are enabled', async () => {
@@ -1012,6 +1013,37 @@ describe('doStream', () => {
10121013
};
10131014
}
10141015

1016+
it('should respect the includeUsage option', async () => {
1017+
prepareStreamResponse({
1018+
content: ['Hello', ', ', 'World!'],
1019+
finish_reason: 'stop',
1020+
});
1021+
1022+
const model = new OpenAICompatibleChatLanguageModel(
1023+
'gpt-4o-2024-08-06',
1024+
{},
1025+
{
1026+
provider: 'test-provider',
1027+
url: () => 'https://my.api.com/v1/chat/completions',
1028+
headers: () => ({}),
1029+
includeUsage: true,
1030+
},
1031+
);
1032+
1033+
const { warnings } = await model.doStream({
1034+
inputFormat: 'prompt',
1035+
mode: { type: 'regular' },
1036+
prompt: TEST_PROMPT,
1037+
});
1038+
1039+
const body = await server.calls[0].requestBody;
1040+
1041+
expect(body.stream).toBe(true);
1042+
expect(body.stream_options).toStrictEqual({ include_usage: true });
1043+
1044+
expect(warnings).toEqual([]);
1045+
});
1046+
10151047
it('should stream text deltas', async () => {
10161048
prepareStreamResponse({
10171049
content: ['Hello', ', ', 'World!'],

packages/openai-compatible/src/openai-compatible-chat-language-model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV1 {
171171
seed,
172172
...providerMetadata?.[this.providerOptionsName],
173173

174+
reasoning_effort:
175+
providerMetadata?.[this.providerOptionsName]?.reasoningEffort ??
176+
providerMetadata?.['openai-compatible']?.reasoningEffort,
177+
174178
// messages:
175179
messages: convertToOpenAICompatibleChatMessages(prompt),
176180
};

0 commit comments

Comments
 (0)