{"record":{"id":"db7eb74e3fdb74c4","repo":"BerriAI/litellm","slug":"failed-to-get-models-response-text","errorCode":null,"errorMessage":"Failed to get models: {response.text}","messagePattern":"Failed to get models: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/openai/chat/gpt_transformation.py","lineNumber":711,"sourceCode":"\n        if api_base is None:\n            api_base = \"https://api.openai.com\"\n        if api_key is None:\n            api_key = get_secret_str(\"OPENAI_API_KEY\")\n\n        # Strip api_base to just the base URL (scheme + host + port)\n        parsed_url: Final = httpx.URL(api_base)\n        base_url = f\"{parsed_url.scheme}://{parsed_url.host}\"\n        if parsed_url.port:\n            base_url += f\":{parsed_url.port}\"\n\n        response: Final = litellm.module_level_client.get(\n            url=f\"{base_url}/v1/models\",\n            headers={\"Authorization\": f\"Bearer {api_key}\"},\n        )\n\n        if response.status_code != 200:\n            raise Exception(f\"Failed to get models: {response.text}\")\n\n        models: Final = response.json()[\"data\"]\n        return [model[\"id\"] for model in models]\n\n    @staticmethod\n    def get_api_key(api_key: str | None = None) -> str | None:\n        return api_key or litellm.api_key or litellm.openai_key or get_secret_str(\"OPENAI_API_KEY\")\n\n    @staticmethod\n    def get_api_base(api_base: str | None = None) -> str | None:\n        return (\n            api_base\n            or litellm.api_base\n            or get_secret_str(\"OPENAI_BASE_URL\")\n            or get_secret_str(\"OPENAI_API_BASE\")\n            or \"https://api.openai.com/v1\"\n        )\n","sourceCodeStart":693,"sourceCodeEnd":729,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/openai/chat/gpt_transformation.py#L693-L729","documentation":"The OpenAI transformation's model-listing helper GETs {base_url}/v1/models with a Bearer token. Any non-200 raises a generic Exception with the response body. This surfaces auth failures, wrong URLs, and servers that don't implement /v1/models.","triggerScenarios":"Calling the helper (e.g. get_models) with an invalid API key (401), an api_base whose host doesn't serve /v1/models (404), or rate-limited/keyless access (429/403). The api_base is stripped to scheme://host[:port], so path components in api_base are discarded.","commonSituations":"Using an OpenAI-compatible server without a /v1/models route, expired OPENAI_API_KEY, api_base with a path prefix that gets stripped, or firewall rules blocking the models endpoint specifically.","solutions":["curl -H \"Authorization: Bearer $KEY\" {base}/v1/models to see the exact status/body returned.","Verify the API key is set and valid (OPENAI_API_KEY env var or api_key arg).","If the server lacks /v1/models, list models another way (provider docs) instead of relying on this helper.","Note that any path in api_base is stripped to the host root — make sure the host root is where /v1/models lives."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    models = get_models(api_key, api_base)\nexcept Exception as e:\n    if \"Failed to get models\" in str(e):\n        # body of the failed /v1/models response is in the message\n        log.warning(\"model listing unavailable: %s\", e)\n        models = FALLBACK_MODEL_LIST","preventionTips":["Don't hard-depend on /v1/models — keep a static fallback list","Remember api_base paths are stripped to host root for this call","Validate API keys before calling model-list helpers"],"tags":["openai","models","authentication","http"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}