Your AI Stream’s Next Chunk Is Probably Not a JSON Message
Ferid Brković
4 min read4 hours ago
A small JavaScript parser that survives split records, split characters, and an unfinished response.
Press enter or click to view image in full size
Imagine your chat interface receiving this perfectly valid message from your backend:
{"type":"delta","text":"café ☕"}
Your development demo works. Then a response arrives in two pieces, with the first piece ending halfway through the word “text.” JSON.parse throws before the next piece reaches your code.
Nothing about that failure tells you the model produced bad JSON. Your frontend tried to parse a delivery fragment as a complete application message.
A streaming interface needs an explicit rule for deciding when a message is complete. That rule belongs in your parser and in your tests.
Start with the format your server actually sends
For this example, your own backend exposes newline-delimited JSON, or NDJSON.…