{"record":{"id":"58c708b6324e6eec","repo":"BerriAI/litellm","slug":"xai-oauth-discovery-response-was-not-valid-json","errorCode":null,"errorMessage":"xAI OAuth discovery response was not valid JSON","messagePattern":"xAI OAuth discovery response was not valid JSON","errorType":"exception","errorClass":"XAIOAuthError","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/oauth.py","lineNumber":239,"sourceCode":"        if expires_at is None:\n            return True\n        try:\n            return time.time() >= float(expires_at) - XAI_OAUTH_EXPIRY_SKEW_SECONDS\n        except (TypeError, ValueError):\n            return True\n\n    def _discover(self) -> dict[str, str]:\n        try:\n            response: Final = self._client().get(XAI_OAUTH_DISCOVERY_URL, headers={\"Accept\": \"application/json\"})\n            response.raise_for_status()\n        except httpx.HTTPStatusError as exc:\n            raise XAIOAuthError(\n                f\"xAI OAuth discovery request failed: {exc.response.status_code} {exc.response.text}\"\n            ) from exc\n        try:\n            data: Final = response.json()\n        except ValueError as exc:\n            raise XAIOAuthError(\"xAI OAuth discovery response was not valid JSON\") from exc\n        authorization_endpoint: Final = data.get(\"authorization_endpoint\")\n        token_endpoint: Final = data.get(\"token_endpoint\")\n        if not authorization_endpoint or not token_endpoint:\n            raise XAIOAuthError(\"xAI OAuth discovery missing endpoints\")\n        return {\n            \"authorization_endpoint\": self._validate_xai_endpoint(authorization_endpoint),\n            \"token_endpoint\": self._validate_xai_endpoint(token_endpoint),\n        }\n\n    def _validate_xai_endpoint(self, url: str) -> str:\n        parsed: Final = urlparse(url)\n        host: Final = (parsed.hostname or \"\").lower()\n        if parsed.scheme != \"https\" or (host != \"x.ai\" and not host.endswith(\".x.ai\")):\n            raise XAIOAuthError(f\"xAI OAuth discovery returned unexpected endpoint: {url}\")\n        return url\n\n    def _pkce_pair(self) -> tuple[str, str]:\n        verifier: Final = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b\"=\").decode()","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/oauth.py#L221-L257","documentation":"XAIOAuthError raised when the discovery response body fails JSON parsing (ValueError from response.json): the document at the discovery URL is not valid JSON, so authorization/token endpoints cannot be extracted.","triggerScenarios":"Thrown at litellm/llms/xai/oauth.py:239 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Retry the login; the discovery endpoint returned non-JSON, indicating a transient or endpoint problem.","Check for proxies or firewalls modifying the response from the xAI discovery URL."],"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"}