Streaming LLM responses that break mid-sentence: SSE frames split across TCP packets, and the parser that assumed otherwise
Problem
A chat UI started showing corrupted text: occasional duplicated words, JSON errors in logs every few hundred streams:
SyntaxError: Unexpected token 'i', ...{"type":"content_block_delta","delta":{"text":"Only the first ha..." is not valid JSON
at JSON.parse (<anonymous>)The failure was network-flavored — worse on high-latency links, absent on localhost — which is the signature of a parser assuming message boundaries align with data arrivals.
Root cause
SSE is a line protocol: events are separated by blank lines, and one JSON event can be split across arbitrary TCP segments. A reader that does chunk.toString().split('\n') per data event sees partial JSON. Two lines of code, three failure modes:
1. Partial frames parsed as JSON (the SyntaxError above). 2. Two events arriving in one chunk, so JSON.parse on the whole chunk fails while the naive fix ("try/catch and skip") silently drops real content — the duplicated/lost words. 3. The opposite: a \r\n vs \n mismatch after a proxy, producing events that never terminate.
async function* sseEvents(res: Response) {
const reader = res.body!.getReader();
const decoder = new TextDecoder();
… 18 more lines in the fix🔒 the fix — including 2 code blocks — is members-only. $1/mo unlocks everything.