{"record":{"id":"5d3ecd1225611d42","repo":"BerriAI/litellm","slug":"failed-to-fetch-models-from-xai-status-code-res","errorCode":null,"errorMessage":"Failed to fetch models from XAI. Status code: {response.status_code}, Response: {response.text}","messagePattern":"Failed to fetch models from XAI\\. Status code: (.+?), Response: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/xai/common_utils.py","lineNumber":85,"sourceCode":"    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:\n            stripped_model_name = model[\"id\"]\n            litellm_model_name = \"xai/\" + stripped_model_name\n            litellm_model_names.append(litellm_model_name)\n        return litellm_model_names\n","sourceCodeStart":67,"sourceCodeEnd":97,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/xai/common_utils.py#L67-L97","documentation":"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.","triggerScenarios":"Thrown at litellm/llms/xai/common_utils.py:85 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check the status code and response body in the error for the cause (invalid key, network, endpoint change).","Verify XAI_API_BASE points to the correct /models endpoint and the API key is valid."],"exampleFix":null,"handlingStrategy":"try-catch","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-14T00:17:10.932Z"}