{"record":{"id":"fd305dfb25410213","repo":"unslothai/unsloth","slug":"chatgpt-returned-an-invalid-device-authorization-r","errorCode":null,"errorMessage":"ChatGPT returned an invalid device authorization response.","messagePattern":"ChatGPT returned an invalid device authorization response\\.","errorType":"exception","errorClass":"CodexAuthError","httpStatus":400,"severity":"error","filePath":"studio/backend/core/inference/openai_codex_auth.py","lineNumber":466,"sourceCode":"                    try:\n                        requested = float(server_interval)\n                    except (TypeError, ValueError):\n                        requested = flow.interval + 5\n                    flow.interval = min(30.0, max(flow.interval + 5, requested))\n                    continue\n                raise CodexAuthError(\n                    \"Device authorization failed. Enable device-code login in ChatGPT settings and retry.\"\n                )\n            body = response.json()\n            code = body.get(\"authorization_code\")\n            verifier = body.get(\"code_verifier\")\n            if (\n                not isinstance(code, str)\n                or not code\n                or not isinstance(verifier, str)\n                or not verifier\n            ):\n                raise CodexAuthError(\"ChatGPT returned an invalid device authorization response.\")\n            await _exchange_code(\n                flow,\n                code,\n                verifier = verifier,\n                redirect_uri = OPENAI_CODEX_DEVICE_REDIRECT_URI,\n            )\n            return\n        except asyncio.CancelledError:\n            return\n        except CodexAuthError as exc:\n            flow.status = \"error\"\n            flow.message = str(exc)\n\n            await _persist_terminal_flow(flow)\n            return\n        except Exception:\n            flow.status = \"error\"\n            flow.message = \"Device authorization failed. Please retry.\"","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/core/inference/openai_codex_auth.py#L448-L484","documentation":"Raised as CodexAuthError at the end of the device polling loop when the server returns success but the JSON body's 'authorization_code' or 'code_verifier' fields are missing, not strings, or empty. This is a response-shape validation: the device grant in this design returns a pre-traded authorization code plus its PKCE verifier, and both must be present and non-empty before _exchange_code is attempted.","triggerScenarios":"The device token endpoint returns 200 with a body lacking authorization_code or code_verifier, containing null for either, or returning them as non-string types; an upstream API contract change renames the fields.","commonSituations":"OpenAI changes the device-grant response schema (e.g. renaming to auth_code); partial proxy responses truncating JSON; server bugs during gradual rollouts; version mismatch between this library's expectations and the live endpoint.","solutions":["Retry the device flow once — transient malformed responses during rollouts do happen.","Upgrade the Studio/library version so the parser matches the current OpenAI device-grant response shape.","If persistent, capture the raw response body and compare field names against OpenAI's current codex device auth contract.","Use the browser flow as a working alternative while device auth is broken."],"exampleFix":"// before\ncode = body.get(\"authorization_code\")\nverifier = body.get(\"code_verifier\")\n\n// after\n# defensive read matching current contract, then explicit validation\ncode = body.get(\"authorization_code\") or body.get(\"auth_code\")\nverifier = body.get(\"code_verifier\") or body.get(\"verifier\")\nif not isinstance(code, str) or not code or not isinstance(verifier, str) or not verifier:\n    raise CodexAuthError(\"ChatGPT returned an invalid device authorization response.\")","handlingStrategy":"retry","validationCode":null,"typeGuard":"def is_device_response_error(exc: BaseException) -> bool:\n    return isinstance(exc, codex_auth.CodexAuthError) and \"invalid device authorization response\" in str(exc)","tryCatchPattern":"for attempt in range(2):\n    try:\n        return await run_device_flow(provider_id)\n    except codex_auth.CodexAuthError as exc:\n        if \"invalid device authorization response\" in str(exc) and attempt == 0:\n            continue  # transient schema/rollout hiccup; retry once\n        raise","preventionTips":["Keep the library version current with OpenAI's device-grant response schema.","Retry once on malformed success responses; treat persistence as a contract change.","Verify raw response bodies when integrating in test environments.","Keep the browser flow available as an alternative when device auth misbehaves."],"tags":["oauth","device-flow","json-validation","api-contract","codex"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}