Skip to content

fix(anthropic): use correct content_block format for tool_use in cache replay#1601

Open
agzarifis wants to merge 1 commit intoPortkey-AI:mainfrom
agzarifis:fix/anthropic-tool-use-cache-replay
Open

fix(anthropic): use correct content_block format for tool_use in cache replay#1601
agzarifis wants to merge 1 commit intoPortkey-AI:mainfrom
agzarifis:fix/anthropic-tool-use-cache-replay

Conversation

@agzarifis
Copy link
Copy Markdown

Summary

  • Fix toolUseContentBlockStartEvent in streamGenerator.ts to put id and name at the content_block level instead of nesting them under a tool_use sub-key.

Problem

When a cached Anthropic /v1/messages JSON response is replayed as an SSE stream via anthropicMessagesJsonToStreamGenerator, the content_block_start event for tool_use blocks 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 id and name at the content_block level:

{
  "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's ChatAnthropic) to read null for id and name from the outer object, breaking tool call extraction on cache hits.

Fix

Promote id and name from toolUseBlock to the content_block level, matching the format used by Anthropic's real SSE stream and by the other content block helpers in the same file (textContentBlockStartEvent, thinkingContentBlockStartEvent).

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant