The naive parser loses data when chunks cut lines
The same stream is cut into chunks of 1000, 170 and 100 characters. Only the first size survives a parser that assumes whole lines.
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!, unreadable lines: 0
chunk size 170: 2 chunks, text: Hello, unreadable lines: 0
chunk size 100: 4 chunks, text: Hel, unreadable lines: 3