unslothai/unsloth · error · CodexTransportError

Could not refresh ChatGPT authorization. Please retry.

Error message

Could not refresh ChatGPT authorization. Please retry.

What it means

Raised when a 401 triggers a token refresh, but the refresh call fails with an unexpected (non-reauthorization) exception — typically a network failure or timeout hitting the token endpoint while the main endpoint was reachable. It is mapped to a 502-style transport error, signaling a transient infrastructure problem rather than bad credentials.

Source

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

                    yielded = True
                    yield response
                    return
                if 300 <= response.status_code < 400:
                    raise CodexTransportError(
                        "ChatGPT Codex endpoint returned a forbidden redirect."
                    )
                detail = await _upstream_error_detail(response)
                if response.status_code == 401 and refresh_access is not None and not refreshed:
                    try:
                        token, account_id = await refresh_access()
                    except CodexReauthorizationRequired as exc:
                        raise CodexReauthorizationError(
                            "ChatGPT authorization expired. Reconnect this connection.",
                            status = 401,
                            metadata = {"access_token": token},
                        ) from exc
                    except Exception as exc:
                        raise CodexTransportError(
                            "Could not refresh ChatGPT authorization. Please retry.",
                            status = 502,
                        ) from exc
                    headers["Authorization"] = f"Bearer {token}"
                    headers["chatgpt-account-id"] = account_id
                    refreshed = True
                    continue
                if response.status_code == 401:
                    raise CodexReauthorizationError(
                        "ChatGPT authorization expired. Reconnect this connection.",
                        status = 401,
                        metadata = {"access_token": token},
                    )
                retryable = response.status_code in _RETRYABLE_STATUSES and not _is_terminal_quota(
                    detail
                )
                if retryable and attempt < _MAX_TRANSIENT_RETRIES:
                    await _retry_pause(_retry_delay_seconds(response, attempt), cancel_event)

View on GitHub (pinned to 203007d190)

Solutions

  1. Retry the original request after a short backoff — the refresh failed transiently, credentials are likely still valid
  2. Check connectivity/latency to the OAuth token endpoint specifically
  3. If persistent, inspect the chained exception (raise ... from exc) for the underlying network cause

Example fix

# before
resp = await client.stream(req)  # single shot, fails hard on transient refresh error

# after
for attempt in range(3):
    try:
        resp = await client.stream(req)
        break
    except CodexTransportError as exc:
        if exc.status != 502 or attempt == 2:
            raise
        await asyncio.sleep(2 ** attempt)
Defensive patterns

Strategy: retry

Try / catch

for attempt in range(3):
    try:
        return await client.stream(request)
    except CodexTransportError as exc:
        if exc.status != 502 or attempt == 2:
            raise
        await asyncio.sleep(2 ** attempt)

Prevention

When it happens

Trigger: 401 from the Codex endpoint; refresh_access() is called and raises anything other than CodexReauthorizationRequired (httpx connect error, DNS failure, 5xx from the token endpoint, timeout).

Common situations: Flaky network where the main request got a 401 (stale token) but the token endpoint request then timed out; token endpoint briefly down or rate-limiting; proxy intermittently dropping connections to auth.openai.com.

Related errors


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