{"record":{"id":"cb18aea34a545475","repo":"BerriAI/litellm","slug":"xai-authorization-failed-no-code-returned","errorCode":null,"errorMessage":"xAI authorization failed: no code returned","messagePattern":"xAI authorization failed: no code returned","errorType":"exception","errorClass":"XAIOAuthError","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/oauth.py","lineNumber":154,"sourceCode":"            redirect_uri=redirect_uri,\n            challenge=challenge,\n            state=state,\n            nonce=nonce,\n        )\n\n        if no_browser or not webbrowser.open(authorize_url):\n            sys.stdout.write(f\"Open this URL to authenticate with xAI:\\n{authorize_url}\\n\")\n            sys.stdout.flush()\n\n        result: Final = self._wait_for_callback(server)\n        if result.get(\"state\") != state:\n            raise XAIOAuthError(\"xAI OAuth state mismatch\")\n        if result.get(\"error\"):\n            description: Final = result.get(\"error_description\") or result[\"error\"]\n            raise XAIOAuthError(f\"xAI authorization failed: {description}\")\n        code: Final = result.get(\"code\")\n        if not code:\n            raise XAIOAuthError(\"xAI authorization failed: no code returned\")\n\n        token_payload: Final = self._exchange_token(\n            discovery[\"token_endpoint\"],\n            {\n                \"grant_type\": \"authorization_code\",\n                \"code\": code,\n                \"redirect_uri\": redirect_uri,\n                \"client_id\": XAI_OAUTH_CLIENT_ID,\n                \"code_verifier\": verifier,\n            },\n        )\n        auth_data: Final = self._build_auth_record(token_payload, discovery[\"token_endpoint\"])\n        self._write_auth_file(auth_data)\n        return auth_data\n\n    def _client(self) -> httpx.Client | HTTPHandler:\n        return self.http_client or _get_httpx_client()\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/oauth.py#L136-L172","documentation":"XAIOAuthError raised when the OAuth callback completed without an authorization code: state matched but no 'code' came back, so there is nothing to exchange at the token endpoint and login must be retried.","triggerScenarios":"Thrown at litellm/llms/xai/oauth.py:154 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Retry the OAuth login; the authorization response returned no code, usually due to a denied or incomplete consent.","Verify the redirect URI configuration and that the browser flow completed successfully."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}