Skip to content

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]

fix(team_endpoints): keep an email delete off the namesakes that never had the team

milan84fad12f8546
diff --git a/litellm/proxy/management_endpoints/team_endpoints.py b/litellm/proxy/management_endpoints/team_endpoints.py
index 7112c6ec40f..bbd9ae1fccd 100644
--- a/litellm/proxy/management_endpoints/team_endpoints.py
+++ b/litellm/proxy/management_endpoints/team_endpoints.py
@@ -3319,15 +3319,16 @@ async def team_member_delete(
member_tx: Final[_MemberDeleteTx] = tx
existing_user_rows: Final = await member_tx.litellm_usertable.find_many(where=key_val)
- # Also clean up any existing team membership rows for this user and team
- user_ids_to_delete: Final = addressed_user_ids.union(
- user.user_id for user in existing_user_rows if user.user_id
- )
-
# A user row can outlive its roster entry, and until the team is off user.teams the user
# still sees it and still fails key creation against it, so removal has to clear it too
stale_user_rows: Final = tuple(user for user in existing_user_rows if data.team_id in user.teams)
+ # Also clean up any existing team membership rows for this user and team. An email can
+ # match several user rows, so with no roster entry to name the member, only the rows
+ # actually carrying the team are the ones this request is allowed to touch
+ cleanup_user_rows: Final = existing_user_rows if removed_team_members else stale_user_rows
+ user_ids_to_delete: Final = addressed_user_ids.union(user.user_id for user in cleanup_user_rows if user.user_id)
+
if not removed_team_members and not stale_user_rows:
raise HTTPException(status_code=400, detail={"error": "User not found in team"})
diff --git a/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
index 973a37ccc06..e54326d1afd 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_team_endpoints.py
@@ -4995,6 +4995,82 @@ async def test_team_member_delete_leaves_a_bystander_named_by_a_conflicting_user
)
+async def test_team_member_delete_by_email_only_touches_the_row_carrying_the_stale_team(
+ mock_db_client, mock_admin_auth
+):
+ """
+ user_email is not unique, so an email delete against an empty roster can match several user
+ rows. Only the row that actually carries the team is stale; the namesake keeps its team, its
+ membership row and its keys.
+ """
+ from litellm.proxy._types import TeamMemberDeleteRequest
+ from litellm.proxy.management_endpoints.team_endpoints import team_member_delete
+
+ test_team_id = "team-del-shared-email-123"
+ stale_user_id = "user-del-shared-email-stale"
+ namesake_user_id = "user-del-shared-email-namesake"
+ shared_email = "[email protected]"
+
+ mock_team_row = MagicMock()
+ mock_team_row.model_dump.return_value = {
+ "team_id": test_team_id,
+ "members_with_roles": [],
+ "team_member_permissions": [],
+ "metadata": {},
+ "models": [],
+ "spend": 0.0,
+ }
+ mock_db_client.db.litellm_teamtable.find_unique = AsyncMock(
+ return_value=mock_team_row
+ )
+ mock_db_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_team_row)
+
+ stale_user_row = MagicMock()
+ stale_user_row.user_id = stale_user_id
+ stale_user_row.user_email = shared_email
+ stale_user_row.teams = [test_team_id]
+
+ namesake_user_row = MagicMock()
+ namesake_user_row.user_id = namesake_user_id
+ namesake_user_row.user_email = shared_email
+ namesake_user_row.teams = ["other-team"]
+
+ mock_db_client.db.litellm_usertable.find_many = AsyncMock(
+ return_value=[stale_user_row, namesake_user_row]
+ )
+ mock_db_client.db.litellm_usertable.update = AsyncMock(return_value=MagicMock())
+
+ mock_db_client.db.litellm_teammembership = MagicMock()
+ mock_db_client.db.litellm_teammembership.delete_many = AsyncMock(
+ return_value=MagicMock()
+ )
+
+ mock_db_client.db.litellm_verificationtoken = MagicMock()
+ mock_db_client.db.litellm_verificationtoken.find_many = AsyncMock(return_value=[])
+ mock_db_client.db.litellm_verificationtoken.delete_many = AsyncMock(
+ return_value=MagicMock()
+ )
+
+ _wire_member_delete_tx(mock_db_client)
+
+ await team_member_delete(
+ data=TeamMemberDeleteRequest(team_id=test_team_id, user_email=shared_email),
+ user_api_key_dict=mock_admin_auth,
+ )
+
+ mock_db_client.db.litellm_usertable.update.assert_awaited_once_with(
+ where={"user_id": stale_user_id},
+ data={"teams": {"set": []}},
+ )
+ mock_db_client.db.litellm_teammembership.delete_many.assert_awaited_once_with(
+ where={"team_id": test_team_id, "user_id": stale_user_id}
+ )
+ mock_db_client.db.litellm_verificationtoken.delete_many.assert_awaited_once_with(
+ where={"user_id": {"in": [stale_user_id]}, "team_id": test_team_id}
+ )
+
+
class _InjectedMemberDeleteFailure(Exception):
pass