BerriAI/litellm · error · Exception

Failed to fetch models from XAI. Status code: {response.stat

Error message

Failed to fetch models from XAI. Status code: {response.status_code}, Response: {response.text}

What it means

Raised after litellm.module_level_client.get() hits the xAI /models endpoint and the response comes back with a non-2xx status. The status code and raw response body are interpolated into the message. Typical causes: an invalid or expired XAI_API_KEY (401), or a wrong XAI_API_BASE pointing somewhere that is not the xAI API.

Source

Thrown at litellm/llms/xai/common_utils.py:85

    def get_base_model(model: str) -> str | None:
        return model.replace("xai/", "")

    def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]:
        api_base = self.get_api_base(api_base)
        api_key = self.get_api_key(api_key)
        if api_base is None or api_key is None:
            raise ValueError(
                "XAI API base or key is not set. Set XAI_API_BASE and provide an xAI API key via api_key, litellm.xai_key, or XAI_API_KEY."
            )
        response: Final = litellm.module_level_client.get(
            url=f"{api_base}/v1/models",
            headers={"Authorization": f"Bearer {api_key}"},
        )

        try:
            response.raise_for_status()
        except httpx.HTTPStatusError:
            raise Exception(
                f"Failed to fetch models from XAI. Status code: {response.status_code}, Response: {response.text}"
            )

        models: Final = response.json()["data"]

        litellm_model_names: Final = []
        for model in models:
            stripped_model_name = model["id"]
            litellm_model_name = "xai/" + stripped_model_name
            litellm_model_names.append(litellm_model_name)
        return litellm_model_names

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Check the status code and response body in the error for the cause (invalid key, network, endpoint change).
  2. Verify XAI_API_BASE points to the correct /models endpoint and the API key is valid.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/xai/common_utils.py:85 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/5d3ecd1225611d42. Report an issue: GitHub.