Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.2 — Server-Sent Events from First Principles
LLM Engineering · Example 2 of 2

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.

01

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).

02

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

01

Describe the SSE wire format used by chat completion streams.

02

Why does a parser that splits each chunk by newline fail, and how does a buffer fix it?

03

Why is [DONE] special, and what happens if you pass it to a JSON parser?

04

Why must a streaming decoder be used for bytes, and what goes wrong without it?

A buffering SSE parser gives the same text for every chunk size
Sign in to run this example in your browser.

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!