BerriAI/litellm · error · XAIOAuthError
xAI OAuth token response was not an object
Error message
xAI OAuth token response was not an object
What it means
Raised in _exchange_token after the token endpoint returned 2xx and the body parsed as JSON, but the parsed value is not a JSON object (e.g. a bare string, number, or list). A compliant OAuth token endpoint must return an object with access_token and friends, so this guards against misconfigured proxies or gateways returning malformed token payloads.
Source
Thrown at litellm/llms/xai/oauth.py:327
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:
expires_at = int(time.time() + int(expires_in))
except (TypeError, ValueError):View on GitHub (pinned to 77b7c6c40c)
Solutions
- Retry the OAuth flow; the token endpoint returned a JSON value that was not an object, indicating an upstream anomaly.
- Check the raw token response with verbose logging to diagnose the endpoint behavior.
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at litellm/llms/xai/oauth.py:327 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/565fec25d60aa55a.
Report an issue: GitHub.