Joining tool-call fragments by index
Two parallel calls, with their argument text cut into fragments that arrive interleaved. Java builds the JSON with small Map helpers.
A streamed reply arrives as many small deltas. An assembler adds the content deltas into text, joins tool-call argument fragments by their index, remembers the finish reason from near the end, and reads the token usage from the last chunk.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write join_text(chunks) that takes a list of chunk dictionaries (each like {"choices": [{"delta": {...}}]}) and returns the joined content text, skipping chunks with no choices or no content.
Write join_arguments(fragments) that takes a list of (index, text) pairs and returns a dictionary from index to the joined text, in the order the fragments arrived.
Explain it without notes
Explain why a streamed reply uses deltas, and what an assembler does with them.
How do you assemble streamed tool calls, and why must you not parse a fragment on its own?
Where do finish_reason and usage appear in a stream, and what should you do if they are missing?
Why does chunk["choices"][0]["delta"] crash on some real streams?
Expected output
finish_reason: tool_calls
call 0 (call_a): get_weather(Pune)
raw arguments: {"city":"Pune"}
call 1 (call_b): get_weather(Delhi)
raw arguments: {"city":"Delhi"}