Skip to content
Draft
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-core/src/generate-text/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';

Check failure on line 3 in examples/ai-core/src/generate-text/responses-reasoning.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Cannot find module '../../lib/run' or its corresponding type declarations.

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 @@ -172,6 +172,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
125 changes: 125 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 @@ -326,6 +326,131 @@
]
`);
});
<<<<<<< HEAD

Check failure on line 329 in packages/xai/src/responses/xai-responses-language-model.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
=======

Check failure on line 330 in packages/xai/src/responses/xai-responses-language-model.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
it('should extract reasoning with encrypted content but empty summary text', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-4-fast-non-reasoning',
output: [
{
type: 'reasoning',
id: 'rs_789',
status: 'completed',
summary: [],
encrypted_content: 'encrypted_zdr_content_xyz',
},
{
type: 'message',
id: 'msg_123',
status: 'completed',
role: 'assistant',
content: [
{
type: 'output_text',
text: 'Here is my response.',
annotations: [],
},
],
},
],
usage: {
input_tokens: 10,
output_tokens: 20,
output_tokens_details: {
reasoning_tokens: 15,
},
},
});

const result = await createModel().doGenerate({
prompt: TEST_PROMPT,
});

expect(result.content).toMatchInlineSnapshot(`
[
{
"providerMetadata": {
"xai": {
"itemId": "rs_789",
"reasoningEncryptedContent": "encrypted_zdr_content_xyz",
},
},
"text": "",
"type": "reasoning",
},
{
"text": "Here is my response.",
"type": "text",
},
]
`);
});

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",
},
]
`);
});
>>>>>>> 19eece65b8 ([v6.0] fix(provider/xai): extract reasoning text from content in responses doGenerate (#16798))

Check failure on line 453 in packages/xai/src/responses/xai-responses-language-model.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
});

describe('settings and options', () => {
Expand Down
21 changes: 18 additions & 3 deletions packages/xai/src/responses/xai-responses-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,31 @@
}

case 'reasoning': {
const summaryTexts = part.summary
.map(s => s.text)
.filter(text => text && text.length > 0);
const texts =
part.summary.length > 0
? part.summary.map(s => s.text)
: (part.content ?? []).map(c => c.text);

<<<<<<< HEAD

Check failure on line 307 in packages/xai/src/responses/xai-responses-language-model.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
if (summaryTexts.length > 0) {
const reasoningText = summaryTexts.join('');
if (part.encrypted_content || part.id) {
content.push({
type: 'reasoning',
text: reasoningText,
=======

Check failure on line 314 in packages/xai/src/responses/xai-responses-language-model.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
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) {
const hasMetadata = part.encrypted_content || part.id;
content.push({
type: 'reasoning',
text: reasoningText,
...(hasMetadata && {
>>>>>>> 19eece65b8 ([v6.0] fix(provider/xai): extract reasoning text from content in responses doGenerate (#16798))

Check failure on line 326 in packages/xai/src/responses/xai-responses-language-model.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Merge conflict marker encountered.
providerMetadata: {
xai: {
...(part.encrypted_content && {
Expand Down
Loading