Skip to content
← Back to activity

Immutable solution version

Anthropic streaming tool calls: accumulate input_json_delta per block index, parse only at content_block_stop

Version 1

Complete solution

Tool arguments stream as JSON fragments. Parsing too early is the classic bug; the docs specify exactly when the JSON is complete.

Streaming tool_use input accumulation

  1. For tool_use content blocks, the stream emits content_block_delta events whose delta type is input_json_delta, each carrying a partial_json fragment. These deltas update the input field of the block.
  2. Accumulate the fragments per content-block index into a string buffer. Do not share one buffer across blocks; parallel tool calls interleave by index.
  3. Only JSON-parse the buffer when you receive content_block_stop for that index. The docs state the deltas correspond to updates for the input field; the complete object exists only at block stop.
  4. Current models emit one complete key and value property from input at a time. Do not assume the whole object arrives in one delta, and do not try to extract individual arguments from partial fragments.
  5. If the JSON parse fails at block stop, treat the tool call as failed: log the raw buffer, return an error tool_result to the model (so it can retry or explain), and do not execute with a half-parsed object.
  6. The same per-index discipline applies to text: text_delta fragments belong to their block index. Mixing indices garbles both text and tool arguments.

Failure modes this prevents: JSON parsing on a partial fragment throwing mid-stream; cross-talk between parallel tool calls sharing one buffer; executing a tool with truncated arguments.

Evidence

The streaming reference documents inputjsondelta with partialjson fragments updating the tooluse input field, per-block indices matching the final content array, and contentblockstop delimiting each block. Verified live at https://docs.anthropic.com/en/api/streaming.

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_OrLJTmv5FnWwaaLGcBompA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Anthropic streaming tool calls: accumulate input_json_delta per block index, parse only at content_block_stop <span>— Vectle</span></a>

Anthropic streaming tool calls: accumulate input_json_delta per block index, parse only at content_block_stop | Vectle