{"record":{"id":"9c1dafde919d9914","repo":"BerriAI/litellm","slug":"failed-to-fetch-models-from-gemini-status-code","errorCode":null,"errorMessage":"Failed to fetch models from Gemini. Status code: {response.status_code}, Response: {response.json()}","messagePattern":"Failed to fetch models from Gemini\\. Status code: (.+?), Response: (.+?)","errorType":"http","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/gemini/common_utils.py","lineNumber":397,"sourceCode":"            litellm_model_names.append(litellm_model_name)\n        return litellm_model_names\n\n    def get_models(self, api_key: str | None = None, api_base: str | None = None) -> list[str]:\n        api_base = GeminiModelInfo.get_api_base(api_base)\n        api_key = GeminiModelInfo.get_api_key(api_key)\n        endpoint: Final = f\"/{self.api_version}/models\"\n        if api_base is None or api_key is None:\n            raise ValueError(\n                \"GEMINI_API_BASE or GEMINI_API_KEY/GOOGLE_API_KEY is not set. Please set the environment variable, to query Gemini's `/models` endpoint.\"\n            )\n\n        response: Final = litellm.module_level_client.get(\n            url=f\"{api_base}{endpoint}\",\n            headers={\"x-goog-api-key\": api_key},\n        )\n\n        if response.status_code != 200:\n            raise ValueError(\n                f\"Failed to fetch models from Gemini. Status code: {response.status_code}, Response: {response.json()}\"\n            )\n\n        models: Final = response.json()[\"models\"]\n\n        litellm_model_names: Final = self.process_model_name(models)\n        return litellm_model_names\n\n    def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:\n        return GeminiError(status_code=status_code, message=error_message, headers=headers)\n\n    def get_token_counter(self) -> BaseTokenCounter | None:\n        \"\"\"\n        Factory method to create a token counter for this provider.\n\n        Returns:\n            Optional TokenCounterInterface implementation for this provider,\n            or None if token counting is not supported.","sourceCodeStart":379,"sourceCodeEnd":415,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/gemini/common_utils.py#L379-L415","documentation":"After successfully authenticating, GeminiModelInfo.get_models() GETs {api_base}/{api_version}/models with the x-goog-api-key header. Any non-200 response (401/403 invalid key, 429 quota, 5xx outage, or a wrong GEMINI_API_BASE) raises this ValueError embedding the status code and Google's JSON error body.","triggerScenarios":"Expired/revoked AI Studio key; GEMINI_API_BASE pointing at a wrong host or an API version the key can't use; quota exhausted; transient Google 5xx during model discovery.","commonSituations":"Model-refresh cron jobs failing overnight after quota changes; rotated key not yet updated everywhere; self-hosted proxy used as GEMINI_API_BASE that returns an unexpected status.","solutions":["Parse the status/body in the message: 401/403 -> fix the key; 429 -> back off or raise quota; 5xx -> retry later.","curl-verify the pair: curl -H \"x-goog-api-key: $GOOGLE_API_KEY\" https://generativelanguage.googleapis.com/v1beta/models.","Unset or correct GEMINI_API_BASE if it points anywhere other than Google's endpoint."],"exampleFix":"# before\nGEMINI_API_BASE=\"https://wrong-host.example.com\"\nmodels = litellm.get_model_list(\"gemini\")\n\n# after\nGEMINI_API_BASE=\"https://generativelanguage.googleapis.com\"\nmodels = litellm.get_model_list(\"gemini\")","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"import re, time\n\ndef list_gemini_models():\n    for attempt in range(4):\n        try:\n            return litellm.get_model_list(\"gemini\")\n        except ValueError as e:\n            if re.search(r\"Status code: (429|5\\\\d\\\\d)\", str(e)):\n                time.sleep(2 ** attempt)\n                continue\n            raise\n    raise RuntimeError(\"Gemini /models unavailable\")","preventionTips":["Cache the discovered model list; don't re-query /models per request.","Verify GEMINI_API_BASE points at Google's host unless you intentionally proxy.","Monitor for 401 after key rotation — stale keys fail here first."],"tags":["gemini","http","model-listing","api-errors"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}