How do you handle streaming when the model is emitting tool calls or structured JSON?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
The tension: streaming exists to show incremental progress, but tool calls and JSON are only actionable when syntactically complete. The stream delivers fragments - {"loc ... ation": "par - and naive json.loads on partial data throws.
Full worked answer with diagram
The tension: streaming exists to show incremental progress, but tool calls and JSON are only actionable when syntactically complete. The stream delivers fragments - {"loc ... ation": "par - and naive json.loads on partial data throws.
Mechanics first: providers stream structured content as typed deltas. OpenAI's Chat Completions sends tool_calls deltas carrying an index, the function name (early), and incremental arguments string fragments, and its Responses API emits typed response.function_call_arguments.delta events; Anthropic sends content_block_start / input_json_delta events per block, with blocks possibly interleaving text and tool use. The client's job is an accumulator: route each delta to its block/tool-call by index, concatenate argument fragments, and mark completion on the block-stop/finish event. Only then parse, validate against the tool's schema, and dispatch.
Patterns on top of that:
- Buffer-then-act for execution: never execute a tool on partial arguments. The block-complete signal is your commit point; schema validation is your safety net (models occasionally emit malformed JSON - have a repair-or-retry path).
- Partial-JSON parsing for display: best-effort parsers that close open brackets/strings let you render progressive UI from incomplete JSON (streaming a table row by row, showing which tool is being called and its arguments as they form). Several SDKs now expose partial-parse helpers for exactly this.
- Interleaving: modern models mix prose and tool calls in one response; keep per-block state machines rather than assuming one content type per message.
- Streaming through your own API: if your backend proxies to clients, re-emit clean, versioned events (text delta / tool started / tool result / done) rather than leaking provider formats - this is where multi-provider normalization earns its keep.
- Failure handling: a stream dying mid-JSON means an unusable fragment - treat as a failed generation, retry idempotently, and make double-execution of tools impossible (idempotency keys on tool side effects).
Worth sketching. It separates the display path from the execution path, which is the distinction that keeps a half-formed tool call from firing.
flowchart LR
S["Block start: index and tool name"] --> D["Argument deltas,<br/>concatenated by index"]
D --> D
D -->|"best-effort partial parse"| UI["Progressive UI only"]
D --> B["Block stop or finish event"]
B --> V["Parse, then validate against the schema"]
V -->|"valid"| X["Dispatch once,<br/>guarded by an idempotency key"]
V -->|"malformed"| RP["Repair or retry, never execute"]
What a good answer covers
- The tension: streaming exists to show incremental progress, but tool calls and JSON are only actionable…
- The stream delivers fragments - {"loc ..
- Mechanics first: providers stream structured content as typed deltas
- OpenAI's Chat Completions sends tool_calls deltas carrying an index, the function name (early), and…
- The client's job is an accumulator
Key terms: accumulator · Buffer-then-act · Partial-JSON parsing · Interleaving · Streaming through your own API · Failure handling
In the glossary: Tool call · Interleaving · Idempotency key
Interviewer follow-ups to expect
Where do you put schema validation when the model streams a 10-item JSON array you want to render progressively?
How do you prevent a tool from executing twice when a stream retry replays the tool call?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep