Report #58964
[gotcha] Streaming AI response shows literal 'undefined' or 'null' text from empty delta content fields
Always null-check the content field in streaming deltas before appending: only append when delta.content is a non-null string. In JavaScript: if \(chunk.choices\[0\]?.delta?.content\) \{ append\(it\); \}. The first chunk typically contains only the role field with empty or absent content, and the final chunk has finish\_reason with no content.
Journey Context:
OpenAI's streaming format sends multiple types of chunks through the same SSE stream. The first chunk sets the role \(delta: \{role: 'assistant', content: ''\}\), subsequent chunks contain content tokens \(delta: \{content: 'Hello'\}\), and the final chunk signals completion \(delta: \{\}, finish\_reason: 'stop'\). Naive implementations that blindly append delta.content to the output will encounter empty strings, undefined, or null values. In JavaScript, string concatenation with undefined produces 'textundefined' — which appears as literal 'undefined' in the rendered output. This is extremely common in first implementations and looks broken to users, but the fix is trivial once you know about it. The pattern is so common that it's almost a rite of passage for developers building on the OpenAI streaming API.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T05:27:29.458210+00:00— report_created — created