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/smooth-ravens-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/xai': patch
---

Fix Responses API validation errors for server-side tools (web_search, x_search, code_execution). Add missing custom_tool_call type and streaming event schemas.
83 changes: 76 additions & 7 deletions packages/xai/src/responses/xai-responses-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ export type XaiResponsesToolCall = {
| 'function_call'
| 'web_search_call'
| 'x_search_call'
| 'code_interpreter_call';
| 'code_interpreter_call'
| 'custom_tool_call';
id: string;
call_id: string;
name: string;
arguments: string;
call_id?: string;
name?: string;
arguments?: string;
input?: string;
status: string;
action?: any;
};

export type XaiResponsesTool =
Expand Down Expand Up @@ -110,11 +113,13 @@ const reasoningSummaryPartSchema = z.object({
});

const toolCallSchema = z.object({
name: z.string(),
arguments: z.string(),
call_id: z.string(),
name: z.string().optional(),
arguments: z.string().optional(),
input: z.string().optional(),
call_id: z.string().optional(),
id: z.string(),
status: z.string(),
action: z.any().optional(),
});

const outputItemSchema = z.discriminatedUnion('type', [
Expand Down Expand Up @@ -142,6 +147,10 @@ const outputItemSchema = z.discriminatedUnion('type', [
type: z.literal('view_x_video_call'),
...toolCallSchema.shape,
}),
z.object({
type: z.literal('custom_tool_call'),
...toolCallSchema.shape,
}),
z.object({
type: z.literal('message'),
role: z.string(),
Expand Down Expand Up @@ -278,6 +287,66 @@ export const xaiResponsesChunkSchema = z.union([
summary_index: z.number(),
text: z.string(),
}),
z.object({
type: z.literal('response.web_search_call.in_progress'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.web_search_call.searching'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.web_search_call.completed'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.x_search_call.in_progress'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.x_search_call.searching'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.x_search_call.completed'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_execution_call.in_progress'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_execution_call.executing'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_execution_call.completed'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_interpreter_call.in_progress'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_interpreter_call.executing'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.code_interpreter_call.completed'),
item_id: z.string(),
output_index: z.number(),
}),
z.object({
type: z.literal('response.done'),
response: xaiResponsesResponseSchema,
Expand Down
36 changes: 25 additions & 11 deletions packages/xai/src/responses/xai-responses-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,29 @@ export class XaiResponsesLanguageModel implements LanguageModelV2 {
part.type === 'code_interpreter_call' ||
part.type === 'code_execution_call' ||
part.type === 'view_image_call' ||
part.type === 'view_x_video_call'
part.type === 'view_x_video_call' ||
part.type === 'custom_tool_call'
) {
let toolName = part.name;
if (webSearchSubTools.includes(part.name)) {
let toolName = part.name ?? '';
if (webSearchSubTools.includes(part.name ?? '')) {
toolName = webSearchToolName ?? 'web_search';
} else if (xSearchSubTools.includes(part.name)) {
} else if (xSearchSubTools.includes(part.name ?? '')) {
toolName = xSearchToolName ?? 'x_search';
} else if (part.name === 'code_execution') {
toolName = codeExecutionToolName ?? 'code_execution';
}

// custom_tool_call uses 'input' field, others use 'arguments'
const toolInput =
part.type === 'custom_tool_call'
? (part.input ?? '')
: (part.arguments ?? '');

content.push({
type: 'tool-call',
toolCallId: part.id,
toolName,
input: part.arguments,
input: toolInput,
providerExecuted: true,
});

Expand Down Expand Up @@ -476,7 +483,8 @@ export class XaiResponsesLanguageModel implements LanguageModelV2 {
part.type === 'code_interpreter_call' ||
part.type === 'code_execution_call' ||
part.type === 'view_image_call' ||
part.type === 'view_x_video_call'
part.type === 'view_x_video_call' ||
part.type === 'custom_tool_call'
) {
if (!seenToolCalls.has(part.id)) {
seenToolCalls.add(part.id);
Expand All @@ -493,15 +501,21 @@ export class XaiResponsesLanguageModel implements LanguageModelV2 {
'x_thread_fetch',
];

let toolName = part.name;
if (webSearchSubTools.includes(part.name)) {
let toolName = part.name ?? '';
if (webSearchSubTools.includes(part.name ?? '')) {
toolName = webSearchToolName ?? 'web_search';
} else if (xSearchSubTools.includes(part.name)) {
} else if (xSearchSubTools.includes(part.name ?? '')) {
toolName = xSearchToolName ?? 'x_search';
} else if (part.name === 'code_execution') {
toolName = codeExecutionToolName ?? 'code_execution';
}

// custom_tool_call uses 'input' field, others use 'arguments'
const toolInput =
part.type === 'custom_tool_call'
? (part.input ?? '')
: (part.arguments ?? '');

controller.enqueue({
type: 'tool-input-start',
id: part.id,
Expand All @@ -511,7 +525,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV2 {
controller.enqueue({
type: 'tool-input-delta',
id: part.id,
delta: part.arguments,
delta: toolInput,
});

controller.enqueue({
Expand All @@ -523,7 +537,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV2 {
type: 'tool-call',
toolCallId: part.id,
toolName,
input: part.arguments,
input: toolInput,
providerExecuted: true,
});
}
Expand Down
Loading