Loading...
Loading...
TypeError: Cannot read properties of undefined (reading 'delta')This error frequently happens when parsing streamed responses from AI APIs (like OpenAI, DeepSeek, or OpenRouter). Often, the last 'chunk' sent in a stream is just `[DONE]` or an empty object without a `choices[0].delta` property. If you try to read `chunk.choices[0].delta.content` directly on this chunk, it crashes.
Always use optional chaining `?.` when reading from streaming chunks so missing properties return undefined instead of throwing.
// ❌ Crashes on the final stream chunk
const text = chunk.choices[0].delta.content;
// ✅ Safe: returns empty string if delta or content is missing
const text = chunk.choices[0]?.delta?.content || "";Most LLM APIs send a specific token when the stream finishes. Stop parsing before processing it.
if (data === "[DONE]") {
break; // Stream finished, exit loop
}
const chunk = JSON.parse(data);
const content = chunk.choices[0]?.delta?.content;