fix(ai): Don't create duplicate tool parts when models call non-existent tools#12774
Merged
gr2m merged 1 commit intovercel:mainfrom Feb 26, 2026
Merged
Conversation
gr2m
approved these changes
Feb 26, 2026
Collaborator
gr2m
left a comment
There was a problem hiding this comment.
Looks good, thank you Josh!
pawaca
added a commit
to pawaca/ai
that referenced
this pull request
Mar 27, 2026
Follow-up to vercel#12774: apply the same existing-part lookup pattern to the tool-input-available case so that a dynamic flag mismatch between tool-input-start and tool-input-available does not create a duplicate part. Closes vercel#12772
4 tasks
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
When a model calls a tool that doesn't exist in the tools object (e.g. a hallucinated tool name), processUIMessageStream creates two message parts for the same toolCallId — one static (tool-{toolName}) and one dynamic (dynamic-tool). This causes downstream failures when the conversation is continued, since the model API rejects requests containing two tool results for the same tool call ID.
Summary
Fixes a bug in
processUIMessageStreamwhere a model calling a non-existent tool produces two UI message parts for the sametoolCallId— one static (tool-{toolName}) and one dynamic (dynamic-tool). This happens becausetool-input-startcreates a static part (whendynamicis undefined for an unknown tool), but the subsequenttool-input-errorarrives withdynamic: trueand only searches for adynamic-toolpart, missing the existing static one and creating a duplicate. The fix checks for an existing part bytoolCallIdbefore branching onchunk.dynamic, so the error updates the existing part in place instead of creating a second one.Manual Verification
Manually verified against my own application. After the fix, I got a single tool part like:
Checklist
pnpm changesetin the project root)Related Issues
Fixes #12772