From 72a960173adfff54686da857ea8069877c723e04 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Tue, 9 Jun 2026 18:38:04 +0530 Subject: [PATCH 1/7] fix(proxy): align /v1/model/info with router deployments Return router model_list entries (including team-scoped models) with team access metadata instead of wildcard-expanded names from get_complete_model_list. Co-authored-by: Cursor --- litellm/proxy/proxy_server.py | 117 ++++++++++++------ .../test_team_model_name_translation.py | 54 ++++++-- .../proxy/test_model_info_default_limits.py | 3 +- tests/test_litellm/proxy/test_proxy_server.py | 3 +- 4 files changed, 127 insertions(+), 50 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 213f682b8f7a..69e9fcf8b831 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -12384,6 +12384,27 @@ async def model_metrics_exceptions( return {"data": response, "exception_types": list(exception_types)} +def _deployment_matches_allowed_model_names( + model: Dict[str, Any], allowed_model_names: Set[str] +) -> bool: + """Match a router deployment against allowed public model names. + + Team-scoped rows store an internal routing key in ``model_name``; callers + with key/team restrictions still refer to the public name in + ``model_info.team_public_model_name``. + """ + if model.get("model_name") in allowed_model_names: + return True + model_info = model.get("model_info") + if not isinstance(model_info, dict): + return False + team_public_model_name = model_info.get("team_public_model_name") + return ( + isinstance(team_public_model_name, str) + and team_public_model_name in allowed_model_names + ) + + def _translate_model_name_for_response(model: dict) -> dict: """For team-scoped DB rows, replace `model_name` with the public name in `model_info.team_public_model_name` before returning. The DB column @@ -12553,49 +12574,67 @@ async def model_info_v1( # noqa: PLR0915 ) return {"data": [_deployment_info_dict]} - all_models: List[dict] = [] - model_access_groups: Dict[str, List[str]] = defaultdict(list) - ## CHECK IF MODEL RESTRICTIONS ARE SET AT KEY/TEAM LEVEL ## - if llm_router is None: - proxy_model_list = [] + # Return router deployments (same source as /v2/model/info), not wildcard- + # expanded model names from get_complete_model_list(). Team-scoped rows + # use internal routing keys (model_name_{team_id}_{uuid}) and were omitted + # when v1 resolved models only via public model_name strings. + all_models: List[dict] = copy.deepcopy(llm_router.model_list) + + if prisma_client is not None: + all_models = await get_all_team_and_direct_access_models( + user_api_key_dict=user_api_key_dict, + prisma_client=prisma_client, + llm_router=llm_router, + all_models=all_models, + ) else: - proxy_model_list = llm_router.get_model_names() model_access_groups = llm_router.get_model_access_groups() - key_models = get_key_models( - user_api_key_dict=user_api_key_dict, - proxy_model_list=proxy_model_list, - model_access_groups=model_access_groups, - ) - team_models = get_team_models( - team_models=user_api_key_dict.team_models, - proxy_model_list=proxy_model_list, - model_access_groups=model_access_groups, - ) - all_models_str = get_complete_model_list( - key_models=key_models, - team_models=team_models, - proxy_model_list=proxy_model_list, - user_model=user_model, - infer_model_from_keys=general_settings.get("infer_model_from_keys", False), - llm_router=llm_router, - ) + proxy_model_list = llm_router.get_model_names() + key_models = get_key_models( + user_api_key_dict=user_api_key_dict, + proxy_model_list=proxy_model_list, + model_access_groups=model_access_groups, + ) + team_models = get_team_models( + team_models=user_api_key_dict.team_models, + proxy_model_list=proxy_model_list, + model_access_groups=model_access_groups, + ) + if key_models or team_models: + allowed_model_names = set( + get_complete_model_list( + key_models=key_models, + team_models=team_models, + proxy_model_list=proxy_model_list, + user_model=user_model, + infer_model_from_keys=general_settings.get( + "infer_model_from_keys", False + ), + llm_router=llm_router, + return_wildcard_routes=False, + ) + ) + all_models = [ + model + for model in all_models + if _deployment_matches_allowed_model_names(model, allowed_model_names) + ] - if len(all_models_str) > 0: - _relevant_models = [] - for model in all_models_str: - router_models = llm_router.get_model_list(model_name=model) - if router_models is not None: - _relevant_models.extend(router_models) - if llm_model_list is not None: - all_models = copy.deepcopy(_relevant_models) # type: ignore - else: - all_models = [] + all_models = [ + _translate_model_name_for_response( + _enrich_model_info_with_litellm_data(model=model, llm_router=llm_router) + ) + for model in all_models + ] + + from litellm.proxy.agent_endpoints.model_list_helpers import ( + append_agents_to_model_info, + ) - # Reassign each entry: _get_proxy_model_info returns a (possibly new) - # dict via _translate_model_name_for_response, which does NOT mutate in - # place. Binding only the loop variable would drop the public-name swap - # for team-scoped rows and leak the internal routing key (#28382). - all_models = [_get_proxy_model_info(model=model) for model in all_models] + all_models = await append_agents_to_model_info( + models=all_models, + user_api_key_dict=user_api_key_dict, + ) verbose_proxy_logger.debug("all_models: %s", all_models) return {"data": all_models} diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index 97e5c494916d..1d46113ed3cd 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -151,21 +151,53 @@ async def test_model_info_v2_translates_team_model_name(monkeypatch): @pytest.mark.asyncio async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): - """/v1/model/info list path (no litellm_model_id) must surface the public - name. Covers the list comprehension that assigns _get_proxy_model_info's - return back into all_models (#28382 review).""" + """/v1/model/info list path (no litellm_model_id) must include team-scoped + deployments from the router model list and surface the public name plus + access_via_team_ids (#28382).""" + team_row = _team_row() + global_row = { + "model_name": "gpt-4o", + "litellm_params": {"model": "gpt-4o"}, + "model_info": {"id": "normal-id-1", "db_model": False}, + } router = MagicMock() - router.get_model_names.return_value = ["team-claude-sonnet"] + router.model_list = [team_row, global_row] + router.get_model_names.return_value = ["gpt-4o"] router.get_model_access_groups.return_value = {} - router.get_model_list.return_value = [_team_row()] monkeypatch.setattr(ps, "user_model", None) - monkeypatch.setattr(ps, "llm_model_list", [_team_row()]) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) - monkeypatch.setattr(ps, "get_key_models", lambda **kw: []) - monkeypatch.setattr(ps, "get_team_models", lambda **kw: []) + monkeypatch.setattr(ps, "prisma_client", MagicMock()) monkeypatch.setattr( - ps, "get_complete_model_list", lambda **kw: ["team-claude-sonnet"] + ps, + "get_all_team_and_direct_access_models", + AsyncMock( + side_effect=lambda all_models, **kwargs: [ + { + **m, + "model_info": { + **m.get("model_info", {}), + **( + {"access_via_team_ids": ["team-abc-123"]} + if m.get("model_info", {}).get("team_id") + else {"direct_access": True} + ), + }, + } + for m in all_models + ] + ), + ) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + import litellm.proxy.agent_endpoints.model_list_helpers as mlh + + monkeypatch.setattr( + mlh, + "append_agents_to_model_info", + AsyncMock(side_effect=lambda models, **kw: models), ) admin = UserAPIKeyAuth( @@ -176,3 +208,7 @@ async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): names = [m["model_name"] for m in resp["data"]] assert "team-claude-sonnet" in names assert "model_name_team-abc-123_4a6b8" not in names + team_model = next( + m for m in resp["data"] if m["model_name"] == "team-claude-sonnet" + ) + assert team_model["model_info"]["access_via_team_ids"] == ["team-abc-123"] diff --git a/tests/test_litellm/proxy/test_model_info_default_limits.py b/tests/test_litellm/proxy/test_model_info_default_limits.py index 641199c96f05..8111a7af0065 100644 --- a/tests/test_litellm/proxy/test_model_info_default_limits.py +++ b/tests/test_litellm/proxy/test_model_info_default_limits.py @@ -146,9 +146,9 @@ async def test_model_info_endpoint_returns_defaults_in_full_model_list(self): deployment_dict = deployment.model_dump(exclude_none=True) mock_router = MagicMock() + mock_router.model_list = [deployment_dict] mock_router.get_model_names.return_value = ["model1"] mock_router.get_model_access_groups.return_value = {} - mock_router.get_model_list.return_value = [deployment_dict] user_api_key_dict = UserAPIKeyAuth(api_key="sk-test") @@ -156,6 +156,7 @@ async def test_model_info_endpoint_returns_defaults_in_full_model_list(self): patch("litellm.proxy.proxy_server.llm_router", mock_router), patch("litellm.proxy.proxy_server.llm_model_list", [deployment_dict]), patch("litellm.proxy.proxy_server.user_model", None), + patch("litellm.proxy.proxy_server.prisma_client", None), patch("litellm.proxy.proxy_server.get_key_models", return_value=["model1"]), patch( "litellm.proxy.proxy_server.get_team_models", return_value=["model1"] diff --git a/tests/test_litellm/proxy/test_proxy_server.py b/tests/test_litellm/proxy/test_proxy_server.py index 8aa839cdfcb6..13fb125fc27b 100644 --- a/tests/test_litellm/proxy/test_proxy_server.py +++ b/tests/test_litellm/proxy/test_proxy_server.py @@ -3810,14 +3810,15 @@ async def test_model_info_v1_oci_secrets_not_leaked(): # Mock the llm_router to return our test data mock_router = MagicMock() + mock_router.model_list = [mock_model_data] mock_router.get_model_names.return_value = ["oci-grok-test"] mock_router.get_model_access_groups.return_value = {} - mock_router.get_model_list.return_value = [mock_model_data] # Mock global variables with ( patch("litellm.proxy.proxy_server.llm_router", mock_router), patch("litellm.proxy.proxy_server.llm_model_list", [mock_model_data]), + patch("litellm.proxy.proxy_server.prisma_client", None), patch( "litellm.proxy.proxy_server.general_settings", {"infer_model_from_keys": False}, From 99c58a944fe4b7a4830dcbe731bbd7993ac77cef Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 10 Jun 2026 17:02:33 +0530 Subject: [PATCH 2/7] fix(proxy): gate v1 team filter and honor key allowlists Only apply get_all_team_and_direct_access_models for admin or user-bound keys, then intersect with key/team model restrictions to avoid empty lists for service tokens and metadata leaks for restricted keys. Co-authored-by: Cursor --- litellm/proxy/proxy_server.py | 100 +++++++++++------ .../test_team_model_name_translation.py | 102 ++++++++++++++++++ 2 files changed, 169 insertions(+), 33 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 69e9fcf8b831..a29f70779513 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -12405,6 +12405,61 @@ def _deployment_matches_allowed_model_names( ) +def _get_v1_model_info_allowed_model_names( + user_api_key_dict: UserAPIKeyAuth, + llm_router: Router, +) -> Optional[Set[str]]: + """Return key/team allowlisted public model names, or None if unrestricted.""" + model_access_groups = llm_router.get_model_access_groups() + proxy_model_list = llm_router.get_model_names() + key_models = get_key_models( + user_api_key_dict=user_api_key_dict, + proxy_model_list=proxy_model_list, + model_access_groups=model_access_groups, + ) + team_models = get_team_models( + team_models=user_api_key_dict.team_models, + proxy_model_list=proxy_model_list, + model_access_groups=model_access_groups, + ) + if not key_models and not team_models: + return None + return set( + get_complete_model_list( + key_models=key_models, + team_models=team_models, + proxy_model_list=proxy_model_list, + user_model=user_model, + infer_model_from_keys=general_settings.get("infer_model_from_keys", False), + llm_router=llm_router, + return_wildcard_routes=False, + ) + ) + + +def _filter_v1_model_info_deployments( + all_models: List[dict], + allowed_model_names: Optional[Set[str]], +) -> List[dict]: + if allowed_model_names is None: + return all_models + return [ + model + for model in all_models + if _deployment_matches_allowed_model_names(model, allowed_model_names) + ] + + +def _should_apply_v1_team_access_filter( + user_api_key_dict: UserAPIKeyAuth, +) -> bool: + """Team membership filtering requires a resolvable user or admin role.""" + return ( + user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN + or user_api_key_dict.user_id is not None + ) + + def _translate_model_name_for_response(model: dict) -> dict: """For team-scoped DB rows, replace `model_name` with the public name in `model_info.team_public_model_name` before returning. The DB column @@ -12579,46 +12634,25 @@ async def model_info_v1( # noqa: PLR0915 # use internal routing keys (model_name_{team_id}_{uuid}) and were omitted # when v1 resolved models only via public model_name strings. all_models: List[dict] = copy.deepcopy(llm_router.model_list) + allowed_model_names = _get_v1_model_info_allowed_model_names( + user_api_key_dict=user_api_key_dict, + llm_router=llm_router, + ) - if prisma_client is not None: + if prisma_client is not None and _should_apply_v1_team_access_filter( + user_api_key_dict=user_api_key_dict + ): all_models = await get_all_team_and_direct_access_models( user_api_key_dict=user_api_key_dict, prisma_client=prisma_client, llm_router=llm_router, all_models=all_models, ) - else: - model_access_groups = llm_router.get_model_access_groups() - proxy_model_list = llm_router.get_model_names() - key_models = get_key_models( - user_api_key_dict=user_api_key_dict, - proxy_model_list=proxy_model_list, - model_access_groups=model_access_groups, - ) - team_models = get_team_models( - team_models=user_api_key_dict.team_models, - proxy_model_list=proxy_model_list, - model_access_groups=model_access_groups, - ) - if key_models or team_models: - allowed_model_names = set( - get_complete_model_list( - key_models=key_models, - team_models=team_models, - proxy_model_list=proxy_model_list, - user_model=user_model, - infer_model_from_keys=general_settings.get( - "infer_model_from_keys", False - ), - llm_router=llm_router, - return_wildcard_routes=False, - ) - ) - all_models = [ - model - for model in all_models - if _deployment_matches_allowed_model_names(model, allowed_model_names) - ] + + all_models = _filter_v1_model_info_deployments( + all_models=all_models, + allowed_model_names=allowed_model_names, + ) all_models = [ _translate_model_name_for_response( diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index 1d46113ed3cd..df9b45fd29d6 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -212,3 +212,105 @@ async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): m for m in resp["data"] if m["model_name"] == "team-claude-sonnet" ) assert team_model["model_info"]["access_via_team_ids"] == ["team-abc-123"] + + +@pytest.mark.asyncio +async def test_model_info_v1_no_user_id_with_db_skips_team_access_filter(monkeypatch): + """Service/CI keys without user_id must not hit the team-membership filter.""" + deployment = { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1", "db_model": False}, + } + router = MagicMock() + router.model_list = [deployment] + router.get_model_names.return_value = ["gpt-4"] + router.get_model_access_groups.return_value = {} + + get_team_access = AsyncMock() + monkeypatch.setattr(ps, "user_model", None) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) + monkeypatch.setattr(ps, "llm_router", router) + monkeypatch.setattr(ps, "prisma_client", MagicMock()) + monkeypatch.setattr(ps, "get_all_team_and_direct_access_models", get_team_access) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + import litellm.proxy.agent_endpoints.model_list_helpers as mlh + + monkeypatch.setattr( + mlh, + "append_agents_to_model_info", + AsyncMock(side_effect=lambda models, **kw: models), + ) + + caller = UserAPIKeyAuth( + user_id=None, + user_role=LitellmUserRoles.INTERNAL_USER, + models=[], + team_models=[], + ) + resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) + + assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] + get_team_access.assert_not_called() + + +@pytest.mark.asyncio +async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkeypatch): + """Key-level model allowlists must apply after DB team-access enrichment.""" + team_row = _team_row() + global_row = { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1", "db_model": False}, + } + router = MagicMock() + router.model_list = [team_row, global_row] + router.get_model_names.return_value = ["gpt-4", "team-claude-sonnet"] + router.get_model_access_groups.return_value = {} + + monkeypatch.setattr(ps, "user_model", None) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) + monkeypatch.setattr(ps, "llm_router", router) + monkeypatch.setattr(ps, "prisma_client", MagicMock()) + monkeypatch.setattr( + ps, + "get_all_team_and_direct_access_models", + AsyncMock( + side_effect=lambda all_models, **kwargs: [ + { + **m, + "model_info": { + **m.get("model_info", {}), + **( + {"access_via_team_ids": ["team-abc-123"]} + if m.get("model_info", {}).get("team_id") + else {"direct_access": True} + ), + }, + } + for m in all_models + ] + ), + ) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + import litellm.proxy.agent_endpoints.model_list_helpers as mlh + + monkeypatch.setattr( + mlh, + "append_agents_to_model_info", + AsyncMock(side_effect=lambda models, **kw: models), + ) + + caller = UserAPIKeyAuth( + user_id="user-1", + user_role=LitellmUserRoles.INTERNAL_USER, + models=["gpt-4"], + team_models=[], + ) + resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) + + assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] From 74e1fbd77a981103cd9a4ed1cbdd662f5cbcf209 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 10 Jun 2026 17:31:01 +0530 Subject: [PATCH 3/7] fix(proxy): skip v1 team filter when user row is missing Require a DB-backed user before applying team-access filtering on /v1/model/info, and skip the trailing filter in get_all_team_and_direct_access_models when user context cannot be resolved. Co-authored-by: Cursor --- litellm/proxy/proxy_server.py | 37 +++++--- .../test_team_model_name_translation.py | 90 +++++++++++++++++++ 2 files changed, 114 insertions(+), 13 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index a29f70779513..57566bed4062 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -10998,12 +10998,17 @@ async def get_all_team_and_direct_access_models( _model["model_info"]["direct_access"] = True ## FILTER OUT MODELS THAT ARE NOT IN DIRECT_ACCESS_MODELS OR ACCESS_VIA_TEAM_IDS - only show user models they can call - all_models = [ - _model - for _model in all_models - if _model.get("model_info", {}).get("direct_access", False) - or _model.get("model_info", {}).get("access_via_team_ids", []) - ] + should_filter_by_user_access = ( + user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN + or user_teams is not None + ) + if should_filter_by_user_access: + all_models = [ + _model + for _model in all_models + if _model.get("model_info", {}).get("direct_access", False) + or _model.get("model_info", {}).get("access_via_team_ids", []) + ] return all_models @@ -12450,14 +12455,19 @@ def _filter_v1_model_info_deployments( ] -def _should_apply_v1_team_access_filter( +async def _should_apply_v1_team_access_filter( user_api_key_dict: UserAPIKeyAuth, + prisma_client: PrismaClient, ) -> bool: - """Team membership filtering requires a resolvable user or admin role.""" - return ( - user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN - or user_api_key_dict.user_id is not None + """Team membership filtering requires admin role or a DB-backed user row.""" + if user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN: + return True + if user_api_key_dict.user_id is None: + return False + user_db_object = await UserRepository(prisma_client).table.find_unique( + where={"user_id": user_api_key_dict.user_id} ) + return user_db_object is not None def _translate_model_name_for_response(model: dict) -> dict: @@ -12639,8 +12649,9 @@ async def model_info_v1( # noqa: PLR0915 llm_router=llm_router, ) - if prisma_client is not None and _should_apply_v1_team_access_filter( - user_api_key_dict=user_api_key_dict + if prisma_client is not None and await _should_apply_v1_team_access_filter( + user_api_key_dict=user_api_key_dict, + prisma_client=prisma_client, ): all_models = await get_all_team_and_direct_access_models( user_api_key_dict=user_api_key_dict, diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index df9b45fd29d6..bc9a44010fa9 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -270,6 +270,12 @@ async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkey router.get_model_names.return_value = ["gpt-4", "team-claude-sonnet"] router.get_model_access_groups.return_value = {} + class MockUserRepository: + def __init__(self, prisma_client): + self.table = MagicMock() + self.table.find_unique = AsyncMock(return_value=MagicMock(model_dump=lambda: {"user_id": "user-1", "teams": []})) + + monkeypatch.setattr(ps, "UserRepository", MockUserRepository) monkeypatch.setattr(ps, "user_model", None) monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) @@ -314,3 +320,87 @@ async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkey resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] + + +@pytest.mark.asyncio +async def test_model_info_v1_missing_db_user_returns_deployments(monkeypatch): + """Keys with user_id but no DB row must not lose their model list.""" + deployment = { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1", "db_model": False}, + } + router = MagicMock() + router.model_list = [deployment] + router.get_model_names.return_value = ["gpt-4"] + router.get_model_access_groups.return_value = {} + + class MockUserRepository: + def __init__(self, prisma_client): + self.table = MagicMock() + self.table.find_unique = AsyncMock(return_value=None) + + get_team_access = AsyncMock() + monkeypatch.setattr(ps, "UserRepository", MockUserRepository) + monkeypatch.setattr(ps, "user_model", None) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) + monkeypatch.setattr(ps, "llm_router", router) + monkeypatch.setattr(ps, "prisma_client", MagicMock()) + monkeypatch.setattr(ps, "get_all_team_and_direct_access_models", get_team_access) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + import litellm.proxy.agent_endpoints.model_list_helpers as mlh + + monkeypatch.setattr( + mlh, + "append_agents_to_model_info", + AsyncMock(side_effect=lambda models, **kw: models), + ) + + caller = UserAPIKeyAuth( + user_id="missing-user", + user_role=LitellmUserRoles.INTERNAL_USER, + models=[], + team_models=[], + ) + resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) + + assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] + get_team_access.assert_not_called() + + +@pytest.mark.asyncio +async def test_get_all_team_and_direct_access_models_missing_user_skips_filter( + monkeypatch, +): + """Defense in depth: unresolved user_id must not empty the model list.""" + models = [ + { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1"}, + } + ] + + class MockUserRepository: + def __init__(self, prisma_client): + self.table = MagicMock() + self.table.find_unique = AsyncMock(return_value=None) + + monkeypatch.setattr(ps, "UserRepository", MockUserRepository) + + caller = UserAPIKeyAuth( + user_id="missing-user", + user_role=LitellmUserRoles.INTERNAL_USER, + models=[], + team_models=[], + ) + result = await ps.get_all_team_and_direct_access_models( + user_api_key_dict=caller, + prisma_client=MagicMock(), + llm_router=MagicMock(), + all_models=models, + ) + + assert result == models From 2c08b393dc332f07c64e0635fd2406288fcfe3b2 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 10 Jun 2026 17:35:36 +0530 Subject: [PATCH 4/7] Revert "fix(proxy): skip v1 team filter when user row is missing" This reverts commit 74e1fbd77a981103cd9a4ed1cbdd662f5cbcf209. --- litellm/proxy/proxy_server.py | 37 +++----- .../test_team_model_name_translation.py | 90 ------------------- 2 files changed, 13 insertions(+), 114 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 57566bed4062..a29f70779513 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -10998,17 +10998,12 @@ async def get_all_team_and_direct_access_models( _model["model_info"]["direct_access"] = True ## FILTER OUT MODELS THAT ARE NOT IN DIRECT_ACCESS_MODELS OR ACCESS_VIA_TEAM_IDS - only show user models they can call - should_filter_by_user_access = ( - user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN - or user_teams is not None - ) - if should_filter_by_user_access: - all_models = [ - _model - for _model in all_models - if _model.get("model_info", {}).get("direct_access", False) - or _model.get("model_info", {}).get("access_via_team_ids", []) - ] + all_models = [ + _model + for _model in all_models + if _model.get("model_info", {}).get("direct_access", False) + or _model.get("model_info", {}).get("access_via_team_ids", []) + ] return all_models @@ -12455,19 +12450,14 @@ def _filter_v1_model_info_deployments( ] -async def _should_apply_v1_team_access_filter( +def _should_apply_v1_team_access_filter( user_api_key_dict: UserAPIKeyAuth, - prisma_client: PrismaClient, ) -> bool: - """Team membership filtering requires admin role or a DB-backed user row.""" - if user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN: - return True - if user_api_key_dict.user_id is None: - return False - user_db_object = await UserRepository(prisma_client).table.find_unique( - where={"user_id": user_api_key_dict.user_id} + """Team membership filtering requires a resolvable user or admin role.""" + return ( + user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN + or user_api_key_dict.user_id is not None ) - return user_db_object is not None def _translate_model_name_for_response(model: dict) -> dict: @@ -12649,9 +12639,8 @@ async def model_info_v1( # noqa: PLR0915 llm_router=llm_router, ) - if prisma_client is not None and await _should_apply_v1_team_access_filter( - user_api_key_dict=user_api_key_dict, - prisma_client=prisma_client, + if prisma_client is not None and _should_apply_v1_team_access_filter( + user_api_key_dict=user_api_key_dict ): all_models = await get_all_team_and_direct_access_models( user_api_key_dict=user_api_key_dict, diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index bc9a44010fa9..df9b45fd29d6 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -270,12 +270,6 @@ async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkey router.get_model_names.return_value = ["gpt-4", "team-claude-sonnet"] router.get_model_access_groups.return_value = {} - class MockUserRepository: - def __init__(self, prisma_client): - self.table = MagicMock() - self.table.find_unique = AsyncMock(return_value=MagicMock(model_dump=lambda: {"user_id": "user-1", "teams": []})) - - monkeypatch.setattr(ps, "UserRepository", MockUserRepository) monkeypatch.setattr(ps, "user_model", None) monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) @@ -320,87 +314,3 @@ def __init__(self, prisma_client): resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] - - -@pytest.mark.asyncio -async def test_model_info_v1_missing_db_user_returns_deployments(monkeypatch): - """Keys with user_id but no DB row must not lose their model list.""" - deployment = { - "model_name": "gpt-4", - "litellm_params": {"model": "gpt-4"}, - "model_info": {"id": "global-id-1", "db_model": False}, - } - router = MagicMock() - router.model_list = [deployment] - router.get_model_names.return_value = ["gpt-4"] - router.get_model_access_groups.return_value = {} - - class MockUserRepository: - def __init__(self, prisma_client): - self.table = MagicMock() - self.table.find_unique = AsyncMock(return_value=None) - - get_team_access = AsyncMock() - monkeypatch.setattr(ps, "UserRepository", MockUserRepository) - monkeypatch.setattr(ps, "user_model", None) - monkeypatch.setattr(ps, "llm_model_list", router.model_list) - monkeypatch.setattr(ps, "llm_router", router) - monkeypatch.setattr(ps, "prisma_client", MagicMock()) - monkeypatch.setattr(ps, "get_all_team_and_direct_access_models", get_team_access) - monkeypatch.setattr( - ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model - ) - import litellm.proxy.agent_endpoints.model_list_helpers as mlh - - monkeypatch.setattr( - mlh, - "append_agents_to_model_info", - AsyncMock(side_effect=lambda models, **kw: models), - ) - - caller = UserAPIKeyAuth( - user_id="missing-user", - user_role=LitellmUserRoles.INTERNAL_USER, - models=[], - team_models=[], - ) - resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) - - assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] - get_team_access.assert_not_called() - - -@pytest.mark.asyncio -async def test_get_all_team_and_direct_access_models_missing_user_skips_filter( - monkeypatch, -): - """Defense in depth: unresolved user_id must not empty the model list.""" - models = [ - { - "model_name": "gpt-4", - "litellm_params": {"model": "gpt-4"}, - "model_info": {"id": "global-id-1"}, - } - ] - - class MockUserRepository: - def __init__(self, prisma_client): - self.table = MagicMock() - self.table.find_unique = AsyncMock(return_value=None) - - monkeypatch.setattr(ps, "UserRepository", MockUserRepository) - - caller = UserAPIKeyAuth( - user_id="missing-user", - user_role=LitellmUserRoles.INTERNAL_USER, - models=[], - team_models=[], - ) - result = await ps.get_all_team_and_direct_access_models( - user_api_key_dict=caller, - prisma_client=MagicMock(), - llm_router=MagicMock(), - all_models=models, - ) - - assert result == models From 952f16a22af9a7f6397749b61f5a05c74b1c0933 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 10 Jun 2026 18:09:15 +0530 Subject: [PATCH 5/7] fix(proxy): restore legacy v1 model access filtering Keep /v1/model/info on key/team allowlists instead of DB team-membership filtering, while still listing router deployments for team-scoped models. Co-authored-by: Cursor --- litellm/proxy/proxy_server.py | 20 ------ .../test_team_model_name_translation.py | 63 ++----------------- 2 files changed, 6 insertions(+), 77 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index a29f70779513..6642fe801636 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -12450,16 +12450,6 @@ def _filter_v1_model_info_deployments( ] -def _should_apply_v1_team_access_filter( - user_api_key_dict: UserAPIKeyAuth, -) -> bool: - """Team membership filtering requires a resolvable user or admin role.""" - return ( - user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN - or user_api_key_dict.user_id is not None - ) - - def _translate_model_name_for_response(model: dict) -> dict: """For team-scoped DB rows, replace `model_name` with the public name in `model_info.team_public_model_name` before returning. The DB column @@ -12639,16 +12629,6 @@ async def model_info_v1( # noqa: PLR0915 llm_router=llm_router, ) - if prisma_client is not None and _should_apply_v1_team_access_filter( - user_api_key_dict=user_api_key_dict - ): - all_models = await get_all_team_and_direct_access_models( - user_api_key_dict=user_api_key_dict, - prisma_client=prisma_client, - llm_router=llm_router, - all_models=all_models, - ) - all_models = _filter_v1_model_info_deployments( all_models=all_models, allowed_model_names=allowed_model_names, diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index df9b45fd29d6..c6a15eb6d468 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -152,8 +152,7 @@ async def test_model_info_v2_translates_team_model_name(monkeypatch): @pytest.mark.asyncio async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): """/v1/model/info list path (no litellm_model_id) must include team-scoped - deployments from the router model list and surface the public name plus - access_via_team_ids (#28382).""" + deployments from the router model list and surface the public name (#28382).""" team_row = _team_row() global_row = { "model_name": "gpt-4o", @@ -168,27 +167,6 @@ async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): monkeypatch.setattr(ps, "user_model", None) monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) - monkeypatch.setattr(ps, "prisma_client", MagicMock()) - monkeypatch.setattr( - ps, - "get_all_team_and_direct_access_models", - AsyncMock( - side_effect=lambda all_models, **kwargs: [ - { - **m, - "model_info": { - **m.get("model_info", {}), - **( - {"access_via_team_ids": ["team-abc-123"]} - if m.get("model_info", {}).get("team_id") - else {"direct_access": True} - ), - }, - } - for m in all_models - ] - ), - ) monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) @@ -208,15 +186,11 @@ async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): names = [m["model_name"] for m in resp["data"]] assert "team-claude-sonnet" in names assert "model_name_team-abc-123_4a6b8" not in names - team_model = next( - m for m in resp["data"] if m["model_name"] == "team-claude-sonnet" - ) - assert team_model["model_info"]["access_via_team_ids"] == ["team-abc-123"] @pytest.mark.asyncio -async def test_model_info_v1_no_user_id_with_db_skips_team_access_filter(monkeypatch): - """Service/CI keys without user_id must not hit the team-membership filter.""" +async def test_model_info_v1_unrestricted_key_returns_all_deployments(monkeypatch): + """Unrestricted keys must see all router deployments (legacy v1 access logic).""" deployment = { "model_name": "gpt-4", "litellm_params": {"model": "gpt-4"}, @@ -227,12 +201,9 @@ async def test_model_info_v1_no_user_id_with_db_skips_team_access_filter(monkeyp router.get_model_names.return_value = ["gpt-4"] router.get_model_access_groups.return_value = {} - get_team_access = AsyncMock() monkeypatch.setattr(ps, "user_model", None) monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) - monkeypatch.setattr(ps, "prisma_client", MagicMock()) - monkeypatch.setattr(ps, "get_all_team_and_direct_access_models", get_team_access) monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) @@ -245,7 +216,7 @@ async def test_model_info_v1_no_user_id_with_db_skips_team_access_filter(monkeyp ) caller = UserAPIKeyAuth( - user_id=None, + user_id="user-1", user_role=LitellmUserRoles.INTERNAL_USER, models=[], team_models=[], @@ -253,12 +224,11 @@ async def test_model_info_v1_no_user_id_with_db_skips_team_access_filter(monkeyp resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] - get_team_access.assert_not_called() @pytest.mark.asyncio -async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkeypatch): - """Key-level model allowlists must apply after DB team-access enrichment.""" +async def test_model_info_v1_restricted_key_filters_deployments(monkeypatch): + """Key-level model allowlists must filter router deployments.""" team_row = _team_row() global_row = { "model_name": "gpt-4", @@ -273,27 +243,6 @@ async def test_model_info_v1_restricted_key_filters_after_team_enrichment(monkey monkeypatch.setattr(ps, "user_model", None) monkeypatch.setattr(ps, "llm_model_list", router.model_list) monkeypatch.setattr(ps, "llm_router", router) - monkeypatch.setattr(ps, "prisma_client", MagicMock()) - monkeypatch.setattr( - ps, - "get_all_team_and_direct_access_models", - AsyncMock( - side_effect=lambda all_models, **kwargs: [ - { - **m, - "model_info": { - **m.get("model_info", {}), - **( - {"access_via_team_ids": ["team-abc-123"]} - if m.get("model_info", {}).get("team_id") - else {"direct_access": True} - ), - }, - } - for m in all_models - ] - ), - ) monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) From 5f701a475c223a3f6cde046609271b0aa01a09fa Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 10 Jun 2026 20:16:34 +0000 Subject: [PATCH 6/7] fix(proxy): drop A2A agent entries from public /v1/model/info list --- litellm/proxy/proxy_server.py | 9 -------- .../test_team_model_name_translation.py | 21 ------------------- 2 files changed, 30 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 6642fe801636..c6a67774d146 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -12641,15 +12641,6 @@ async def model_info_v1( # noqa: PLR0915 for model in all_models ] - from litellm.proxy.agent_endpoints.model_list_helpers import ( - append_agents_to_model_info, - ) - - all_models = await append_agents_to_model_info( - models=all_models, - user_api_key_dict=user_api_key_dict, - ) - verbose_proxy_logger.debug("all_models: %s", all_models) return {"data": all_models} diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index c6a15eb6d468..570e5b682cf2 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -170,13 +170,6 @@ async def test_model_info_v1_list_path_translates_team_model_name(monkeypatch): monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) - import litellm.proxy.agent_endpoints.model_list_helpers as mlh - - monkeypatch.setattr( - mlh, - "append_agents_to_model_info", - AsyncMock(side_effect=lambda models, **kw: models), - ) admin = UserAPIKeyAuth( user_id="u", user_role=LitellmUserRoles.PROXY_ADMIN, team_models=[] @@ -207,13 +200,6 @@ async def test_model_info_v1_unrestricted_key_returns_all_deployments(monkeypatc monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) - import litellm.proxy.agent_endpoints.model_list_helpers as mlh - - monkeypatch.setattr( - mlh, - "append_agents_to_model_info", - AsyncMock(side_effect=lambda models, **kw: models), - ) caller = UserAPIKeyAuth( user_id="user-1", @@ -246,13 +232,6 @@ async def test_model_info_v1_restricted_key_filters_deployments(monkeypatch): monkeypatch.setattr( ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model ) - import litellm.proxy.agent_endpoints.model_list_helpers as mlh - - monkeypatch.setattr( - mlh, - "append_agents_to_model_info", - AsyncMock(side_effect=lambda models, **kw: models), - ) caller = UserAPIKeyAuth( user_id="user-1", From 2e0a32ba9f45aaac2e943b1f3d688b9fb706bfdb Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 10 Jun 2026 20:45:37 +0000 Subject: [PATCH 7/7] fix(proxy): scope team BYOK rows on /v1/model/info to caller's teams Listing the full router model_list let any authenticated key without explicit model restrictions enumerate other teams' BYOK deployments (public name, team_id, api_base) via /v1/model/info. Reuse the existing _get_caller_byok_team_scope check so non-admin callers only see global deployments plus their own team's BYOK rows; admins keep the full view. --- litellm/proxy/proxy_server.py | 41 +++++-- .../test_team_model_name_translation.py | 101 ++++++++++++++++++ 2 files changed, 133 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index c6a67774d146..2fb53c8d76cb 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -11115,6 +11115,22 @@ async def _get_caller_byok_team_scope( return set(user_row.teams or []) +def _byok_row_outside_caller_teams( + model_info_dict: Dict[str, Any], allowed_team_ids: Optional[Set[str]] +) -> bool: + """Whether a team BYOK row belongs to a team the caller is not a member of. + + `team_id` is only set on team BYOK rows; non-team rows fall through + unaffected. `allowed_team_ids is None` means no scoping (e.g. admins). + """ + if allowed_team_ids is None: + return False + team_id = model_info_dict.get("team_id") + if team_id is None: + return False + return team_id not in allowed_team_ids + + # Hard cap on rows the DB-side BYOK search may pull when results need to be # sorted across the full match set. Without this, an authenticated caller # can hit `/v2/model/info?search=&sortBy=` and force the @@ -11236,15 +11252,7 @@ async def _apply_search_filter_to_models( ) def _is_byok_outside_caller_teams(model_info_dict: Dict[str, Any]) -> bool: - # `team_id` is only set on team BYOK rows. Non-team rows fall - # through unaffected — they are gated by other paths (router - # membership, direct_access, include_team_models). - if allowed_team_ids is None: - return False - team_id = model_info_dict.get("team_id") - if team_id is None: - return False - return team_id not in allowed_team_ids + return _byok_row_outside_caller_teams(model_info_dict, allowed_team_ids) def _model_matches_search(m: Dict[str, Any]) -> bool: # Team BYOK models persist an internal `model_name` @@ -12634,6 +12642,21 @@ async def model_info_v1( # noqa: PLR0915 allowed_model_names=allowed_model_names, ) + # Team BYOK deployments carry an internal routing key and other teams' + # public name/team_id/api_base; drop the ones the caller cannot access so + # listing the full router model_list does not leak cross-team metadata. + allowed_team_ids = await _get_caller_byok_team_scope( + user_api_key_dict=user_api_key_dict, + prisma_client=prisma_client, + ) + all_models = [ + model + for model in all_models + if not _byok_row_outside_caller_teams( + model.get("model_info") or {}, allowed_team_ids + ) + ] + all_models = [ _translate_model_name_for_response( _enrich_model_info_with_litellm_data(model=model, llm_router=llm_router) diff --git a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py index 570e5b682cf2..9757999c85ed 100644 --- a/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py +++ b/tests/test_litellm/proxy/proxy_server/test_team_model_name_translation.py @@ -242,3 +242,104 @@ async def test_model_info_v1_restricted_key_filters_deployments(monkeypatch): resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) assert [m["model_name"] for m in resp["data"]] == ["gpt-4"] + + +def _other_team_row() -> dict: + return { + "model_name": "model_name_team-other_9f2c1", + "litellm_params": { + "model": "azure/gpt-5.2-low-rpm-testing", + "api_base": "https://team-other-private.example.com", + }, + "model_info": { + "id": "byok-id-other", + "team_id": "team-other", + "team_public_model_name": "team-claude-sonnet", + "db_model": True, + }, + } + + +@pytest.mark.asyncio +async def test_model_info_v1_unrestricted_key_hides_other_team_byok(monkeypatch): + """Unrestricted non-admin keys must not enumerate other teams' BYOK + deployments, but must still see global models and their own team's.""" + team_row = _team_row() + other_team_row = _other_team_row() + global_row = { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1", "db_model": False}, + } + router = MagicMock() + router.model_list = [team_row, other_team_row, global_row] + router.get_model_names.return_value = ["gpt-4"] + router.get_model_access_groups.return_value = {} + + prisma_client = MagicMock() + caller_user_row = MagicMock() + caller_user_row.teams = ["team-abc-123"] + prisma_client.db.litellm_usertable.find_unique = AsyncMock( + return_value=caller_user_row + ) + + monkeypatch.setattr(ps, "user_model", None) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) + monkeypatch.setattr(ps, "llm_router", router) + monkeypatch.setattr(ps, "prisma_client", prisma_client) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + + caller = UserAPIKeyAuth( + user_id="user-1", + user_role=LitellmUserRoles.INTERNAL_USER, + models=[], + team_models=[], + ) + resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) + + returned_ids = {m["model_info"]["id"] for m in resp["data"]} + assert returned_ids == {"global-id-1", "byok-id-1"} + assert "byok-id-other" not in returned_ids + names = [m["model_name"] for m in resp["data"]] + assert "team-claude-sonnet" in names + assert "gpt-4" in names + + +@pytest.mark.asyncio +async def test_model_info_v1_service_key_hides_all_team_byok(monkeypatch): + """A key without a resolvable user (e.g. CI/service token) sees only + global deployments, never any team-scoped BYOK rows.""" + team_row = _team_row() + other_team_row = _other_team_row() + global_row = { + "model_name": "gpt-4", + "litellm_params": {"model": "gpt-4"}, + "model_info": {"id": "global-id-1", "db_model": False}, + } + router = MagicMock() + router.model_list = [team_row, other_team_row, global_row] + router.get_model_names.return_value = ["gpt-4"] + router.get_model_access_groups.return_value = {} + + prisma_client = MagicMock() + + monkeypatch.setattr(ps, "user_model", None) + monkeypatch.setattr(ps, "llm_model_list", router.model_list) + monkeypatch.setattr(ps, "llm_router", router) + monkeypatch.setattr(ps, "prisma_client", prisma_client) + monkeypatch.setattr( + ps, "_enrich_model_info_with_litellm_data", lambda model, **kw: model + ) + + caller = UserAPIKeyAuth( + user_id=None, + user_role=LitellmUserRoles.INTERNAL_USER, + team_id="team-abc-123", + models=[], + team_models=[], + ) + resp = await ps.model_info_v1(user_api_key_dict=caller, litellm_model_id=None) + + assert [m["model_info"]["id"] for m in resp["data"]] == ["global-id-1"]