unslothai/unsloth · critical · CodexReauthorizationError

ChatGPT authorization expired. Reconnect this connection.

Error message

ChatGPT authorization expired. Reconnect this connection.

What it means

Raised when the Codex endpoint returns 401, the client attempts a token refresh via the refresh_access callback, and that callback raises CodexReauthorizationRequired — i.e. the stored refresh token was revoked, expired, or the backend flagged the bundle as requiring reauthorization. This is a terminal auth state: refreshing cannot succeed and the user must reconnect the ChatGPT connection.

Source

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

                cancel_event = cancel_event,
            ) as response:
                if response is None:
                    yield None
                    return
                if 200 <= response.status_code < 300:
                    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},

View on GitHub (pinned to 203007d190)

Solutions

  1. Stop retrying — this is terminal; surface a 'Reconnect ChatGPT' action to the user
  2. Re-run the OAuth connection flow to obtain a fresh access+refresh token pair
  3. If it recurs immediately after reconnecting, check whether multiple installations share one OAuth bundle and are racing refresh-token rotation; give each installation its own connection

Example fix

// before
while (true) { try { await client.stream(req); break; } catch (e) { await sleep(1000); } } // infinite loop on 401

// after
try {
  await client.stream(req);
} catch (e) {
  if (e instanceof CodexReauthorizationError) {
    await showReconnectPrompt(providerId); // one-shot, no retry
    return;
  }
  throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try:
    async for chunk in client.stream(request):
        ...
except CodexReauthorizationError:
    show_reconnect_prompt(provider_id)  # terminal: no retry
    disable_codex_until_reconnected(provider_id)

Prevention

When it happens

Trigger: 401 from the responses endpoint while refreshed is False, and refresh_access() raises CodexReauthorizationRequired (refresh token invalid/rotated, or bundle marked reauthorization_required). The error is raised with status 401 and carries the access token in metadata.

Common situations: User revoked the application authorization at OpenAI; refresh token rotation mismatch after restoring a DB backup to another machine; the account password changed or sessions were invalidated; long idle period past refresh-token lifetime.

Related errors


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