Skip to content

Commit 502310f

Browse files
fix(xai): pass through reasoning effort (anomalyco#42160)
Co-authored-by: Aiden Cline <[email protected]>
1 parent 37fe5c8 commit 502310f

2 files changed

Lines changed: 168 additions & 7 deletions

File tree

‎packages/core/test/provider-xai-responses.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,56 @@ test("xAI Responses sends promptCacheKey as prompt_cache_key", async () => {
3030

3131
expect(body?.prompt_cache_key).toBe("session-123")
3232
})
33+
34+
test("xAI Responses passes through xhigh reasoning effort", async () => {
35+
let body: Record<string, unknown> | undefined
36+
const mockFetch = Object.assign(
37+
async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => {
38+
body = JSON.parse(String(init?.body))
39+
return Response.json({
40+
id: "response-1",
41+
created_at: 0,
42+
model: "grok-4",
43+
object: "response",
44+
output: [],
45+
usage: { input_tokens: 1, output_tokens: 0 },
46+
status: "completed",
47+
})
48+
},
49+
{ preconnect: fetch.preconnect },
50+
)
51+
const model = createXai({ apiKey: "test", fetch: mockFetch }).responses("grok-4")
52+
53+
await model.doGenerate({
54+
prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }],
55+
providerOptions: { xai: { reasoningEffort: "xhigh" } },
56+
})
57+
58+
expect(body?.reasoning).toEqual({ effort: "xhigh" })
59+
})
60+
61+
test("xAI Chat passes through xhigh reasoning effort", async () => {
62+
let body: Record<string, unknown> | undefined
63+
const mockFetch = Object.assign(
64+
async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => {
65+
body = JSON.parse(String(init?.body))
66+
return Response.json({
67+
id: "chat-1",
68+
created: 0,
69+
model: "grok-4",
70+
object: "chat.completion",
71+
choices: [{ index: 0, message: { role: "assistant", content: "Hello" }, finish_reason: "stop" }],
72+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
73+
})
74+
},
75+
{ preconnect: fetch.preconnect },
76+
)
77+
const model = createXai({ apiKey: "test", fetch: mockFetch }).chat("grok-4")
78+
79+
await model.doGenerate({
80+
prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }],
81+
providerOptions: { xai: { reasoningEffort: "xhigh" } },
82+
})
83+
84+
expect(body?.reasoning_effort).toBe("xhigh")
85+
})

‎patches/@ai-sdk%[email protected]‎

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
diff --git a/dist/index.d.mts b/dist/index.d.mts
2-
index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..990ef4195bc67b6d25f249e1c81cf51710390f9a 100644
2+
index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..6ac6e2873b7681ac632c903694aa38c7b09773fa 100644
33
--- a/dist/index.d.mts
44
+++ b/dist/index.d.mts
5-
@@ -78,6 +78,7 @@ declare const xaiLanguageModelResponsesOptions: z.ZodObject<{
5+
@@ -5,12 +5,7 @@ import { FetchFunction } from '@ai-sdk/provider-utils';
6+
7+
type XaiChatModelId = 'grok-4.3' | 'grok-4.20-0309-reasoning' | 'grok-4.20-0309-non-reasoning' | 'grok-4.20-multi-agent-0309' | 'grok-build-0.1' | (string & {});
8+
declare const xaiLanguageModelChatOptions: z.ZodObject<{
9+
- reasoningEffort: z.ZodOptional<z.ZodEnum<{
10+
- none: "none";
11+
- low: "low";
12+
- medium: "medium";
13+
- high: "high";
14+
- }>>;
15+
+ reasoningEffort: z.ZodOptional<z.ZodString>;
16+
logprobs: z.ZodOptional<z.ZodBoolean>;
17+
topLogprobs: z.ZodOptional<z.ZodNumber>;
18+
parallel_function_calling: z.ZodOptional<z.ZodBoolean>;
19+
@@ -68,16 +63,12 @@ type XaiResponsesModelId = 'grok-4.3' | 'grok-4.20-0309-reasoning' | 'grok-4.20-
20+
* @see https://docs.x.ai/docs/api-reference#create-new-response
21+
*/
22+
declare const xaiLanguageModelResponsesOptions: z.ZodObject<{
23+
- reasoningEffort: z.ZodOptional<z.ZodEnum<{
24+
- none: "none";
25+
- low: "low";
26+
- medium: "medium";
27+
- high: "high";
28+
- }>>;
29+
+ reasoningEffort: z.ZodOptional<z.ZodString>;
30+
logprobs: z.ZodOptional<z.ZodBoolean>;
631
topLogprobs: z.ZodOptional<z.ZodNumber>;
732
store: z.ZodOptional<z.ZodBoolean>;
833
previousResponseId: z.ZodOptional<z.ZodString>;
@@ -11,10 +36,35 @@ index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..990ef4195bc67b6d25f249e1c81cf517
1136
"file_search_call.results": "file_search_call.results";
1237
}>>>>;
1338
diff --git a/dist/index.d.ts b/dist/index.d.ts
14-
index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..990ef4195bc67b6d25f249e1c81cf51710390f9a 100644
39+
index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..6ac6e2873b7681ac632c903694aa38c7b09773fa 100644
1540
--- a/dist/index.d.ts
1641
+++ b/dist/index.d.ts
17-
@@ -78,6 +78,7 @@ declare const xaiLanguageModelResponsesOptions: z.ZodObject<{
42+
@@ -5,12 +5,7 @@ import { FetchFunction } from '@ai-sdk/provider-utils';
43+
44+
type XaiChatModelId = 'grok-4.3' | 'grok-4.20-0309-reasoning' | 'grok-4.20-0309-non-reasoning' | 'grok-4.20-multi-agent-0309' | 'grok-build-0.1' | (string & {});
45+
declare const xaiLanguageModelChatOptions: z.ZodObject<{
46+
- reasoningEffort: z.ZodOptional<z.ZodEnum<{
47+
- none: "none";
48+
- low: "low";
49+
- medium: "medium";
50+
- high: "high";
51+
- }>>;
52+
+ reasoningEffort: z.ZodOptional<z.ZodString>;
53+
logprobs: z.ZodOptional<z.ZodBoolean>;
54+
topLogprobs: z.ZodOptional<z.ZodNumber>;
55+
parallel_function_calling: z.ZodOptional<z.ZodBoolean>;
56+
@@ -68,16 +63,12 @@ type XaiResponsesModelId = 'grok-4.3' | 'grok-4.20-0309-reasoning' | 'grok-4.20-
57+
* @see https://docs.x.ai/docs/api-reference#create-new-response
58+
*/
59+
declare const xaiLanguageModelResponsesOptions: z.ZodObject<{
60+
- reasoningEffort: z.ZodOptional<z.ZodEnum<{
61+
- none: "none";
62+
- low: "low";
63+
- medium: "medium";
64+
- high: "high";
65+
- }>>;
66+
+ reasoningEffort: z.ZodOptional<z.ZodString>;
67+
logprobs: z.ZodOptional<z.ZodBoolean>;
1868
topLogprobs: z.ZodOptional<z.ZodNumber>;
1969
store: z.ZodOptional<z.ZodBoolean>;
2070
previousResponseId: z.ZodOptional<z.ZodString>;
@@ -23,9 +73,18 @@ index 266c5ffdd9ee74ff95908ce90858ee4369d4e4ae..990ef4195bc67b6d25f249e1c81cf517
2373
"file_search_call.results": "file_search_call.results";
2474
}>>>>;
2575
diff --git a/dist/index.js b/dist/index.js
26-
index 717b74538f5c8f0d6ab1475ebb2a84a47ccd3950..dd7dbeb3bc307e0d355f4bb4939d06cc8eae7528 100644
76+
index 717b74538f5c8f0d6ab1475ebb2a84a47ccd3950..0fd8f0d1cae951cd24401034a9c1dba762d9fd84 100644
2777
--- a/dist/index.js
2878
+++ b/dist/index.js
79+
@@ -246,7 +246,7 @@ var searchSourceSchema = import_v4.z.discriminatedUnion("type", [
80+
rssSourceSchema
81+
]);
82+
var xaiLanguageModelChatOptions = import_v4.z.object({
83+
- reasoningEffort: import_v4.z.enum(["none", "low", "medium", "high"]).optional(),
84+
+ reasoningEffort: import_v4.z.string().optional(),
85+
logprobs: import_v4.z.boolean().optional(),
86+
topLogprobs: import_v4.z.number().int().min(0).max(8).optional(),
87+
/**
2988
@@ -1119,6 +1119,14 @@ async function convertToXaiResponsesInput({
3089
type: "input_file",
3190
file_url: block.data.toString()
@@ -41,6 +100,15 @@ index 717b74538f5c8f0d6ab1475ebb2a84a47ccd3950..dd7dbeb3bc307e0d355f4bb4939d06cc
41100
} else {
42101
throw new import_provider4.UnsupportedFunctionalityError({
43102
functionality: `file part media type ${block.mediaType} as inline data (xAI Responses requires a URL or a Files API reference for non-image files)`
103+
@@ -1746,7 +1754,7 @@ var xaiLanguageModelResponsesOptions = import_v47.z.object({
104+
* tokens), `medium` and `high` (uses more reasoning tokens). Not all models
105+
* support reasoning effort; see xAI's docs for the values each model accepts.
106+
*/
107+
- reasoningEffort: import_v47.z.enum(["none", "low", "medium", "high"]).optional(),
108+
+ reasoningEffort: import_v47.z.string().optional(),
109+
logprobs: import_v47.z.boolean().optional(),
110+
topLogprobs: import_v47.z.number().int().min(0).max(8).optional(),
111+
/**
44112
@@ -1760,6 +1768,10 @@ var xaiLanguageModelResponsesOptions = import_v47.z.object({
45113
* The ID of the previous response from the model.
46114
*/
@@ -63,9 +131,18 @@ index 717b74538f5c8f0d6ab1475ebb2a84a47ccd3950..dd7dbeb3bc307e0d355f4bb4939d06cc
63131
};
64132
if (xaiTools2 && xaiTools2.length > 0) {
65133
diff --git a/dist/index.mjs b/dist/index.mjs
66-
index a26af109585fc2bd3053b320142aa869c06d36f4..774adaf971b648544317a4fc65d0c56e488d4fc7 100644
134+
index a26af109585fc2bd3053b320142aa869c06d36f4..5faca56477b4e55a87f6f57850731c7d3e1721a5 100644
67135
--- a/dist/index.mjs
68136
+++ b/dist/index.mjs
137+
@@ -230,7 +230,7 @@ var searchSourceSchema = z.discriminatedUnion("type", [
138+
rssSourceSchema
139+
]);
140+
var xaiLanguageModelChatOptions = z.object({
141+
- reasoningEffort: z.enum(["none", "low", "medium", "high"]).optional(),
142+
+ reasoningEffort: z.string().optional(),
143+
logprobs: z.boolean().optional(),
144+
topLogprobs: z.number().int().min(0).max(8).optional(),
145+
/**
69146
@@ -1122,6 +1122,14 @@ async function convertToXaiResponsesInput({
70147
type: "input_file",
71148
file_url: block.data.toString()
@@ -81,6 +158,15 @@ index a26af109585fc2bd3053b320142aa869c06d36f4..774adaf971b648544317a4fc65d0c56e
81158
} else {
82159
throw new UnsupportedFunctionalityError3({
83160
functionality: `file part media type ${block.mediaType} as inline data (xAI Responses requires a URL or a Files API reference for non-image files)`
161+
@@ -1749,7 +1757,7 @@ var xaiLanguageModelResponsesOptions = z7.object({
162+
* tokens), `medium` and `high` (uses more reasoning tokens). Not all models
163+
* support reasoning effort; see xAI's docs for the values each model accepts.
164+
*/
165+
- reasoningEffort: z7.enum(["none", "low", "medium", "high"]).optional(),
166+
+ reasoningEffort: z7.string().optional(),
167+
logprobs: z7.boolean().optional(),
168+
topLogprobs: z7.number().int().min(0).max(8).optional(),
169+
/**
84170
@@ -1763,6 +1771,10 @@ var xaiLanguageModelResponsesOptions = z7.object({
85171
* The ID of the previous response from the model.
86172
*/
@@ -158,9 +244,18 @@ index f90df62eb9a30154388b1390e9f3acc3ccc022bf..00e61cba6cf048ae0045be692f33cb7e
158244

159245
if (xaiTools && xaiTools.length > 0) {
160246
diff --git a/src/responses/xai-responses-options.ts b/src/responses/xai-responses-options.ts
161-
index f8e96c061bf8793a402ababb8cad65bb2ad6aead..15c168892c1e8755453c61d3061e958cfd51ac71 100644
247+
index f8e96c061bf8793a402ababb8cad65bb2ad6aead..2a39a36221ab23ea0000bff1d7854c5bce3f9d74 100644
162248
--- a/src/responses/xai-responses-options.ts
163249
+++ b/src/responses/xai-responses-options.ts
250+
@@ -18,7 +18,7 @@ export const xaiLanguageModelResponsesOptions = z.object({
251+
* tokens), `medium` and `high` (uses more reasoning tokens). Not all models
252+
* support reasoning effort; see xAI's docs for the values each model accepts.
253+
*/
254+
- reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).optional(),
255+
+ reasoningEffort: z.string().optional(),
256+
logprobs: z.boolean().optional(),
257+
topLogprobs: z.number().int().min(0).max(8).optional(),
258+
/**
164259
@@ -32,6 +32,10 @@ export const xaiLanguageModelResponsesOptions = z.object({
165260
* The ID of the previous response from the model.
166261
*/
@@ -172,3 +267,16 @@ index f8e96c061bf8793a402ababb8cad65bb2ad6aead..15c168892c1e8755453c61d3061e958c
172267
/**
173268
* Specify additional output data to include in the model response.
174269
* Example values: 'file_search_call.results'.
270+
diff --git a/src/xai-chat-options.ts b/src/xai-chat-options.ts
271+
index d70a72a9fa01da2c711c291da5ce949efbde60b5..fd6b1ae025388b614f08b620244be553199479ca 100644
272+
--- a/src/xai-chat-options.ts
273+
+++ b/src/xai-chat-options.ts
274+
@@ -51,7 +51,7 @@ const searchSourceSchema = z.discriminatedUnion('type', [
275+
276+
// xai-specific provider options
277+
export const xaiLanguageModelChatOptions = z.object({
278+
- reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).optional(),
279+
+ reasoningEffort: z.string().optional(),
280+
logprobs: z.boolean().optional(),
281+
topLogprobs: z.number().int().min(0).max(8).optional(),
282+

0 commit comments

Comments
 (0)