Clatter LabsHelp Center

How do I detect an error that happens in the middle of a stream?

TroubleshootingUpdated

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#

  1. For every data: line, parse the JSON and check for an error key before reading choices.
  2. If you see one, mark the answer as incomplete. Don't save or show the partial text as final.
  3. Retry the whole request with backoff (stream_error, model_overloaded and model_timeout are 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?#

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.xyz
For deleting your whole account or the data of one service.