Skip to content

Conversation

@dancer
Copy link
Collaborator

@dancer dancer commented Oct 29, 2025

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

  • added xai responses api with server-side tool execution
  • implemented 5 server-side tools: web_search, x_search, code_execution, view_image, view_x_video
  • added tool name mapping to handle sub-tool variants (browse_page → web_search, x_semantic_search → x_search)
  • added citation extraction from response annotations
  • added multi-turn conversation support with function_call items
  • created next.js example with toolloopagent for multi-step workflows

Manual Verification

tested with next.js example app using toolloopagent:

  • multi-step research queries with web_search and x_search working
  • tool sub-calls properly mapped to parent tools (no infinite loops)
  • citations displaying with url fallback when title missing
  • streaming events handling all 11+ event types correctly
  • 99 tests passing with real api fixtures

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

follow up

use lazySchema, see #9339 on all of xai

@dancer dancer marked this pull request as draft October 29, 2025 19:14
@dancer dancer requested a review from gr2m October 29, 2025 19:14
Copy link
Collaborator

@gr2m gr2m left a 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',
Copy link
Collaborator

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.

Copy link
Collaborator

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({
Copy link
Collaborator

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([
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use lazySchema, see above

@dancer dancer marked this pull request as ready for review October 30, 2025 02:45
@gr2m gr2m self-assigned this Oct 30, 2025
Copy link
Collaborator

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing the next.js chat-xai-web-search example returns in an error for multi-turn

Image

Copy link
Contributor

@vercel vercel bot left a 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-check

Result: 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.

@dancer
Copy link
Collaborator Author

dancer commented Nov 3, 2025

tested and verified fix via /chat-xai-web-search on next-openai

prompts tried:

  1. What are the latest announcements from xAI?
  2. Tell me more about the latest model
  3. When was it released?

updated fixtures as well according to https://github.com/vercel/ai/blob/main/contributing/testing.md#test-fixtures

cc: @gr2m

Copy link
Collaborator

@gr2m gr2m left a 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

@dancer dancer merged commit 5ad1bbe into main Nov 3, 2025
26 checks passed
@dancer dancer deleted the josh/xx branch November 3, 2025 18:11
gr2m added a commit that referenced this pull request Nov 3, 2025
…` 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants