BerriAI/litellm · error · XAIOAuthError

Could not start xAI OAuth callback server: {last_error}

Error message

Could not start xAI OAuth callback server: {last_error}

What it means

XAIOAuthError raised after both attempts to bind the loopback OAuth callback HTTPServer (fixed port and ephemeral 0) failed with OSError: there is no redirect_uri to embed in the authorization URL, so login cannot start.

Source

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

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

    def _start_callback_server(self, state: str) -> tuple[_CallbackServer, str]:
        last_error: OSError | None = None
        for port in (XAI_OAUTH_REDIRECT_PORT, 0):
            try:
                server = _CallbackServer((XAI_OAUTH_REDIRECT_HOST, port), _CallbackHandler)
                server.expected_state = state
                server.callback_result = None
                actual_port = server.server_address[1]
                redirect_uri = f"http://{XAI_OAUTH_REDIRECT_HOST}:{actual_port}{XAI_OAUTH_REDIRECT_PATH}"
                return server, redirect_uri
            except OSError as exc:
                last_error = exc
        raise XAIOAuthError(f"Could not start xAI OAuth callback server: {last_error}")

    def _build_authorize_url(
        self,
        authorization_endpoint: str,
        redirect_uri: str,
        challenge: str,
        state: str,
        nonce: str,
    ) -> str:
        params: Final = {
            "response_type": "code",
            "client_id": XAI_OAUTH_CLIENT_ID,
            "redirect_uri": redirect_uri,
            "scope": XAI_OAUTH_SCOPE,
            "code_challenge": challenge,
            "code_challenge_method": "S256",
            "state": state,
            "nonce": nonce,

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Free up the callback port or configure a different port; the error shows why the local server could not bind.
  2. Check for firewall restrictions or another process already listening on the OAuth callback port.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/xai/oauth.py:273 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/74dedffeb6fcacc5. Report an issue: GitHub.