fix(anthropic): use correct content_block format for tool_use in cache replay#1601
Open
agzarifis wants to merge 1 commit intoPortkey-AI:mainfrom
Open
fix(anthropic): use correct content_block format for tool_use in cache replay#1601agzarifis wants to merge 1 commit intoPortkey-AI:mainfrom
agzarifis wants to merge 1 commit intoPortkey-AI:mainfrom
Conversation
…e replay
When a cached Anthropic /v1/messages JSON response is replayed as an
SSE stream, toolUseContentBlockStartEvent wraps the entire ToolUseBlock
inside a nested `tool_use` key:
content_block: { type: "tool_use", tool_use: { id, name, input: {} } }
The Anthropic streaming spec puts id and name at the content_block
level:
content_block: { type: "tool_use", id, name, input: {} }
The nested format causes downstream clients (e.g. langchain-anthropic)
to extract null id and name from the outer object, breaking tool call
processing on cache hits.
Promote id and name to the content_block level to match the real
Anthropic SSE format.
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.
Summary
toolUseContentBlockStartEventinstreamGenerator.tsto putidandnameat thecontent_blocklevel instead of nesting them under atool_usesub-key.Problem
When a cached Anthropic
/v1/messagesJSON response is replayed as an SSE stream viaanthropicMessagesJsonToStreamGenerator, thecontent_block_startevent fortool_useblocks is emitted as:{ "type": "content_block_start", "index": 0, "content_block": { "type": "tool_use", "tool_use": { "id": "toolu_01X...", "name": "my_tool", "input": {} } } }The Anthropic streaming spec puts
idandnameat thecontent_blocklevel:{ "type": "content_block_start", "index": 0, "content_block": { "type": "tool_use", "id": "toolu_01X...", "name": "my_tool", "input": {} } }This causes downstream clients (e.g.
langchain-anthropic'sChatAnthropic) to readnullforidandnamefrom the outer object, breaking tool call extraction on cache hits.Fix
Promote
idandnamefromtoolUseBlockto thecontent_blocklevel, matching the format used by Anthropic's real SSE stream and by the other content block helpers in the same file (textContentBlockStartEvent,thinkingContentBlockStartEvent).