Streaming Partial Tool-Call Arguments in LLM Apps: Parsing, Validation and Fallbacks

Streamed tool calls arrive as JSON fragments, not clean objects. Learn the parse-once pattern for OpenAI and Anthropic, with validation and fallbacks.

By Galactic News Editorial · Published 2026-11-24T04:44:24+00:00 · Updated 2026-11-24T04:44:24+00:00

Streaming partial tool-call arguments LLM apps parsing validation fallbacks

Streaming Partial Tool-Call Arguments in LLM Apps: Parsing, Validation and Fallbacks

When a language model calls a tool in a streamed response, the arguments do not arrive as clean JSON. They arrive as fragments. A single argument string may split mid-key, mid-value, or mid-escape sequence. Parse a fragment too early and your code crashes. Parse it too late and the user stares at a frozen screen. The fix is a small, disciplined pattern: buffer fragments per call, parse once when the stream signals completion, and validate before you execute.

This guide explains how streaming partial tool-call arguments LLM apps receive actually work across providers, the accumulation pattern that handles them, and the fallbacks that keep a broken stream from breaking your app.

What Arrives on the Wire

Streaming tool calls work the same way at every major provider, with different envelopes. The model emits the tool call in pieces, and your code reassembles them.

OpenAI Chat Completions. Each server-sent event is a chat.completion.chunk. Tool calls arrive inside choices[0].delta.tool_calls. The first chunk for a call carries the id and function.name. Every later chunk carries only an incremental function.arguments string fragment. The index field is the only reliable key: it is present on every chunk. Parallel tool calls get different index values, and chunks for different indices may interleave. Concatenate the fragments per index, and parse only after finish_reason arrives.

Anthropic Messages API. A streamed turn is a sequence of events: message_start, content_block_start, content_block_delta, content_block_stop, message_stop. For a tool call, the start event carries the block type, name, and id, with input set to an empty object. The actual input arrives as input_json_delta events, each carrying a partial_json string fragment. Concatenate them in arrival order and parse when the block closes. Anthropic also offers fine-grained tool streaming: set eager_input_streaming to true on a tool, and fragments arrive without server-side buffering or JSON validation, which cuts time to the first fragment for large parameters.

Google Gemini. The Interactions API streams tool arguments as partial arguments in the same fragmented style, and its docs state the contract directly: aggregate the deltas to reconstruct the complete tool calls before executing them. The older generateContent streaming surface is less explicit about fragmentation, so confirm which surface your SDK version uses before relying on partial arguments.

ProviderFragment fieldAccumulation keyParse when
OpenAI Chat Completionsdelta.tool_calls[].function.argumentsindexfinish_reason arrives
Anthropic Messages APIinput_json_delta.partial_jsoncontent-block indexcontent_block_stop
Google Gemini (Interactions API)partial arguments deltasevent indexstream terminates

Streaming Partial Tool-Call Arguments LLM: The Accumulation Pattern

The correct pattern has three rules. Buffer per index. Append every fragment. Parse exactly once, at the end. Here is the shape in Python, using the OpenAI SDK:

calls = {}  # index -> {id, name, arguments}
for chunk in stream:
    for tc in chunk.choices[0].delta.tool_calls or []:
        slot = calls.setdefault(tc.index, {"id": None, "name": "", "arguments": ""})
        if tc.id:
            slot["id"] = tc.id
        if tc.function and tc.function.name:
            slot["name"] = tc.function.name
        if tc.function and tc.function.arguments:
            slot["arguments"] += tc.function.arguments
# Only now, after the stream ends, parse:
for index in sorted(calls):
    args = json.loads(calls[index]["arguments"])
    execute(calls[index]["name"], args)

The Anthropic version is structurally identical: open a buffer on content_block_start for a tool_use block, append each partial_json fragment, and call json.loads once at content_block_stop. Most official SDKs also ship an accumulator helper that does this for you. Use it when it exists. Write the manual pattern only when your SDK has none.

Two details catch people out. First, the id and name arrive exactly once, in the first delta. Later chunks carry nulls for those fields, so never overwrite a stored id or name with a null. Second, never key buffers by tool name. Parallel calls to the same tool share a name but have different indices, and name-keyed buffers corrupt each other.

Validation Before Execution

A fully assembled argument string is still untrusted input. The model can produce JSON that parses but violates your schema: a missing required field, a wrong type, an out-of-range value. Validate before you call the tool.

  1. Parse with a strict decoder. Reject trailing data and duplicate keys if your language supports it. A parse error means the stream was cut short, not that the arguments are almost right.
  2. Validate against the tool schema. Check required fields, types, and value ranges. Use the same JSON Schema you sent the model. If validation fails, do not guess a repair.
  3. Enforce your own limits. Cap string lengths, array sizes, and numeric ranges at the application level. A model can request a thousand items or a ten-megabyte string. Your tool should never accept that blindly.
  4. Sanitise before side effects. Any argument that reaches a shell, a database, or a file path must be checked or parameterised. Tool arguments are model output, not trusted input.

With Anthropic's fine-grained streaming, validation matters even more. The server streams fragments without validating them, so the accumulated string might not be valid JSON at all. Treat fine-grained mode as a latency optimisation for large parameters, and keep the same parse-once-at-stop discipline.

Fallbacks When the Stream Breaks

Streams die mid-response. Networks drop, providers time out, and clients disconnect. Your app needs a plan for each case.

Partial arguments, no completion. If the stream ends without a finish signal, discard the buffer. Never execute a half-assembled call. Log the truncated arguments for debugging, then retry the whole turn or tell the user the request failed.

Parse failure after assembly. Retry the request once with streaming disabled. A non-streamed response returns the complete tool call in one object, which removes the fragmentation problem entirely. If that also fails, surface the error instead of retrying forever.

Validation failure. Send the model a correction message describing the schema violation and ask it to retry the call. One repair round is reasonable. More than that usually means the prompt or schema is unclear, and repeated retries burn tokens without progress.

Timeouts. Set a per-turn timeout and a per-chunk idle timeout. A stream that produces nothing for too long is dead, even if the connection is technically open. Abort it, release the buffer, and retry or report the failure.

Bottom Line

Streaming partial tool-call arguments is a solved problem with a small pattern: buffer fragments per index, parse once at completion, validate against the schema, and never execute a half-assembled call. OpenAI, Anthropic, and Gemini all deliver fragments the same way, differing only in event names. Build one provider-normalising accumulator, put validation between parsing and execution, and give every stream a timeout and a non-streamed retry path. The user sees fast, progressive tool calls. Your app stays safe when the stream does not cooperate.