{"record":{"id":"7f4d526b59b261c9","repo":"BerriAI/litellm","slug":"xai-api-base-or-key-is-not-set-set-xai-api-base-a","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/common_utils.py","lineNumber":74,"sourceCode":"        litellm.api_key fallback. Responses and realtime historically\n        preferred litellm.api_key over XAI_API_KEY, so those paths opt into\n        the legacy order with legacy_generic_before_env=True. In both modes,\n        the provider-specific litellm.xai_key takes precedence over fallbacks.\n        \"\"\"\n        if legacy_generic_before_env:\n            return api_key or litellm.xai_key or litellm.api_key or get_secret_str(\"XAI_API_KEY\")\n\n        return api_key or litellm.xai_key or get_secret_str(\"XAI_API_KEY\")\n\n    @staticmethod\n    def get_base_model(model: str) -> str | None:\n        return model.replace(\"xai/\", \"\")\n\n    def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]:\n        api_base = self.get_api_base(api_base)\n        api_key = self.get_api_key(api_key)\n        if api_base is None or api_key is None:\n            raise ValueError(\n                \"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.\"\n            )\n        response: Final = litellm.module_level_client.get(\n            url=f\"{api_base}/v1/models\",\n            headers={\"Authorization\": f\"Bearer {api_key}\"},\n        )\n\n        try:\n            response.raise_for_status()\n        except httpx.HTTPStatusError:\n            raise Exception(\n                f\"Failed to fetch models from XAI. Status code: {response.status_code}, Response: {response.text}\"\n            )\n\n        models: Final = response.json()[\"data\"]\n\n        litellm_model_names: Final = []\n        for model in models:","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/common_utils.py#L56-L92","documentation":"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.","triggerScenarios":"Thrown at litellm/llms/xai/common_utils.py:74 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Set XAI_API_BASE and provide an API key via api_key, litellm.xai_key, or the XAI_API_KEY environment variable.","Check that the environment variables are loaded in the process running litellm."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}