Streaming SSE Delta Assembler
Background
Streaming LLM responses arrive as Server-Sent Events: each data: {...} line carries a partial token (delta). The client must concatenate these deltas into the final text, skipping event-control lines and tolerating malformed entries without crashing.
Problem statement
Implement assemble_sse(lines). Walk the input lines; any line starting with data: is parsed as JSON; if it contains a delta field, append str(delta) to the output. Skip everything else (including malformed JSON) without raising.
Input
lines—list[str]of SSE-format event lines, in stream order.
Output
Returns a str — the concatenated delta values.
Examples
Example 1 — standard streaming
Input:
['data: {"delta": "Hello"}',
'data: {"delta": " world"}',
'',
'data: {"delta": "!"}']
Output: "Hello world!"
Example 2 — non-data lines skipped
Input: ['event: open', 'data: {"delta": "x"}', 'id: 1']
Output: "x"
Example 3 — malformed JSON tolerated
Input: ['data: not-json', 'data: {"delta": "ok"}']
Output: "ok"
Constraints
- Empty input returns
"". - Lines that don't start with
data:are silently skipped. - Malformed JSON lines are silently skipped, not raised.
deltamay be any JSON value; coerce tostrbefore concatenating.
Notes
- Why be lenient. SSE streams routinely carry heartbeats, ID lines, and event-type markers. Raising on these would break every client.
- Production extras. Real assemblers also handle
[DONE]sentinel, recovery from broken UTF-8, and reconnection withLast-Event-Id.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Standard example assembly
- •Non-data lines skipped (sample)
- •Malformed JSON tolerated reference