BerriAI/litellm · error · XAIOAuthError
xAI OAuth token response was not valid JSON
Error message
xAI OAuth token response was not valid JSON
What it means
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.
Source
Thrown at litellm/llms/xai/oauth.py:325
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
if not access_token:
raise XAIOAuthError("xAI OAuth token response missing access_token")
if not refresh_token:
raise XAIOAuthError("xAI OAuth token response missing refresh_token")
expires_in: Final = token_payload.get("expires_in") or 3600
try:View on GitHub (pinned to 77b7c6c40c)
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.
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at litellm/llms/xai/oauth.py:325 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/17bf93240b1002ae.
Report an issue: GitHub.