# KeyError: 'content' in streaming response chunk

- **ID:** `llm/streaming-chunk-missing-content-field`
- **Domain:** llm
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

Streaming chunks from LLM APIs may omit the 'content' field when they contain only metadata (e.g., finish_reason, usage info) or when the chunk is empty due to internal processing.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai-python>=1.0.0 | active | — | — |
| gpt-4-1106-preview | active | — | — |
| gpt-3.5-turbo-1106 | active | — | — |

## Workarounds

1. **Use robust parsing that handles all chunk structures. Example: `for chunk in response: if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content: yield chunk.choices[0].delta.content`** (90% success)
   ```
   Use robust parsing that handles all chunk structures. Example: `for chunk in response: if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content: yield chunk.choices[0].delta.content`
   ```
2. **Use the OpenAI Python library's built-in streaming iterator which handles these edge cases internally: `for chunk in client.chat.completions.create(..., stream=True):` and access via `chunk.choices[0].delta.content` with null checks.** (95% success)
   ```
   Use the OpenAI Python library's built-in streaming iterator which handles these edge cases internally: `for chunk in client.chat.completions.create(..., stream=True):` and access via `chunk.choices[0].delta.content` with null checks.
   ```
3. **Implement a retry with exponential backoff for chunks that raise KeyError, logging the raw chunk for debugging.** (70% success)
   ```
   Implement a retry with exponential backoff for chunks that raise KeyError, logging the raw chunk for debugging.
   ```

## Dead Ends

- **** — This handles missing content but doesn't account for chunks that have no 'choices' array at all, which can still cause errors. (60% fail)
- **** — This causes silent data loss or crashes when the error occurs. (90% fail)
- **** — Streaming inherently delivers partial data; the issue is about chunk structure, not completeness. (70% fail)
