BerriAI/litellm · error · XAIOAuthError

xAI OAuth token request failed

Error message

xAI OAuth token request failed: {exc.response.status_code} {exc.response.text}

What it means

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.

Solutions

  1. Check the status code and response in the error (e.g. invalid_grant) and restart the login flow if the code expired.
  2. Verify the client configuration and that the authorization code is exchanged only once.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/xai/oauth.py:319 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/4eddf7fed362e26b. Report an issue: GitHub.

Appendix: source

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

                if server.callback_result is not None:
                    return server.callback_result
        finally:
            server.server_close()
        raise XAIOAuthError("Timed out waiting for xAI OAuth callback")

    def _exchange_token(self, token_endpoint: str, data: dict[str, str]) -> dict[str, Any]:
        try:
            response: Final = self._client().post(
                token_endpoint,
                headers={
                    "Accept": "application/json",
                    "Content-Type": "application/x-www-form-urlencoded",
                },
                data=data,
            )
            response.raise_for_status()
        except httpx.HTTPStatusError as exc:
            raise XAIOAuthError(
                f"xAI OAuth token request failed: {exc.response.status_code} {exc.response.text}"
            ) from exc
        try:
            body: Final = response.json()
        except ValueError as exc:
            raise XAIOAuthError("xAI OAuth token response was not valid JSON") from exc
        if not isinstance(body, dict):
            raise XAIOAuthError("xAI OAuth token response was not an object")
        return body

    def _build_auth_record(
        self,
        token_payload: dict[str, Any],
        token_endpoint: str,
        fallback_refresh_token: str | None = None,
    ) -> dict[str, Any]:
        access_token: Final = token_payload.get("access_token")
        refresh_token: Final = token_payload.get("refresh_token") or fallback_refresh_token

View on GitHub (pinned to 77b7c6c40c)