unslothai/unsloth · error · CodexTransportError

ChatGPT stream ended before completion.

Error message

ChatGPT stream ended before completion.

What it means

After the SSE loop finished, no terminal success event (response.completed with usage) was emitted and the stream was not user-cancelled — the connection closed mid-generation without a proper terminator. This guards against silently truncated answers: partial text was yielded but completion never confirmed.

Source

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

                        elif kind in ("response.failed", "error"):
                            error = (
                                event.get("error")
                                or (event.get("response") or {}).get("error")
                                or {}
                            )
                            code = error.get("code") if isinstance(error, dict) else None
                            raise CodexTransportError(
                                f"ChatGPT Codex generation failed{f' ({code})' if code else ''}."
                            )
                except httpx.HTTPError:
                    if cancel_event is not None and cancel_event.is_set():
                        return
                    raise
        finally:
            if cancel_task is not None:
                cancel_task.cancel()
        if not emitted_terminal and not (cancel_event is not None and cancel_event.is_set()):
            raise CodexTransportError("ChatGPT stream ended before completion.")

View on GitHub (pinned to 203007d190)

Solutions

  1. Retry the generation — the request is idempotent at the prompt level and most truncations are transient
  2. Raise stream/read timeouts and proxy idle timeouts for long generations
  3. If using tool deltas, keep the stream active; avoid long silent gaps that trigger LB idle kills
Defensive patterns

Strategy: retry

Try / catch

try:
    collect(stream)
except CodexTransportError as exc:
    if 'ended before completion' in str(exc):
        return await retry_generation_once(request)  # truncated: discard partials
    raise

Prevention

When it happens

Trigger: Upstream or an intermediary closes the TCP stream after deltas but before response.completed; emitted_terminal stays False and cancel_event is unset, so the guard raises. Any dropped connection during generation.

Common situations: Idle-timeout or byte-timeout on a proxy/load balancer cutting long streams; mobile/flaky networks; upstream connection resets; keep-alive gaps exceeding intermediary limits.

Related errors


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