Report #72462
[gotcha] Anthropic streaming breaks when response contains both text and tool\_use — naive concatenation produces garbled output
Track content blocks by their index from content\_block\_start events. Buffer content\_block\_delta values separately per block type: accumulate text deltas for text blocks, accumulate input\_json\_delta for tool\_use blocks. Render and process each block type independently.
Journey Context:
Anthropic's Messages API can return multiple content blocks in a single response: a text block followed by a tool\_use block, or multiple tool\_use blocks interleaved with text. When streaming, these arrive as separate content\_block\_start → content\_block\_delta → content\_block\_stop event sequences, each with an index. A naive implementation that concatenates all delta values into one string will mix human-readable text with JSON tool-call arguments, producing garbage output. This is different from OpenAI's approach where tool calls are a separate top-level field in the response object. Teams migrating from OpenAI to Anthropic frequently hit this because their streaming parser assumes a single content stream. The fix requires restructuring the streaming handler to maintain per-block buffers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T04:13:02.227210+00:00— report_created — created