Skip to content

fix(anthropic): record streamed tool calls and avoid corrupting response text - #8054

Open
ansh-rohilla wants to merge 1 commit into
getsentry:masterfrom
ansh-rohilla:fix/anthropic-stream-tool-calls-8050
Open

ansh-rohilla wants to merge 1 commit into
getsentry:masterfrom
ansh-rohilla:fix/anthropic-stream-tool-calls-8050

Conversation

@ansh-rohilla

Copy link
Copy Markdown

Description

When streaming Anthropic responses that include tool calls (tool_use), input_json_delta chunks were being appended to the same content_blocks list as text deltas. This resulted in two bugs:

  1. gen_ai.response.text was corrupted with raw tool JSON (e.g. {"location": "San Francisco, CA"}).
  2. gen_ai.response.tool_calls was never populated for streamed responses.

This PR addresses this by:

  • Tracking content blocks by index across content_block_start, content_block_delta, and content_block_stop in _collect_ai_data().
  • Capturing tool_use metadata on block start and accumulating JSON fragments in _partial_json.
  • Parsing accumulated JSON into the block's input dictionary on content_block_stop.
  • Passing the normalized content blocks to _set_output_data() in _set_streaming_output_data(), which properly populates gen_ai.response.tool_calls and keeps gen_ai.response.text clean.
  • Updating existing streaming tool use tests to verify gen_ai.response.tool_calls and adding tests for mixed text + tool use streams and block lifecycle events.

Issues

…nse text

Streamed Anthropic tool_use responses accumulated input_json_delta chunks into
the same content_blocks list as text, causing gen_ai.response.text to be
polluted with raw tool JSON and gen_ai.response.tool_calls to never be set.

Track content blocks by index and event type across content_block_start,
content_block_delta, and content_block_stop, parse accumulated tool JSON deltas
into tool input on completion, and forward structured blocks to _set_output_data.

Fixes getsentry#8050
@ansh-rohilla
ansh-rohilla requested a review from a team as a code owner October 17, 2026 12:12

This branch has not been deployed

No deployments
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.

Anthropic integration: streamed tool_use input is appended to gen_ai.response.text and tool_calls are not recorded

1 participant