{"record":{"id":"17bf93240b1002ae","repo":"BerriAI/litellm","slug":"xai-oauth-token-response-was-not-valid-json","errorCode":null,"errorMessage":"xAI OAuth token response was not valid JSON","messagePattern":"xAI OAuth token response was not valid JSON","errorType":"exception","errorClass":"XAIOAuthError","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/oauth.py","lineNumber":325,"sourceCode":"    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\n        if not access_token:\n            raise XAIOAuthError(\"xAI OAuth token response missing access_token\")\n        if not refresh_token:\n            raise XAIOAuthError(\"xAI OAuth token response missing refresh_token\")\n        expires_in: Final = token_payload.get(\"expires_in\") or 3600\n        try:","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/oauth.py#L307-L343","documentation":"XAIOAuthError raised when the token-endpoint response body is not valid JSON (ValueError from response.json): the exchange may have succeeded at the transport level, but the token payload cannot be read.","triggerScenarios":"Thrown at litellm/llms/xai/oauth.py:325 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Retry the login; the token endpoint returned non-JSON, indicating a transient or endpoint issue.","Check for proxies altering the response from the xAI token endpoint."],"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"}