fix(ai): preserve caller-referenced tool calls in pruneMessages#12602
Open
doganarif wants to merge 1 commit intovercel:mainfrom
Open
fix(ai): preserve caller-referenced tool calls in pruneMessages#12602doganarif wants to merge 1 commit intovercel:mainfrom
doganarif wants to merge 1 commit intovercel:mainfrom
Conversation
pruneMessages builds a set of kept tool call IDs by scanning the last N messages, then strips tool-call/result parts with unknown IDs from older messages. It did not follow providerOptions caller references (e.g. Anthropic code_execution caller.toolId), causing the referenced server_tool_use to be removed while the dependent tool_use was kept. This made Anthropic reject the request with "source tool not found". Add a transitive pass after the initial scan: if a kept tool-call has a caller.toolId reference, that ID is added to the kept set. Fixes vercel#12504
|
|
||
| const result = pruneMessages({ | ||
| messages, | ||
| toolCalls: 'before-last-5-messages', |
Contributor
There was a problem hiding this comment.
Suggested change
| toolCalls: 'before-last-5-messages', | |
| toolCalls: 'before-last-3-messages', |
Test for "caller tool id dependencies" uses a window size (before-last-5-messages) that already includes the referenced caller tool's result in the initial scan, so the transitive closure code is never exercised and the test passes even without the new feature code.
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 using Anthropic's
code_execution_20250825with programmatic tool calling (allowedCallers),convertToModelMessagessplits a multi-step assistant UIMessage into separate ModelMessages at step boundaries. Aserver_tool_useblock ends up in one message, and the dependenttool_use(withcaller.toolIdpointing back to it) ends up in another.pruneMessagesbuildskeptToolCallIdsby scanning the last N messages, then strips tool-call/result parts from older messages if their ID is not in the set. It did not followproviderOptions.anthropic.caller.toolIdreferences, so theserver_tool_usemessage was removed while thetool_usethat references it was kept. Anthropic then rejects the request with"source tool not found".Fix
After the initial
keptToolCallIdsscan, do a transitive pass over all messages: if a kept tool-call part has acaller.toolIdin itsproviderOptions, add that referenced ID to the kept set. The loop repeats until no new IDs are added, handling multi-level dependencies.Test
Added a test with a
code_executionserver tool use and a programmaticlookuptool use that references it viacaller.toolId. Verifies both tool-call IDs and their results survive pruning.Fixes #12504