E3 · Lesson 08
A streamed tool call is half a JSON object
Streaming sends the reply as it is generated, so a person sees the first words in a fraction of the time the whole reply takes. For text that is pure gain: every prefix of a sentence can be shown. For a tool call it is a trap. No prefix of a JSON object is a JSON object, and acting on a prefix is acting on a call the model has not finished writing.
What arrives
With "stream": true, an OpenAI-compatible server sends server-sent events (WHATWG, HTML Standard, section 9.2): one data: line per chunk, ending with data: [DONE]. A tool call can arrive in fragments, keyed by index:
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function",
"function":{"name":"read_file","arguments":""}}]}}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"pa"}}]}}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"th\": \"a.txt\"}"}}]}}]}
data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}
data: {"choices":[],"usage":{"prompt_tokens":140,"completion_tokens":31}}
data: [DONE]
The call's id and name come in its first fragment, and its arguments are every fragment's arguments joined in arrival order. Later fragments carry no id, and some servers send an empty one, so an assembler that overwrites the id with each fragment's ends up with none. Usage arrives last, in a chunk with no choices, and only if you asked for it (stream_options: {"include_usage": true}). A loop that streams without asking has no token counts, and its token budget never fires (lesson 6).
Servers differ, so write for the hardest one
OpenAI's API sends arguments in fragments, as above (its function-calling guide shows response.function_call_arguments.delta events for the Responses API). Ollama sends each tool call whole, in one chunk:
data: {"choices":[{"index":0,"delta":{"content":"","tool_calls":[{"id":"call_6n2ulo9l","index":1,
"type":"function","function":{"name":"read_file","arguments":"{\"path\":\"notes/b.txt\"}"}}]}, ...}]}
(measured on qwen3:4b, 25 October 2026). An assembler written against Ollama, one chunk per call, passes every local test and breaks on the first hosted model. loop/stream.py's assemble is written for fragments and handles whole calls as the special case of one fragment. The check feeds it both.
When to act
Show text as it arrives if a person is watching. Act on a tool call only after three things have happened: the stream has ended with finish_reason set, the call's arguments have been joined, and they have passed lesson 2's checks. [DONE] alone is not enough: ChatClient treats a stream that ends without finish_reason, cleanly or not, as a dropped connection, because nothing in it says the reply was finished. A UI that displays "Sending e-mail to …" from the first fragment may display a recipient that the finished call does not contain. A partial string is not a promise of its own continuation.
What streaming buys, measured
The study sends the same prompt ten times each way, streamed and not, and records the time to the first content or tool-call byte and to the last byte. The numbers are in the build. Two effects are worth predicting before you look.
- For a tool call, the first useful byte is the call itself, and with Ollama it arrives whole, near the end. Streaming buys almost nothing.
- For a thinking model, the first content byte comes after the thinking. Ollama streams thinking in a separate
reasoningfield thatassembleignores, so time to first answer byte is close to total time. Streaming the reasoning to the user is a product decision. Putting it into the history is a context decision (E2).
Streaming does not make the model faster. It changes when the waiting happens, and for a loop that cannot act until the call is complete, mostly it does not change that either.
Check yourself
- Fragments for index 1 arrive before any for index 0. What order do the assembled tool calls come in, and why?
- A stream ends after
{"pabecause the connection dropped. What should the loop do? - Why must the assembler take a call's id from its first fragment, and not overwrite it with later ones?
Answers
- Index order, 0 then 1. The
indexis the model's order of calls, and arrival order is an accident of the network. Lesson 4's rule (results in the order asked) needs that order. - Treat it as a failed model call.
ChatClientraisesRemoteProtocolErrorwhen a stream ends without afinish_reason, whether the connection dropped or closed cleanly, andclassifyretries the whole call (lesson 5). Never parse or repair the fragment:{"pacould have become any call. - Later fragments normally omit the id. A server that repeats it is fine, but a fragment carrying a different id would otherwise silently re-key the call, and the tool result would answer an id the model never issued. The first id is the one the model announced.