feat(xai): Correctly report tool-calls finish reason for xAI models#12236
Closed
subhraneel2005 wants to merge 3 commits intovercel:mainfrom
Closed
feat(xai): Correctly report tool-calls finish reason for xAI models#12236subhraneel2005 wants to merge 3 commits intovercel:mainfrom
subhraneel2005 wants to merge 3 commits intovercel:mainfrom
Conversation
a13d153 to
84c2bcd
Compare
Contributor
aayush-kapoor
left a comment
There was a problem hiding this comment.
this will not work - the incorrect reason is being passed through AI Gateway and it's not on the xai provider
Author
|
okayy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
This change was necessary because xAI models, particularly
grok-4.1-fast-reasoning, were incorrectly reporting afinishReasonof "stop" when they had, in fact, performed tool calls. This led to an inconsistentFinishReasonbeing 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 oftool_callobjects in the response, rather than a distinctfinish_reasonstring, 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 oftool_callobjects in the xAI model's response (for bothdoGenerateanddoStreammethods). If tool calls are identified, thefinishReasonis now overridden and set to'tool-calls'. This ensures that the AI SDK consistently and accurately reports theFinishReasonas'tool-calls'whenever an xAI model invokes a tool, regardless of the rawfinish_reasonstring provided by the xAI API.Manual Verification
The fix was verified by running the comprehensive test suite for the
packages/xaipackage. All 154 tests passed across both Node.js and Edge environments, indicating that the changes correctly address thefinishReasonissue without introducing regressions or breaking existing functionality.Checklist
pnpm changesetin the project root)Related Issues