The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor: scope /health response to caller's models and tidy display fields (#26935)
Krrish Dholakiab14e1d7d6a5b
diff --git a/litellm/proxy/health_check.py b/litellm/proxy/health_check.pyindex 7d67750c78f..7c340ff5df6 100644--- a/litellm/proxy/health_check.py+++ b/litellm/proxy/health_check.py@@ -29,6 +29,10 @@ ILLEGAL_DISPLAY_PARAMS = ["exception", # internal; not JSON-serializable, never for display"litellm_metadata", # internal tracking metadata with auth objects; not for display]+# Provider routing fields. Allowed for proxy admins so they can see which+# region/version a deployment is checking; gated at the endpoint layer for+# non-admin callers (see _strip_admin_only_fields_from_health_result).+ADMIN_ONLY_HEALTH_DISPLAY_PARAMS = ("api_base", "api_version")MINIMAL_DISPLAY_PARAMS = ["model", "mode_error"]diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.pyindex b4b5de1746e..1eda01e5c63 100644--- a/litellm/proxy/health_endpoints/_health_endpoints.py+++ b/litellm/proxy/health_endpoints/_health_endpoints.py@@ -20,6 +20,7 @@ from litellm.proxy._types import (CallInfo,EnterpriseLicenseData,Litellm_EntityType,+ LitellmUserRoles,ProxyErrorTypes,ProxyException,UserAPIKeyAuth,@@ -28,6 +29,7 @@ from litellm.proxy._types import (from litellm.proxy.auth.user_api_key_auth import user_api_key_authfrom litellm.proxy.db.exception_handler import PrismaDBExceptionHandlerfrom litellm.proxy.health_check import (+ ADMIN_ONLY_HEALTH_DISPLAY_PARAMS,_clean_endpoint_data,_update_litellm_params_for_health_check,perform_health_check,@@ -723,6 +725,90 @@ async def _save_background_health_checks_to_db(# Continue execution - don't let database save failure break health checks+_PROXY_ADMIN_ROLES = frozenset(+ {+ LitellmUserRoles.PROXY_ADMIN.value,+ # View-only admins are operators (oncall, support); they need the+ # routing fields (api_base, api_version) to diagnose health and tell+ # which provider region a check is hitting. They cannot mutate config+ # so granting them the read-only view is safe.+ LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY.value,+ }+)+++def _is_proxy_admin(user_api_key_dict: UserAPIKeyAuth) -> bool:+ """+ Return True if the caller has a proxy-admin role (full or view-only).++ user_role on UserAPIKeyAuth can be either a LitellmUserRoles enum or its+ string value depending on how the auth path constructed the object, so we+ compare against the raw value rather than the enum identity.+ """+ role = user_api_key_dict.user_role+ if role is None:+ return False+ role_value = role.value if hasattr(role, "value") else role+ return role_value in _PROXY_ADMIN_ROLES+++def _strip_admin_only_fields_from_health_result(result: dict) -> dict:+ """+ Return a copy of the /health response with provider routing fields+ (``api_base``, ``api_version``) removed from each healthy/unhealthy+ endpoint entry. Used to hide those fields from non-admin callers while+ still showing them which deployments they own and whether each one is+ healthy. Proxy admins receive the unmodified result.+ """+ out = dict(result)+ drop = set(ADMIN_ONLY_HEALTH_DISPLAY_PARAMS)+ for key in ("healthy_endpoints", "unhealthy_endpoints"):+ eps = out.get(key)+ if isinstance(eps, list):+ out[key] = [+ (+ {k: v for k, v in ep.items() if k not in drop}+ if isinstance(ep, dict)+ else ep+ )+ for ep in eps+ ]+ return out+++def _filter_health_check_results_by_model_ids(+ results: dict, allowed_model_ids: set+) -> dict:+ """+ Restrict a cached background health-check result dict to endpoints whose+ model_id is in ``allowed_model_ids``.++ Endpoints without a model_id (e.g. CLI-model entries that predate the+ model_id wiring) are dropped conservatively — we cannot prove they belong+ to the caller, so they are excluded rather than leaked.++ Each retained endpoint is shallow-copied before being returned, so any+ downstream transform (e.g. _strip_admin_only_fields_from_health_result)+ cannot accidentally mutate the shared ``health_check_results`` cache.+ """+ healthy = [+ dict(ep)+ for ep in (results.get("healthy_endpoints") or [])+ if ep.get("model_id") in allowed_model_ids+ ]+ unhealthy = [+ dict(ep)+ for ep in (results.get("unhealthy_endpoints") or [])+ if ep.get("model_id") in allowed_model_ids+ ]+ return {+ "healthy_endpoints": healthy,+ "unhealthy_endpoints": unhealthy,+ "healthy_count": len(healthy),+ "unhealthy_count": len(unhealthy),+ }++async def _perform_health_check_and_save(model_list,target_model,@@ -771,6 +857,7 @@ async def _perform_health_check_and_save(@router.get("/health", tags=["health"], dependencies=[Depends(user_api_key_auth)])async def health_endpoint(+ response: Response,user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),model: Optional[str] = fastapi.Query(None, description="Specify the model name (optional)"@@ -838,11 +925,26 @@ async def health_endpoint(detail={"error": f"Model with ID {model_id} not found"},)+ is_admin = _is_proxy_admin(user_api_key_dict)++ def _post_process(result: dict) -> dict:+ # api_base / api_version reveal which provider/region/internal host the+ # deployment talks to; only proxy admins receive them. Non-admin keys+ # still see model/model_id and the healthy/unhealthy status. We also+ # set a header so non-admin clients that previously parsed those+ # fields can detect the change programmatically.+ if is_admin:+ return result+ response.headers["Litellm-Health-Field-Notice"] = (+ "api_base and api_version are admin-only on this endpoint"+ )+ return _strip_admin_only_fields_from_health_result(result)+try:if llm_model_list is None:# if no router set, check if user set a model using litellm --model ollama/llama2if user_model is not None:- return await _perform_health_check_and_save(+ cli_result = await _perform_health_check_and_save(model_list=[],target_model=None,cli_model=user_model,@@ -853,20 +955,59 @@ async def health_endpoint(model_id=None, # CLI model doesn't have model_idmax_concurrency=health_check_concurrency,)+ return _post_process(cli_result)raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,detail={"error": "Model list not initialized"},)_llm_model_list = copy.deepcopy(llm_model_list)### FILTER MODELS FOR ONLY THOSE USER HAS ACCESS TO ###+ # Live path: scope by model_name (every deployment has one).+ # Cache path: scope by model_id (the cache is keyed on model_id).+ # Consequence: a deployment whose model_name the caller can access+ # but which lacks model_info.id will appear in the live /health+ # response but NOT in the background-cache /health response. This is+ # surfaced via the "warnings" field below so operators can fix the+ # missing model_info.id rather than guess at the discrepancy.if len(user_api_key_dict.models) > 0:- pass- else:- pass #+ allowed_models = set(user_api_key_dict.models)+ _llm_model_list = [+ m for m in _llm_model_list if m.get("model_name") in allowed_models+ ]if use_background_health_checks:- return health_check_results+ if len(user_api_key_dict.models) > 0:+ allowed_model_ids = {+ (m.get("model_info") or {}).get("id")+ for m in _llm_model_list+ if (m.get("model_info") or {}).get("id")+ }+ filtered = _filter_health_check_results_by_model_ids(+ health_check_results, allowed_model_ids+ )+ if not allowed_model_ids:+ # Caller has accessible model_names but none of the+ # matching deployments expose a model_info.id, so the+ # cache filter (which keys on model_id) drops every+ # entry. Surface this both as a warning log and a+ # structured "warnings" field on the response so the+ # caller can distinguish "no deployments found" from+ # "deployments excluded due to missing model_info.id".+ verbose_proxy_logger.warning(+ "health_endpoint: scoped key %s has accessible models %s "+ "but none of the matching deployments carry a model_info.id; "+ "background health-check cache will return an empty result.",+ user_api_key_dict.user_id,+ list(user_api_key_dict.models),+ )+ filtered["warnings"] = [+ "Some accessible deployments are missing model_info.id "+ "and were excluded from this response. Ask a proxy admin "+ "to populate model_info.id for these models."+ ]+ return _post_process(filtered)+ return _post_process(health_check_results)else:- return await _perform_health_check_and_save(+ router_result = await _perform_health_check_and_save(model_list=_llm_model_list,target_model=target_model,cli_model=None,@@ -877,6 +1018,7 @@ async def health_endpoint(model_id=model_id,max_concurrency=health_check_concurrency,)+ return _post_process(router_result)except Exception as e:verbose_proxy_logger.error("litellm.proxy.proxy_server.py::health_endpoint(): Exception occured - {}".format(diff --git a/tests/test_litellm/proxy/health_endpoints/test_health_endpoints.py b/tests/test_litellm/proxy/health_endpoints/test_health_endpoints.pyindex ba260142351..d59682c2d5f 100644--- a/tests/test_litellm/proxy/health_endpoints/test_health_endpoints.py+++ b/tests/test_litellm/proxy/health_endpoints/test_health_endpoints.py@@ -778,3 +778,373 @@ def test_get_callback_identifier_custom_logger_registry_and_fallback():result = get_callback_identifier(my_callback_function)# Should fall back to callback_name() which returns __name__assert result == "my_callback_function"+++# ---------------------------------------------------------------------------+# /health response shape: model-access scoping and display-field allowlist+# ---------------------------------------------------------------------------+# These tests pin the contract that the /health response (a) only includes+# deployments the calling key is allowed to see, and (b) does not return+# provider routing fields like api_base / api_version. They guard against+# regressions that would widen the response shape.+++async def test_health_endpoint_filters_model_list_by_user_access():+ """+ health_endpoint() should restrict _llm_model_list to deployments whose+ model_name appears in user_api_key_dict.models before running the health+ check. A key scoped to ["model-a"] should only see model-a in the result,+ not other deployments configured on the proxy.+ """+ from litellm.proxy._types import UserAPIKeyAuth+ from litellm.proxy.health_endpoints._health_endpoints import health_endpoint++ full_model_list = [+ {+ "model_name": "model-a",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-a.test",+ },+ "model_info": {"id": "id-a"},+ },+ {+ "model_name": "model-b",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-b.test",+ "api_version": "2024-10-21",+ },+ "model_info": {"id": "id-b"},+ },+ ]++ user_api_key_dict = UserAPIKeyAuth(+ api_key="hashed-test-key",+ models=["model-a"],+ )++ captured: dict = {}++ async def fake_perform(**kwargs):+ captured["model_list"] = kwargs["model_list"]+ return {+ "healthy_endpoints": [],+ "unhealthy_endpoints": [],+ "healthy_count": 0,+ "unhealthy_count": 0,+ }++ with (+ patch("litellm.proxy.proxy_server.llm_model_list", full_model_list),+ patch("litellm.proxy.proxy_server.llm_router", None),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch("litellm.proxy.proxy_server.use_background_health_checks", False),+ patch("litellm.proxy.proxy_server.user_model", None),+ patch("litellm.proxy.proxy_server.health_check_results", {}),+ patch("litellm.proxy.proxy_server.health_check_details", True),+ patch("litellm.proxy.proxy_server.health_check_concurrency", 1),+ patch(+ "litellm.proxy.health_endpoints._health_endpoints._perform_health_check_and_save",+ side_effect=fake_perform,+ ),+ ):+ from fastapi import Response++ await health_endpoint(response=Response(), user_api_key_dict=user_api_key_dict)++ assert (+ "model_list" in captured+ ), "health_endpoint did not call _perform_health_check_and_save"+ returned_names = {m["model_name"] for m in captured["model_list"]}+ assert returned_names == {+ "model-a"+ }, f"health_endpoint did not scope model_list to caller access: {returned_names}"+++async def test_health_endpoint_filters_background_cache_by_user_access():+ """+ When background_health_checks is enabled, health_endpoint() should also+ scope the cached result to the caller's allowed models rather than+ returning the cache verbatim.+ """+ from litellm.proxy._types import UserAPIKeyAuth+ from litellm.proxy.health_endpoints._health_endpoints import health_endpoint++ full_model_list = [+ {+ "model_name": "model-a",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-a.test",+ },+ "model_info": {"id": "id-a"},+ },+ {+ "model_name": "model-b",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-b.test",+ },+ "model_info": {"id": "id-b"},+ },+ ]++ cached_results = {+ "healthy_endpoints": [+ {+ "model": "openai/gpt-4o",+ "model_id": "id-a",+ "api_base": "https://example-a.test",+ },+ {+ "model": "openai/gpt-4o",+ "model_id": "id-b",+ "api_base": "https://example-b.test",+ },+ ],+ "unhealthy_endpoints": [],+ "healthy_count": 2,+ "unhealthy_count": 0,+ }++ user_api_key_dict = UserAPIKeyAuth(+ api_key="hashed-test-key",+ models=["model-a"],+ )++ with (+ patch("litellm.proxy.proxy_server.llm_model_list", full_model_list),+ patch("litellm.proxy.proxy_server.llm_router", None),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch("litellm.proxy.proxy_server.use_background_health_checks", True),+ patch("litellm.proxy.proxy_server.user_model", None),+ patch("litellm.proxy.proxy_server.health_check_results", cached_results),+ patch("litellm.proxy.proxy_server.health_check_details", True),+ patch("litellm.proxy.proxy_server.health_check_concurrency", 1),+ ):+ from fastapi import Response++ result = await health_endpoint(+ response=Response(), user_api_key_dict=user_api_key_dict+ )++ # Sanity: the source cache had two entries before scoping; the scoping+ # step is what reduces it to one. (This guards against the test passing+ # vacuously when the cache filter drops everything because cached+ # entries lack the model_id key — both entries carry model_id above.)+ assert len(cached_results["healthy_endpoints"]) == 2+ assert all(+ ep.get("model_id") for ep in cached_results["healthy_endpoints"]+ ), "test fixture invariant: every cached entry must carry a model_id"++ # The non-admin caller must not see api_base on the returned cache entries.+ returned = result.get("healthy_endpoints", [])+ assert (+ len(returned) == 1+ ), f"expected exactly one cached entry after scoping, got {len(returned)}"+ assert returned[0]["model_id"] == "id-a"+ assert "api_base" not in returned[0]+ assert result["healthy_count"] == 1+ assert result["unhealthy_count"] == 0+++async def test_health_endpoint_admin_sees_routing_fields_non_admin_does_not():+ """+ A proxy admin should still see ``api_base`` and ``api_version`` in the+ /health response so they can tell which Vertex region / Azure resource+ + API version is healthy. A non-admin caller must not — both fields+ should be stripped, and the response should carry a notice header so+ non-admin clients can detect the change programmatically.+ """+ from fastapi import Response++ from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth+ from litellm.proxy.health_endpoints._health_endpoints import health_endpoint++ full_model_list = [+ {+ "model_name": "model-a",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-a.test",+ },+ "model_info": {"id": "id-a"},+ },+ ]+ cached_results = {+ "healthy_endpoints": [+ {+ "model": "openai/gpt-4o",+ "model_id": "id-a",+ "api_base": "https://us-central1-aiplatform.googleapis.com/v1/projects/p",+ "api_version": "2024-10-21",+ },+ ],+ "unhealthy_endpoints": [],+ "healthy_count": 1,+ "unhealthy_count": 0,+ }++ admin_key = UserAPIKeyAuth(+ api_key="hashed-admin-key",+ models=["model-a"],+ user_role=LitellmUserRoles.PROXY_ADMIN,+ )+ non_admin_key = UserAPIKeyAuth(+ api_key="hashed-user-key",+ models=["model-a"],+ )++ common_patches = [+ patch("litellm.proxy.proxy_server.llm_model_list", full_model_list),+ patch("litellm.proxy.proxy_server.llm_router", None),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch("litellm.proxy.proxy_server.use_background_health_checks", True),+ patch("litellm.proxy.proxy_server.user_model", None),+ patch("litellm.proxy.proxy_server.health_check_results", cached_results),+ patch("litellm.proxy.proxy_server.health_check_details", True),+ patch("litellm.proxy.proxy_server.health_check_concurrency", 1),+ ]++ for p in common_patches:+ p.start()+ try:+ admin_response = Response()+ non_admin_response = Response()+ admin_result = await health_endpoint(+ response=admin_response, user_api_key_dict=admin_key+ )+ non_admin_result = await health_endpoint(+ response=non_admin_response, user_api_key_dict=non_admin_key+ )+ finally:+ for p in common_patches:+ p.stop()++ admin_eps = admin_result.get("healthy_endpoints", [])+ non_admin_eps = non_admin_result.get("healthy_endpoints", [])++ assert len(admin_eps) == 1+ assert (+ admin_eps[0]["api_base"]+ == "https://us-central1-aiplatform.googleapis.com/v1/projects/p"+ ), "admin must see the full api_base so they can identify the region"+ assert (+ admin_eps[0]["api_version"] == "2024-10-21"+ ), "admin must see api_version so they can distinguish provider deployments"++ assert len(non_admin_eps) == 1+ assert "api_base" not in non_admin_eps[0]+ assert "api_version" not in non_admin_eps[0]++ # Non-admin response must advertise that api_base/api_version were+ # withheld so clients that previously parsed them can detect the change.+ assert (+ non_admin_response.headers.get("Litellm-Health-Field-Notice")+ == "api_base and api_version are admin-only on this endpoint"+ )+ assert "Litellm-Health-Field-Notice" not in admin_response.headers++ # Stripping must produce a copy — the shared cache must still carry the+ # routing fields so the next admin caller can read them.+ cached_first = cached_results["healthy_endpoints"][0]+ assert (+ cached_first["api_base"]+ == "https://us-central1-aiplatform.googleapis.com/v1/projects/p"+ )+ assert cached_first["api_version"] == "2024-10-21"+++async def test_health_endpoint_warns_when_scoped_models_lack_model_id():+ """+ When a scoped key's accessible models exist on the proxy but none of the+ matching deployments expose a ``model_info.id``, the cache filter drops+ everything. The response should include a structured ``warnings`` field+ so the caller can distinguish "no deployments configured" from+ "deployments excluded due to missing model_info.id".+ """+ from fastapi import Response++ from litellm.proxy._types import UserAPIKeyAuth+ from litellm.proxy.health_endpoints._health_endpoints import health_endpoint++ full_model_list = [+ {+ "model_name": "model-a",+ "litellm_params": {+ "model": "openai/gpt-4o",+ "api_base": "https://example-a.test",+ },+ # Intentionally no model_info.id — this is the misconfiguration+ # the warnings field is meant to flag.+ "model_info": {},+ },+ ]+ cached_results = {+ "healthy_endpoints": [+ {+ "model": "openai/gpt-4o",+ "model_id": "id-a",+ "api_base": "https://example-a.test",+ },+ ],+ "unhealthy_endpoints": [],+ "healthy_count": 1,+ "unhealthy_count": 0,+ }+ user_api_key_dict = UserAPIKeyAuth(+ api_key="hashed-user-key",+ models=["model-a"],+ )++ with (+ patch("litellm.proxy.proxy_server.llm_model_list", full_model_list),+ patch("litellm.proxy.proxy_server.llm_router", None),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch("litellm.proxy.proxy_server.use_background_health_checks", True),+ patch("litellm.proxy.proxy_server.user_model", None),+ patch("litellm.proxy.proxy_server.health_check_results", cached_results),+ patch("litellm.proxy.proxy_server.health_check_details", True),+ patch("litellm.proxy.proxy_server.health_check_concurrency", 1),+ ):+ result = await health_endpoint(+ response=Response(), user_api_key_dict=user_api_key_dict+ )++ assert result["healthy_count"] == 0+ assert result["unhealthy_count"] == 0+ assert "warnings" in result, (+ "empty cache result must surface a warnings field so the caller "+ "can distinguish 'no deployments' from 'deployments excluded'"+ )+ assert any("model_info.id" in w for w in result["warnings"])+++def test_clean_endpoint_data_strips_credentials_keeps_routing_fields():+ """+ _clean_endpoint_data() drops credentials but leaves api_base /+ api_version intact — the per-caller hide/show happens in the endpoint+ layer based on user role, not in the cleaning helper. This guarantees+ proxy admins continue to see those fields in the /health response.+ """+ from litellm.proxy.health_check import _clean_endpoint_data++ raw = {+ "model": "openai/gpt-4o",+ "api_key": "sk-test",+ "api_base": "https://example.test/v1",+ "api_version": "2024-10-21",+ "aws_access_key_id": "AKIAEXAMPLE",+ }++ cleaned = _clean_endpoint_data(raw, details=True)++ assert "api_key" not in cleaned+ assert "aws_access_key_id" not in cleaned+ assert cleaned.get("api_base") == "https://example.test/v1"+ assert cleaned.get("api_version") == "2024-10-21"