From e8fccb5445b02913bb24d3d044ce613ebe130856 Mon Sep 17 00:00:00 2001 From: Milan Date: Wed, 29 Apr 2026 00:44:23 +0300 Subject: [PATCH] perf(proxy): reuse caller_user in _authorize_and_filter_teams self-queries Avoid a second get_user_object when listing own teams for /team/list; caller_user was already loaded for org-admin detection. --- .../management_endpoints/team_endpoints.py | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py index 5de547fb306..09cdde8b388 100644 --- a/litellm/proxy/management_endpoints/team_endpoints.py +++ b/litellm/proxy/management_endpoints/team_endpoints.py @@ -4145,6 +4145,7 @@ async def _authorize_and_filter_teams( """ is_proxy_admin = _user_has_admin_view(user_api_key_dict) allowed_org_ids: Optional[List[str]] = None + caller_user = None if not is_proxy_admin: # Check if user is an org admin (even for own queries, so they see org teams) @@ -4203,24 +4204,34 @@ async def _authorize_and_filter_teams( # Regular users are scoped to the canonical team ids on their user row. # This mirrors /v2/team/list and avoids exposing teams from stale # members_with_roles entries. - try: - target_user = await get_user_object( - user_id=user_id, - prisma_client=prisma_client, - user_api_key_cache=user_api_key_cache, - user_id_upsert=False, - proxy_logging_obj=proxy_logging_obj, - ) - except ValueError: - raise HTTPException( - status_code=404, - detail={"error": f"User not found, passed user_id={user_id}"}, - ) - if target_user is None: - raise HTTPException( - status_code=404, - detail={"error": f"User not found, passed user_id={user_id}"}, - ) + # + # Reuse caller_user when listing own teams — already loaded above for + # org-admin detection — to avoid a second get_user_object round-trip. + if ( + caller_user is not None + and user_api_key_dict.user_id is not None + and user_id == user_api_key_dict.user_id + ): + target_user = caller_user + else: + try: + target_user = await get_user_object( + user_id=user_id, + prisma_client=prisma_client, + user_api_key_cache=user_api_key_cache, + user_id_upsert=False, + proxy_logging_obj=proxy_logging_obj, + ) + except ValueError: + raise HTTPException( + status_code=404, + detail={"error": f"User not found, passed user_id={user_id}"}, + ) + if target_user is None: + raise HTTPException( + status_code=404, + detail={"error": f"User not found, passed user_id={user_id}"}, + ) user_team_ids = target_user.teams or [] if not user_team_ids: return []