Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.3 — Assembling a Streamed Reply
LLM Engineering · Example 1 of 2

Assembling text, the stop reason and usage

Six scripted chunks: role only, three text deltas, the stop chunk and a final chunk with the token counts and no choices.

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.

01

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.

02

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

01

Explain why a streamed reply uses deltas, and what an assembler does with them.

02

How do you assemble streamed tool calls, and why must you not parse a fragment on its own?

03

Where do finish_reason and usage appear in a stream, and what should you do if they are missing?

04

Why does chunk["choices"][0]["delta"] crash on some real streams?

Assembling text, the stop reason and usage
Sign in to run this example in your browser.

Expected output

chunk 1: text so far = []
chunk 2: text so far = [Hel]
chunk 3: text so far = [Hello]
chunk 4: text so far = [Hello!]
chunk 5: text so far = [Hello!]
chunk 6: text so far = [Hello!]
role: assistant
finish_reason: stop
usage: prompt=12 completion=3 total=15