BerriAI/litellm · error · ValueError

XAI API base or key is not set. Set XAI_API_BASE and provide

Error message

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.

What it means

Raised by get_models (and similar callers) when no xAI API base or key resolved via api_base/api_key params, litellm.xai_key, XAI_API_BASE/XAI_API_KEY env, or fallbacks — the /v1/models listing request would have nowhere to go or be unauthenticated.

Source

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

        litellm.api_key fallback. Responses and realtime historically
        preferred litellm.api_key over XAI_API_KEY, so those paths opt into
        the legacy order with legacy_generic_before_env=True. In both modes,
        the provider-specific litellm.xai_key takes precedence over fallbacks.
        """
        if legacy_generic_before_env:
            return api_key or litellm.xai_key or litellm.api_key or get_secret_str("XAI_API_KEY")

        return api_key or litellm.xai_key or get_secret_str("XAI_API_KEY")

    @staticmethod
    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:

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Set XAI_API_BASE and provide an API key via api_key, litellm.xai_key, or the XAI_API_KEY environment variable.
  2. Check that the environment variables are loaded in the process running litellm.
Defensive patterns

Strategy: validation

When it happens

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