{"record":{"id":"4eddf7fed362e26b","repo":"BerriAI/litellm","slug":"xai-oauth-token-request-failed-exc-response-stat","errorCode":null,"errorMessage":"xAI OAuth token request failed: {exc.response.status_code} {exc.response.text}","messagePattern":"xAI OAuth token request failed: (.+?) (.+?)","errorType":"exception","errorClass":"XAIOAuthError","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/oauth.py","lineNumber":319,"sourceCode":"                if server.callback_result is not None:\n                    return server.callback_result\n        finally:\n            server.server_close()\n        raise XAIOAuthError(\"Timed out waiting for xAI OAuth callback\")\n\n    def _exchange_token(self, token_endpoint: str, data: dict[str, str]) -> dict[str, Any]:\n        try:\n            response: Final = self._client().post(\n                token_endpoint,\n                headers={\n                    \"Accept\": \"application/json\",\n                    \"Content-Type\": \"application/x-www-form-urlencoded\",\n                },\n                data=data,\n            )\n            response.raise_for_status()\n        except httpx.HTTPStatusError as exc:\n            raise XAIOAuthError(\n                f\"xAI OAuth token request failed: {exc.response.status_code} {exc.response.text}\"\n            ) from exc\n        try:\n            body: Final = response.json()\n        except ValueError as exc:\n            raise XAIOAuthError(\"xAI OAuth token response was not valid JSON\") from exc\n        if not isinstance(body, dict):\n            raise XAIOAuthError(\"xAI OAuth token response was not an object\")\n        return body\n\n    def _build_auth_record(\n        self,\n        token_payload: dict[str, Any],\n        token_endpoint: str,\n        fallback_refresh_token: str | None = None,\n    ) -> dict[str, Any]:\n        access_token: Final = token_payload.get(\"access_token\")\n        refresh_token: Final = token_payload.get(\"refresh_token\") or fallback_refresh_token","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/oauth.py#L301-L337","documentation":"Raised when the xAI OAuth token exchange POST fails with an HTTP status error; status and body are embedded in XAIOAuthError. Fires on invalid client credentials, bad grant, or wrong token endpoint.","triggerScenarios":"Thrown at litellm/llms/xai/oauth.py:319 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check the status code and response in the error (e.g. invalid_grant) and restart the login flow if the code expired.","Verify the client configuration and that the authorization code is exchanged only once."],"exampleFix":null,"handlingStrategy":"try-catch","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-14T05:17:10.506Z"}