gotchatypescriptMajor
OpenAI streaming responses lose chunks when not properly buffering SSE
Viewed 0 times
openai@4.x
streamingopenaisseserver-sent-eventsdeltachunksbuffer
Error Messages
Problem
When consuming OpenAI streaming responses with the stream option, partial chunks can be dropped or concatenated incorrectly if you process the raw stream without proper SSE parsing. The result is garbled text or missing tokens in the final output.
Solution
Use the official openai SDK's stream helper which handles SSE framing automatically. If consuming raw HTTP, buffer on newline boundaries and only parse lines prefixed with 'data: ', skipping 'data: [DONE]' sentinel. Accumulate delta.content values rather than replacing them.
Why
OpenAI streaming uses Server-Sent Events format. Each SSE message is newline-delimited and may arrive across multiple TCP segments. Without buffering you risk splitting a JSON payload mid-parse.
Gotchas
- The final 'data: [DONE]' line must be handled explicitly — JSON.parse will throw on it
- delta.content can be undefined for the first and last chunks — always use ?? ''
- Stream can emit empty data lines as keep-alives — guard against empty strings before parsing
Code Snippets
Correct OpenAI streaming with SDK
import OpenAI from 'openai';
const client = new OpenAI();
const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello' }],
stream: true,
});
let fullText = '';
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content ?? '';
fullText += delta;
process.stdout.write(delta);
}Context
Building chat UIs or any feature that streams LLM tokens to the client
Revisions (0)
No revisions yet.