A buffering SSE parser gives the same text for every chunk size
Feed chunks, cut at each blank line, keep the unfinished tail. A keep-alive comment is ignored and [DONE] stops the reader.
Streaming APIs send server-sent events: lines that start with data:, separated by blank lines, ending with [DONE]. Network chunks can cut a line anywhere, so a parser must buffer until the blank line.
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 parse_event(block) that takes the text of one event (without the final blank line) and returns the joined data of its data: lines, or None if there are none (for example a comment).
Write chunk_everywhere(text) that returns the text cut into every possible 2-piece split ([(text[:i], text[i:]) ...] for i from 1 to len(text) - 1) so you can test a parser at every cut point.
Explain it without notes
Describe the SSE wire format used by chat completion streams.
Why does a parser that splits each chunk by newline fail, and how does a buffer fix it?
Why is [DONE] special, and what happens if you pass it to a JSON parser?
Why must a streaming decoder be used for bytes, and what goes wrong without it?
Expected output
chunk size 1000: 1 chunks, text: Hello!
chunk size 60: 6 chunks, text: Hello!
chunk size 20: 17 chunks, text: Hello!
chunk size 1: 326 chunks, text: Hello!