Skip to content
Closed
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
9 changes: 9 additions & 0 deletions .changeset/fix-xai-toolname-mapping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@ai-sdk/xai": patch
---

fix: map server-side tool types to correct toolName when part.name is empty

When xAI returns web_search_call, x_search_call, code_execution_call, or other server-side tool events, the part.name field may be empty or undefined. Previously, this caused AI_NoSuchToolError because the toolName was set to an empty string.

This fix checks part.type in addition to part.name to correctly map the tool call back to the user-provided tool name from the tools object.
213 changes: 213 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 @@ -615,6 +615,160 @@ describe('XaiResponsesLanguageModel', () => {
expect(result.content[1].type).toBe('tool-call');
});
});

describe('empty part.name mapping', () => {
it('should map web_search_call to correct toolName when part.name is empty', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-4-fast',
output: [
{
type: 'web_search_call',
id: 'ws_123',
name: undefined,
arguments: '{"query":"test"}',
call_id: '',
status: 'completed',
},
],
usage: { input_tokens: 10, output_tokens: 5 },
});

const result = await createModel().doGenerate({
prompt: TEST_PROMPT,
tools: [
{
type: 'provider',
id: 'xai.web_search',
name: 'my_web_search',
args: {},
},
],
});

expect(result.content).toHaveLength(1);
expect(result.content[0]).toMatchObject({
type: 'tool-call',
toolCallId: 'ws_123',
toolName: 'my_web_search',
providerExecuted: true,
});
});

it('should map x_search_call to correct toolName when part.name is empty', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-4-fast',
output: [
{
type: 'x_search_call',
id: 'xs_123',
name: undefined,
arguments: '{"query":"test"}',
call_id: '',
status: 'completed',
},
],
usage: { input_tokens: 10, output_tokens: 5 },
});

const result = await createModel().doGenerate({
prompt: TEST_PROMPT,
tools: [
{
type: 'provider',
id: 'xai.x_search',
name: 'my_x_search',
args: {},
},
],
});

expect(result.content).toHaveLength(1);
expect(result.content[0]).toMatchObject({
type: 'tool-call',
toolCallId: 'xs_123',
toolName: 'my_x_search',
providerExecuted: true,
});
});

it('should map code_execution_call to correct toolName when part.name is empty', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-4-fast',
output: [
{
type: 'code_execution_call',
id: 'ce_123',
name: undefined,
arguments: '{}',
call_id: '',
status: 'completed',
},
],
usage: { input_tokens: 10, output_tokens: 5 },
});

const result = await createModel().doGenerate({
prompt: TEST_PROMPT,
tools: [
{
type: 'provider',
id: 'xai.code_execution',
name: 'my_code_exec',
args: {},
},
],
});

expect(result.content).toHaveLength(1);
expect(result.content[0]).toMatchObject({
type: 'tool-call',
toolCallId: 'ce_123',
toolName: 'my_code_exec',
providerExecuted: true,
});
});

it('should map view_image_call to view_image when part.name is empty', async () => {
prepareJsonResponse({
id: 'resp_123',
object: 'response',
status: 'completed',
model: 'grok-4-fast',
output: [
{
type: 'view_image_call',
id: 'vi_123',
name: undefined,
arguments: '{}',
call_id: '',
status: 'completed',
},
],
usage: { input_tokens: 10, output_tokens: 5 },
});

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

expect(result.content).toHaveLength(1);
expect(result.content[0]).toMatchObject({
type: 'tool-call',
toolCallId: 'vi_123',
toolName: 'view_image',
providerExecuted: true,
});
});
});
});

describe('doStream', () => {
Expand Down Expand Up @@ -737,6 +891,65 @@ describe('XaiResponsesLanguageModel', () => {
providerExecuted: true,
});
});

it('should map web_search_call to correct toolName when part.name is empty in stream', async () => {
prepareStreamChunks([
JSON.stringify({
type: 'response.created',
response: {
id: 'resp_123',
object: 'response',
model: 'grok-4-fast',
status: 'in_progress',
output: [],
},
}),
JSON.stringify({
type: 'response.output_item.added',
item: {
type: 'web_search_call',
id: 'ws_456',
name: undefined,
arguments: '{"query":"streaming test"}',
call_id: '',
status: 'completed',
},
output_index: 0,
}),
JSON.stringify({
type: 'response.done',
response: {
id: 'resp_123',
object: 'response',
status: 'completed',
output: [],
usage: { input_tokens: 10, output_tokens: 5 },
},
}),
]);

const { stream } = await createModel().doStream({
prompt: TEST_PROMPT,
tools: [
{
type: 'provider',
id: 'xai.web_search',
name: 'custom_search',
args: {},
},
],
});

const parts = await convertReadableStreamToArray(stream);

expect(parts).toContainEqual({
type: 'tool-call',
toolCallId: 'ws_456',
toolName: 'custom_search',
input: '{"query":"streaming test"}',
providerExecuted: true,
});
});
});

describe('citation streaming', () => {
Expand Down
44 changes: 38 additions & 6 deletions packages/xai/src/responses/xai-responses-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,28 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
part.type === 'custom_tool_call'
) {
let toolName = part.name ?? '';
if (webSearchSubTools.includes(part.name ?? '')) {
// Map tool name based on part.type when part.name is empty/undefined
// xAI server-side tools may not include name in the response
if (
part.type === 'web_search_call' ||
webSearchSubTools.includes(part.name ?? '')
) {
toolName = webSearchToolName ?? 'web_search';
} else if (xSearchSubTools.includes(part.name ?? '')) {
} else if (
part.type === 'x_search_call' ||
xSearchSubTools.includes(part.name ?? '')
) {
toolName = xSearchToolName ?? 'x_search';
} else if (part.name === 'code_execution') {
} else if (
part.type === 'code_execution_call' ||
part.type === 'code_interpreter_call' ||
part.name === 'code_execution'
) {
toolName = codeExecutionToolName ?? 'code_execution';
} else if (part.type === 'view_image_call') {
toolName = 'view_image';
} else if (part.type === 'view_x_video_call') {
toolName = 'view_x_video';
}

// custom_tool_call uses 'input' field, others use 'arguments'
Expand Down Expand Up @@ -498,12 +514,28 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
];

let toolName = part.name ?? '';
if (webSearchSubTools.includes(part.name ?? '')) {
// Map tool name based on part.type when part.name is empty/undefined
// xAI server-side tools may not include name in the response
if (
part.type === 'web_search_call' ||
webSearchSubTools.includes(part.name ?? '')
) {
toolName = webSearchToolName ?? 'web_search';
} else if (xSearchSubTools.includes(part.name ?? '')) {
} else if (
part.type === 'x_search_call' ||
xSearchSubTools.includes(part.name ?? '')
) {
toolName = xSearchToolName ?? 'x_search';
} else if (part.name === 'code_execution') {
} else if (
part.type === 'code_execution_call' ||
part.type === 'code_interpreter_call' ||
part.name === 'code_execution'
) {
toolName = codeExecutionToolName ?? 'code_execution';
} else if (part.type === 'view_image_call') {
toolName = 'view_image';
} else if (part.type === 'view_x_video_call') {
toolName = 'view_x_video';
}

// custom_tool_call uses 'input' field, others use 'arguments'
Expand Down