C.W.K.
Stream
Lesson 05 of 06 · published

SSE Wire Format

~22 min · sse, wire-format, raw

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Understanding the raw Server-Sent Events (SSE) format is essential for building custom streaming parsers, debugging, and working without the SDK.

Chat Completions SSE Format

Uses flat data: lines — no event: field:

Responses API SSE Format

Uses both event: and data: fields for typed events:

Events are separated by blank lines (\\n\\n). Chat Completions ends with data: [DONE]. The Responses API has no [DONE] sentinel — the response.completed event signals completion.

The full grammar in five lines

SSE is just newline-delimited frames. A frame is one or more lines of field: value, terminated by an empty line. The fields you'll actually see from OpenAI: data: (the JSON payload), : (a comment, used as keep-alive by some proxies — ignore). The terminator is the literal line data: [DONE].

Why this matters: if you're behind a corporate proxy or a CDN that injects keep-alive comments, hand-rolled parsers that only know about data: lines silently break. Either use iter_lines (which handles this) or write a parser that explicitly skips lines starting with :.

Code

Reading raw SSE with httpx.stream·text
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-...","choices":[{"delta":{"content":" world"},"finish_reason":null}]}

data: {"id":"chatcmpl-...","choices":[{"delta":{},"finish_reason":"stop"}]}

data: [DONE]
Parsing data: lines and the [DONE] sentinel·text
event: response.created
data: {"type":"response.created","response":{...},"sequence_number":1}

event: response.output_text.delta
data: {"type":"response.output_text.delta","delta":"Hello","sequence_number":5}

event: response.completed
data: {"type":"response.completed","response":{...},"sequence_number":15}

External links

Exercise

Make a streaming Chat Completions call with raw httpx.stream — no SDK. Parse the SSE frames yourself. Print a count of (1) data events, (2) keep-alive comments (':' lines), (3) the [DONE] terminator.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.