When using the xAI Responses API with streaming, a ZodError is thrown because the xaiResponsesChunkSchema is missing several event types that the API actually returns.
ZodError: Invalid input: expected "response.code_interpreter_call.interpreting"
The full error shows multiple missing event types in the union schema validation.
The following event types are returned by the xAI API but not defined in the schema:
response.code_interpreter_call.interpretingresponse.code_interpreter_call_code.deltaresponse.code_interpreter_call_code.doneresponse.custom_tool_call_input.deltaresponse.custom_tool_call_input.doneThere's also a duplicate text output issue. The message type is processed in both response.output_item.added and response.output_item.done events, causing the same text to be rendered twice.
xaiResponsesChunkSchema:// code_interpreter interpreting state
z.object({
type: z.literal("response.code_interpreter_call.interpreting"),
item_id: z.string(),
output_index: z.number()
}),
// code_interpreter code streaming delta
z.object({
type: z.literal("response.code_interpreter_call_code.delta"),
item_id: z.string(),
output_index: z.number(),
delta: z.string()
}),
// code_interpreter code done
z.object({
type: z.literal("response.code_interpreter_call_code.done"),
item_id: z.string(),
output_index: z.number(),
code: z.string()
}),
// custom_tool_call input delta
z.object({
type: z.literal("response.custom_tool_call_input.delta"),
item_id: z.string(),
output_index: z.number(),
delta: z.string()
}),
// custom_tool_call input done
z.object({
type: z.literal("response.custom_tool_call_input.done"),
item_id: z.string(),
output_index: z.number(),
input: z.string()
}),
Fix duplicate output by only processing message type on added event:
// Before
if (part.type === "message") {
// After
if (part.type === "message" && event.type === "response.output_item.added") {
### AI SDK Version
@ai-sdk/xai: 3.0.0-beta.53
ai: 6.0.0-beta.x
### Code of Conduct
- [x] I agree to follow this project's Code of Conduct