-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: xai server-side tool calling #9896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preliminary review. Please add an example to examples/next-openai/app and test the xAI Responses API with multi-steps. We have been using that for all providers, we should probably rename the folder 😅
| inputWarnings.push({ | ||
| type: 'other', | ||
| message: | ||
| 'xAI Responses API does not support this content type in user messages', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would include block.type in the message, it will make it more useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you didn't add block.type to message:?
| num_server_side_tools_used: z.number().optional(), | ||
| }); | ||
|
|
||
| export const xaiResponsesResponseSchema = z.object({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use lazySchema, see #9339. You can do it in a follow up PR though and do all of xAI then.
| status: z.string(), | ||
| }); | ||
|
|
||
| export const xaiResponsesChunkSchema = z.union([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use lazySchema, see above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional Comments:
packages/provider/src/language-model/v3/language-model-v3-tool-result.ts (modified) (line 22):
The type definition changed result from unknown to NonNullable<unknown>, which excludes undefined. However, the XAI responses implementation sets result: undefined for server-side tool calls, causing a type incompatibility.
View Details
📝 Patch Details
diff --git a/packages/xai/src/responses/xai-responses-language-model.ts b/packages/xai/src/responses/xai-responses-language-model.ts
index 954968896..d3100793e 100644
--- a/packages/xai/src/responses/xai-responses-language-model.ts
+++ b/packages/xai/src/responses/xai-responses-language-model.ts
@@ -212,7 +212,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
type: 'tool-result',
toolCallId: part.id,
toolName,
- result: undefined,
+ result: {},
providerExecuted: true,
});
@@ -500,7 +500,7 @@ export class XaiResponsesLanguageModel implements LanguageModelV3 {
type: 'tool-result',
toolCallId: part.id,
toolName,
- result: undefined,
+ result: {},
providerExecuted: true,
});
}
Analysis
Type incompatibility between LanguageModelV3ToolResult and XAI implementation
What fails: XaiResponsesLanguageModel creates tool-result objects with result: undefined (lines 215 and 503), but the type definition LanguageModelV3ToolResult.result is NonNullable<unknown>, which excludes undefined.
How to reproduce:
npm run type-checkResult: TypeScript reports two type errors:
packages/xai/src/responses/xai-responses-language-model.ts(215,11): error TS2322: Type 'undefined' is not assignable to type '{}'.
packages/xai/src/responses/xai-responses-language-model.ts(503,21): error TS2322: Type 'undefined' is not assignable to type '{}'.
Expected: Type check should pass. The type was changed to NonNullable<unknown> in commit 26836cf ("spec update: toll result .result is unknown, but not nullable"), but the XAI implementation was not updated to comply with this constraint.
Fix: Changed result: undefined to result: {} in both locations. This satisfies the type constraint while semantically representing "no result data" for server-side tool executions where the XAI API does not return structured result data.
|
tested and verified fix via /chat-xai-web-search on next-openai prompts tried:
updated fixtures as well according to https://github.com/vercel/ai/blob/main/contributing/testing.md#test-fixtures cc: @gr2m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just one nit left:
#9896 (comment)
We can merge but I'd like @nicoalbanese to review docs, even after PR is merged
…` to `NonNullable<JSONValue>` (#9931) ## Background Discovered in #9896 (review) ## Summary As far as I can tell, `result` has to be set to something that is not null or undefined. This makes the spec more precise and would have surfaced the problem of #9896 (review) immediately ## Manual Verification When the fix from this PR is applied, a type error is thrown in #9896

Background
xai supports server-side agentic tool calling through their responses api. this allows models to autonomously execute tools like web search, x search, and code execution on their servers without requiring client-side tool handling
https://docs.x.ai/docs/api-reference#create-new-response
https://docs.x.ai/docs/guides/tools/overview
Summary
Manual Verification
tested with next.js example app using toolloopagent:
Checklist
pnpm changesetin the project root)follow up
use lazySchema, see #9339 on all of xai