How do I detect an error that happens in the middle of a stream?
When a streamed reply fails after it started, the HTTP status is already 200, so the API reports the failure inside the stream: one data: {"error": {...}} event, then data: [DONE].
What to do in your code#
- For every
data:line, parse the JSON and check for anerrorkey before readingchoices. - If you see one, mark the answer as incomplete. Don't save or show the partial text as final.
- Retry the whole request with backoff (
stream_error,model_overloadedandmodel_timeoutare retryable).
With the OpenAI SDKs#
The SDKs raise an exception while you iterate the stream when an error event arrives. Wrap the loop in try / catch and treat a raised error as an incomplete reply.
text = []
try:
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
text.append(chunk.choices[0].delta.content)
except Exception as e:
print("stream failed, retrying:", e)
A clean stream always ends with data: [DONE]; a stream that ends without it (a dropped connection) should be treated the same way.
Still need help?#
- General support: support@clatterlabs.com or team@clatterlabs.xyz
- Infrastructure questions, or permanent deletion of your data (your whole account or one service): infrastructure@clatterlabs.xyz
We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Related articles
Still need help?
Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyzFor deleting your whole account or the data of one service.