fix: prevent duplicate tool_use IDs causing API 400 errors #10760
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.
Summary
Fixes API 400 error "tool_use ids must be unique" occurring in v3.41.0 when using Anthropic Claude models with native tool calling.
Root Cause
NativeToolCallParser.rawChunkTrackeris keyed byindex(not by tool ID). When the same tool call ID arrives on multiple indices (e.g., during stream retry/reconnection):tool_call_starteventassistantMessageContentSolution
Two-layer defense against duplicate tool_use IDs:
Layer 1: Streaming Guard
Check
streamingToolCallIndices.has(event.id)before adding new tool_use blocks during streaming to prevent duplicates from duplicatetool_call_startevents.Layer 2: Pre-flight Deduplication
Filter duplicate tool_use IDs before building the API request content as defense-in-depth.
Performance
Both layers add negligible overhead:
Testing
Closes COM-494
Important
Fixes API 400 errors by preventing duplicate
tool_useIDs with two deduplication layers inTask.ts.tool_useIDs causing API 400 errors by implementing two deduplication layers inTask.ts.streamingToolCallIndicesto avoid duplicatetool_call_startevents during streaming.tool_useIDs before building API request content.tool_call_start.duplicate-tool-use-ids.spec.tscovering both deduplication layers.tool_useblocks are added during stream retries or reconnections.This description was created by
for 2bbd6fd. You can customize this summary. It will automatically update as commits are pushed.