Report #90719
[gotcha] SSE stream from OpenAI API hangs or throws error when data: \[DONE\] termination signal is not handled
Explicitly check each SSE data line for the string '\[DONE\]' before attempting JSON.parse. When received, close the stream connection and finalize the UI state. Never pass this line to your JSON parser.
Journey Context:
OpenAI's streaming API terminates with the SSE event 'data: \[DONE\]'. If your consumer doesn't check for this, two things go wrong: \(1\) JSON.parse\('\[DONE\]'\) throws a SyntaxError that may crash the stream handler silently, and \(2\) without explicit stream closure, the connection may stay open waiting for more events, leaving the UI in a perpetual loading state. This is especially painful with browser EventSource which auto-reconnects on unexpected closures. The fix is trivial — a string equality check before parsing — but it's the \#1 omission in first implementations because the streaming format detail is buried in the docs and the error manifests as a hang, not a crash.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T10:51:54.050197+00:00— report_created — created