Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-schools-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/xai': patch
---

fix reasoning text extraction from content in responses doGenerate
26 changes: 26 additions & 0 deletions examples/ai-functions/src/generate-text/xai/responses-reasoning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
import { run } from '../../lib/run';

run(async () => {
const result = await generateText({
model: xai.responses('grok-3-mini-latest'),
prompt: 'How many "r"s are in the word "strawberry"?',
});

for (const part of result.content) {
switch (part.type) {
case 'reasoning': {
console.log('--- reasoning ---');
console.log(part.text);
break;
}
case 'text': {
console.log('--- text ---');
console.log(part.text);
break;
}
}
console.log();
}
});
3 changes: 3 additions & 0 deletions packages/xai/src/responses/xai-responses-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ const outputItemSchema = z.discriminatedUnion('type', [
type: z.literal('reasoning'),
id: z.string(),
summary: z.array(reasoningSummaryPartSchema),
content: z
.array(z.object({ type: z.string(), text: z.string() }))
.nullish(),
status: z.string(),
encrypted_content: z.string().nullish(),
}),
Expand Down
62 changes: 62 additions & 0 deletions packages/xai/src/responses/xai-responses-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,68 @@ describe('XaiResponsesLanguageModel', () => {
]
`);
});

it('should extract reasoning from content when summary is empty', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-3-mini',
output: [
{
type: 'reasoning',
id: 'rs_456',
status: 'completed',
summary: [],
content: [
{
type: 'reasoning_text',
text: 'Let me think step by step.',
},
],
},
{
type: 'message',
id: 'msg_123',
status: 'completed',
role: 'assistant',
content: [
{
type: 'output_text',
text: 'The answer is 444.',
annotations: [],
},
],
},
],
usage: {
input_tokens: 10,
output_tokens: 15,
},
});

const result = await createModel('grok-3-mini').doGenerate({
prompt: TEST_PROMPT,
});

expect(result.content).toMatchInlineSnapshot(`
[
{
"providerMetadata": {
"xai": {
"itemId": "rs_456",
},
},
"text": "Let me think step by step.",
"type": "reasoning",
},
{
"text": "The answer is 444.",
"type": "text",
},
]
`);
});
});

describe('settings and options', () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/xai/src/responses/xai-responses-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,14 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
}

case 'reasoning': {
const summaryTexts = part.summary
.map(s => s.text)
.filter(text => text && text.length > 0);

const reasoningText = summaryTexts.join('');
const texts =
part.summary.length > 0
? part.summary.map(s => s.text)
: (part.content ?? []).map(c => c.text);

const reasoningText = texts
.filter(text => text && text.length > 0)
.join('');

// condition changed here since encrypted content can now come with empty reasoning text
if (reasoningText || part.encrypted_content) {
Expand Down
Loading