{"record":{"id":"578f2ec31e2e8457","repo":"unslothai/unsloth","slug":"chatgpt-authorization-expired-reconnect-this-conn","errorCode":null,"errorMessage":"ChatGPT authorization expired. Reconnect this connection.","messagePattern":"ChatGPT authorization expired\\. Reconnect this connection\\.","errorType":"exception","errorClass":"CodexReauthorizationError","httpStatus":401,"severity":"critical","filePath":"studio/backend/core/inference/openai_codex_client.py","lineNumber":370,"sourceCode":"                cancel_event = cancel_event,\n            ) as response:\n                if response is None:\n                    yield None\n                    return\n                if 200 <= response.status_code < 300:\n                    yielded = True\n                    yield response\n                    return\n                if 300 <= response.status_code < 400:\n                    raise CodexTransportError(\n                        \"ChatGPT Codex endpoint returned a forbidden redirect.\"\n                    )\n                detail = await _upstream_error_detail(response)\n                if response.status_code == 401 and refresh_access is not None and not refreshed:\n                    try:\n                        token, account_id = await refresh_access()\n                    except CodexReauthorizationRequired as exc:\n                        raise CodexReauthorizationError(\n                            \"ChatGPT authorization expired. Reconnect this connection.\",\n                            status = 401,\n                            metadata = {\"access_token\": token},\n                        ) from exc\n                    except Exception as exc:\n                        raise CodexTransportError(\n                            \"Could not refresh ChatGPT authorization. Please retry.\",\n                            status = 502,\n                        ) from exc\n                    headers[\"Authorization\"] = f\"Bearer {token}\"\n                    headers[\"chatgpt-account-id\"] = account_id\n                    refreshed = True\n                    continue\n                if response.status_code == 401:\n                    raise CodexReauthorizationError(\n                        \"ChatGPT authorization expired. Reconnect this connection.\",\n                        status = 401,\n                        metadata = {\"access_token\": token},","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/core/inference/openai_codex_client.py#L352-L388","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Stop retrying — this is terminal; surface a 'Reconnect ChatGPT' action to the user","Re-run the OAuth connection flow to obtain a fresh access+refresh token pair","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"],"exampleFix":"// before\nwhile (true) { try { await client.stream(req); break; } catch (e) { await sleep(1000); } } // infinite loop on 401\n\n// after\ntry {\n  await client.stream(req);\n} catch (e) {\n  if (e instanceof CodexReauthorizationError) {\n    await showReconnectPrompt(providerId); // one-shot, no retry\n    return;\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    async for chunk in client.stream(request):\n        ...\nexcept CodexReauthorizationError:\n    show_reconnect_prompt(provider_id)  # terminal: no retry\n    disable_codex_until_reconnected(provider_id)","preventionTips":["Check bundle.get('reauthorization_required') before starting requests and prompt reconnect proactively","Give each installation its own OAuth connection — do not copy bundles between machines","Track refresh failures per connection and pre-emptively flag connections whose refresh token keeps getting revoked"],"tags":["auth","oauth","codex","token-refresh"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}