Skip to content

feat(xai): Correctly report tool-calls finish reason for xAI models#12236

Closed
subhraneel2005 wants to merge 3 commits intovercel:mainfrom
subhraneel2005:feat/xai-tool-calls-finish-reason
Closed

feat(xai): Correctly report tool-calls finish reason for xAI models#12236
subhraneel2005 wants to merge 3 commits intovercel:mainfrom
subhraneel2005:feat/xai-tool-calls-finish-reason

Conversation

@subhraneel2005
Copy link

@subhraneel2005 subhraneel2005 commented Feb 4, 2026

Background

This change was necessary because xAI models, particularly grok-4.1-fast-reasoning, were incorrectly reporting a finishReason of "stop" when they had, in fact, performed tool calls. This led to an inconsistent FinishReason being reported by the AI SDK, leading to potential inconsistencies in how tool-calling completions were handled. The xAI API documentation indicates that tool calls are primarily signaled by the presence of tool_call objects in the response, rather than a distinct finish_reason string, and a general "stop" reason could be returned even with an active tool call.

Summary

This pull request modifies the xAI provider within the AI SDK (packages/xai/src/xai-chat-language-model.ts). The changes introduce logic to explicitly detect the presence of tool_call objects in the xAI model's response (for both doGenerate and doStream methods). If tool calls are identified, the finishReason is now overridden and set to 'tool-calls'. This ensures that the AI SDK consistently and accurately reports the FinishReason as 'tool-calls' whenever an xAI model invokes a tool, regardless of the raw finish_reason string provided by the xAI API.

Manual Verification

The fix was verified by running the comprehensive test suite for the packages/xai package. All 154 tests passed across both Node.js and Edge environments, indicating that the changes correctly address the finishReason issue without introducing regressions or breaking existing functionality.

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)

Related Issues

@subhraneel2005 subhraneel2005 force-pushed the feat/xai-tool-calls-finish-reason branch from a13d153 to 84c2bcd Compare February 4, 2026 12:28
Copy link
Contributor

@aayush-kapoor aayush-kapoor left a comment

Choose a reason for hiding this comment

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

this will not work - the incorrect reason is being passed through AI Gateway and it's not on the xai provider

@subhraneel2005
Copy link
Author

okayy

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.

2 participants