OpenAIStreamParseError
openai.OpenAIStreamParseError
Stack trace
openai.OpenAIStreamParseError: Incomplete chunk received while parsing SSE stream
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 123, in _stream
for chunk in self._stream_response(response):
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 98, in _stream_response
event = self._parse_sse_event(chunk)
File "/usr/local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 75, in _parse_sse_event
raise OpenAIStreamParseError("Incomplete chunk received while parsing SSE stream") Why it happens
This error happens because the streaming response from OpenAI's SSE endpoint sends a chunk of data that is incomplete or truncated, causing the SSE parser to fail. Network interruptions, proxy buffering, or client-side read timeouts can cause partial chunks to be received.
Detection
Monitor streaming responses for OpenAIStreamParseError exceptions and log the raw chunk data to detect incomplete or malformed SSE chunks before they cause crashes.
Causes & fixes
Network interruptions or unstable connections cause partial SSE chunks to be received.
Ensure stable network connectivity and implement retry logic on streaming failures to recover from transient network issues.
Proxy or middleware buffers and splits SSE chunks improperly, breaking the stream format.
Configure proxies or middleware to disable buffering or chunk aggregation for SSE endpoints to preserve chunk integrity.
Client-side read timeout or premature stream closure truncates SSE chunks.
Increase client read timeout settings and ensure the stream is fully consumed before closing the connection.
Code: broken vs fixed
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
stream=True
)
for chunk in response: # This line triggers OpenAIStreamParseError
print(chunk) import os
from openai import OpenAI, OpenAIStreamParseError
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
stream=True
)
for chunk in response:
print(chunk)
except OpenAIStreamParseError:
print("Stream parse error: incomplete chunk received. Retrying...")
# Implement retry logic here Workaround
Catch OpenAIStreamParseError exceptions, buffer partial chunks manually, and attempt to reconstruct the full SSE event before parsing to avoid immediate failure.
Prevention
Use stable network connections, disable proxy buffering for SSE streams, and implement robust retry and backoff strategies to handle transient streaming parse errors.