unslothai/unsloth · error · CodexTransportError

ChatGPT returned a malformed stream.

Error message

ChatGPT returned a malformed stream.

What it means

Raised while parsing the SSE stream from the Codex responses endpoint: a 'data:' line's payload failed json.loads or produced an event whose type could not be classified by response_event_type. The upstream contract (each data line is a JSON event) was violated mid-stream.

Source

Thrown at studio/backend/core/inference/openai_codex_client.py:577

                    async for line in response.aiter_lines():
                        if cancel_event is not None and cancel_event.is_set():
                            return
                        if not line:
                            event_name = ""
                            continue
                        if line.startswith("event:"):
                            event_name = line[6:].strip()
                            continue
                        if not line.startswith("data:"):
                            continue
                        raw = line[5:].strip()
                        if raw == "[DONE]":
                            break
                        try:
                            event = json.loads(raw)
                            kind = response_event_type(event, event_name)
                        except (ValueError, json.JSONDecodeError) as exc:
                            raise CodexTransportError(
                                "ChatGPT returned a malformed stream."
                            ) from exc
                        if kind in ("response.created", "response.in_progress"):
                            continue
                        if kind in ("response.output_text.delta", "response.refusal.delta"):
                            delta = event.get("delta")
                            if not isinstance(delta, str):
                                raise CodexTransportError("ChatGPT returned a malformed stream.")
                            yield _chunk(completion_id, model, {"content": delta})
                        elif kind in (
                            "response.reasoning_summary_text.delta",
                            "response.reasoning_text.delta",
                        ):
                            delta = event.get("delta")
                            if isinstance(delta, str):
                                yield _chunk(completion_id, model, {"reasoning_content": delta})
                        elif kind == "response.output_item.added":
                            item = event.get("item")

View on GitHub (pinned to 203007d190)

Solutions

  1. Retry the generation — single malformed events are usually transient
  2. Capture the raw line that failed (add logging of `raw` before the raise) to identify whether it is HTML/truncation
  3. If reproducible for a specific request, reduce response size or check proxy buffering settings
  4. Update the parser if OpenAI added a new event kind (version change)
Defensive patterns

Strategy: retry

Try / catch

try:
    collect(stream)
except CodexTransportError as exc:
    if 'malformed stream' in str(exc):
        result = collect(await client.stream(request))  # one retry, no partial reuse
    else:
        raise

Prevention

When it happens

Trigger: A data: line containing invalid JSON (truncated by a proxy, injected HTML error page, partial chunk), or a JSON body whose shape response_event_type cannot map to a known kind, or [DONE] not terminating cleanly.

Common situations: Intercepting proxy or buffer that truncates long SSE lines; upstream briefly emitting a malformed keep-alive; API contract change adding an event shape the parser does not recognize; proxy injecting a 502 HTML page into the stream.

Understand the failure class

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/94e2402dcf6b76c8. Report an issue: GitHub.