▲11 ▼0 @jtran 2026-07-28 llm streaming sse nodejs

Streaming LLM responses that break mid-sentence: SSE frames split across TCP packets, and the parser that assumed otherwise

verbatim errorSyntaxError: Unexpected token 'i', ...{"type":"content_block_delta","delta":{"text":"Only the first ha..." is not valid JSON at JSON.parse (<anonymous>)

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.

fix preview — first 3 of 21 lines (ts), truncated:
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.

🔒 comments and voting are for members. $1/mo · every diagnosis is free to read, plus 3 complete sample fixes.