BerriAI/litellm · error · XAIOAuthError

xAI OAuth discovery response was not valid JSON

Error message

xAI OAuth discovery response was not valid JSON

What it means

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.

Source

Thrown at litellm/llms/xai/oauth.py:239

        if expires_at is None:
            return True
        try:
            return time.time() >= float(expires_at) - XAI_OAUTH_EXPIRY_SKEW_SECONDS
        except (TypeError, ValueError):
            return True

    def _discover(self) -> dict[str, str]:
        try:
            response: Final = self._client().get(XAI_OAUTH_DISCOVERY_URL, headers={"Accept": "application/json"})
            response.raise_for_status()
        except httpx.HTTPStatusError as exc:
            raise XAIOAuthError(
                f"xAI OAuth discovery request failed: {exc.response.status_code} {exc.response.text}"
            ) from exc
        try:
            data: Final = response.json()
        except ValueError as exc:
            raise XAIOAuthError("xAI OAuth discovery response was not valid JSON") from exc
        authorization_endpoint: Final = data.get("authorization_endpoint")
        token_endpoint: Final = data.get("token_endpoint")
        if not authorization_endpoint or not token_endpoint:
            raise XAIOAuthError("xAI OAuth discovery missing endpoints")
        return {
            "authorization_endpoint": self._validate_xai_endpoint(authorization_endpoint),
            "token_endpoint": self._validate_xai_endpoint(token_endpoint),
        }

    def _validate_xai_endpoint(self, url: str) -> str:
        parsed: Final = urlparse(url)
        host: Final = (parsed.hostname or "").lower()
        if parsed.scheme != "https" or (host != "x.ai" and not host.endswith(".x.ai")):
            raise XAIOAuthError(f"xAI OAuth discovery returned unexpected endpoint: {url}")
        return url

    def _pkce_pair(self) -> tuple[str, str]:
        verifier: Final = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Retry the login; the discovery endpoint returned non-JSON, indicating a transient or endpoint problem.
  2. Check for proxies or firewalls modifying the response from the xAI discovery URL.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/xai/oauth.py:239 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/58c708b6324e6eec. Report an issue: GitHub.