Recipes: SSE parser, stream assembler and read_stream
Three recorded streams, each cut into 7-character chunks: text with usage, a tool call, and a stream that ends before [DONE].
Three recipes to copy: an SSE parser, a stream assembler, and a cancel-safe consumer that always ends in a known state and always cleans up. Plus the checklist to run before you ship a streaming feature.
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 chunked(text, size) that cuts a string into pieces of size characters (the last one may be shorter), and use it to check that a parser gives the same events for sizes 1, 3 and 1000. Use the SseParser below.
Write summarise(result) that turns a result dictionary with text, finish and usage (or None) into one line such as text=[Hi] finish=stop tokens=15, writing tokens=- when usage is None.
Explain it without notes
Walk through what happens to one chunk of bytes from the network until it becomes text on the screen.
Which three states can a cancel-safe consumer end in, and what must always happen?
Give five items you would put on a pre-flight checklist for a streaming feature.
Expected output
text stream: text=[Hello!] finish=stop total_tokens=15 calls=-
tool stream: text=[] finish=tool_calls total_tokens=- calls=get_time({})
cut stream : error: stream ended before [DONE] (text so far: Hel)