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
perf(scim): batch group member identity lookups into one query per push (#46412)
Yassin Kortam461cbea6e6f6
diff --git a/litellm/constants.py b/litellm/constants.pyindex bbf6b8a4f04..6b34491efff 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -76,6 +76,9 @@ ROUTER_SETTINGS_MANAGED_OUTSIDE_CONFIG: Final[frozenset[str]] = frozenset(})DEFAULT_BATCH_SIZE: Final = get_env_int("DEFAULT_BATCH_SIZE", 512)+SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZE: Final = get_env_int_in_range(+ "SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZE", 1000, 1, 10_000+)DEFAULT_FLUSH_INTERVAL_SECONDS: Final = get_env_int("DEFAULT_FLUSH_INTERVAL_SECONDS", 5)CLICKHOUSE_BATCH_SIZE: Final = get_env_int("CLICKHOUSE_BATCH_SIZE", 10_000)CLICKHOUSE_FLUSH_INTERVAL_SECONDS: Final = get_env_float("CLICKHOUSE_FLUSH_INTERVAL_SECONDS", 1.0)diff --git a/litellm/proxy/management_endpoints/scim/scim_v2.py b/litellm/proxy/management_endpoints/scim/scim_v2.pyindex 04886072856..a4ffa5cfd4d 100644--- a/litellm/proxy/management_endpoints/scim/scim_v2.py+++ b/litellm/proxy/management_endpoints/scim/scim_v2.py@@ -9,9 +9,9 @@ from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequencefrom copy import deepcopyfrom dataclasses import dataclassfrom functools import partial-from itertools import chain+from itertools import chain, groupbyfrom types import MappingProxyType-from typing import TYPE_CHECKING, Final, NamedTuple, Protocol, overload+from typing import TYPE_CHECKING, Final, NamedTuple, Protocol, TypeAlias, overloadfrom fastapi import (APIRouter,@@ -29,6 +29,7 @@ from typing_extensions import ReadOnly, TypedDict, assert_neverimport litellmfrom litellm._logging import verbose_proxy_loggerfrom litellm._uuid import uuid+from litellm.constants import SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZEfrom litellm.litellm_core_utils.safe_json_dumps import safe_dumpsfrom litellm.models.user import SCIMPlaceholderfrom litellm.proxy._types import (@@ -70,6 +71,7 @@ from litellm.proxy.utils import ( # noqa: F401 # legacy module exportshandle_exception_on_proxy,premium_user_check,)+from litellm.repositories.chunked_in import find_many_infrom litellm.repositories.table_repositories import (InvitationLinkRepository,OrganizationMembershipRepository,@@ -565,6 +567,111 @@ class _CaseInsensitiveMatch(TypedDict):mode: ReadOnly[str]+_InFilter: Final = TypedDict("_InFilter", {"in": ReadOnly[Sequence[str]]})+_CaseInsensitiveInFilter: Final = TypedDict(+ "_CaseInsensitiveInFilter", {"in": ReadOnly[Sequence[str]], "mode": ReadOnly[str]}+)+++class _UserIdIn(TypedDict):+ user_id: ReadOnly[_InFilter]+++class _SsoUserIdIn(TypedDict):+ sso_user_id: ReadOnly[_InFilter]+++class _UserEmailIn(TypedDict):+ user_email: ReadOnly[_CaseInsensitiveInFilter]+++_IdentityRowsClause: TypeAlias = _UserIdIn | _SsoUserIdIn | _UserEmailIn+++class _IdentityRowsWhere(TypedDict):+ OR: ReadOnly[Sequence[_IdentityRowsClause]]+++@dataclass(frozen=True, slots=True)+class _IdentityIndex:+ user_ids: frozenset[str]+ by_sso_user_id: Mapping[str, tuple[str, ...]]+ by_email: Mapping[str, tuple[str, ...]]+++async def _identity_rows_for_chunk(chunk: Sequence[str], prisma_client: PrismaClient) -> tuple[LiteLLM_UserTable, ...]:+ subjects: Final = list(dict.fromkeys(value.strip() for value in chunk))+ where: Final[_IdentityRowsWhere] = {+ "OR": [+ {"user_id": {"in": list(chunk)}}, # bounded-ok: chunk cap is 10,000+ {"sso_user_id": {"in": subjects}}, # bounded-ok: subjects are chunk-bounded+ {"user_email": {"in": subjects, "mode": "insensitive"}}, # bounded-ok: subjects are chunk-bounded+ ]+ }+ return tuple(await _table(UserRepository(prisma_client)).find_many(where=where))+++async def _identity_rows_for_values(+ values: Sequence[str], prisma_client: PrismaClient+) -> tuple[LiteLLM_UserTable, ...]:+ unique_values: Final = tuple(dict.fromkeys(values))+ if not unique_values:+ return ()++ batch_size: Final = SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZE+ chunks: Final = tuple(+ unique_values[start : start + batch_size] for start in range(0, len(unique_values), batch_size)+ )+ chunk_rows: Final = tuple([await _identity_rows_for_chunk(chunk, prisma_client) for chunk in chunks])+ rows: Final = tuple(chain.from_iterable(chunk_rows))+ return tuple({row.user_id: row for row in rows}.values())+++def _identity_index(rows: Sequence[LiteLLM_UserTable]) -> _IdentityIndex:+ sso_entries: Final = tuple(sorted((row.sso_user_id, row.user_id) for row in rows if row.sso_user_id is not None))+ email_entries: Final = tuple(+ sorted((row.user_email.lower(), row.user_id) for row in rows if row.user_email is not None)+ )+ by_sso_user_id: Final = MappingProxyType(+ {+ subject: tuple(sorted({user_id for _, user_id in matches}))+ for subject, matches in groupby(sso_entries, key=lambda entry: entry[0])+ }+ )+ by_email: Final = MappingProxyType(+ {+ email: tuple(sorted({user_id for _, user_id in matches}))+ for email, matches in groupby(email_entries, key=lambda entry: entry[0])+ }+ )+ return _IdentityIndex(+ user_ids=frozenset(row.user_id for row in rows),+ by_sso_user_id=by_sso_user_id,+ by_email=by_email,+ )+++def _users_named_by(value: str, index: _IdentityIndex) -> tuple[str, ...]:+ subject: Final = value.strip()+ named: Final = frozenset(+ chain(+ index.by_sso_user_id.get(subject, ()),+ index.by_email.get(subject.lower(), ()),+ )+ )+ return tuple(sorted(named))+++def _accounts_named_by(value: str, index: _IdentityIndex) -> tuple[str, ...]:+ named: Final = frozenset(+ chain(+ _users_named_by(value, index),+ (value,) if value in index.user_ids else (),+ )+ )+ return tuple(sorted(named))++async def _users_named_by_member_value(value: str, prisma_client: PrismaClient, *, take: int | None = 2) -> tuple[str, ...]:@@ -597,50 +704,35 @@ async def _users_named_by_member_value(return tuple(dict.fromkeys(row.user_id for row in rows))-async def _accounts_named_by_member_value(value: str, prisma_client: PrismaClient) -> tuple[str, ...]:- """Every user id this member value names, by user id, SSO identity or email.+async def _scim_team_ids_among(values: Sequence[str], prisma_client: PrismaClient) -> frozenset[str]:+ unique_values: Final = tuple(dict.fromkeys(values))+ if not unique_values:+ return frozenset()- Classification needs to know whether the value is one account's ``user_id`` and- whether it names any other account, so all three fields are read in one pass. The- id is compared exactly and unstripped, as a primary key lookup would; the- identities compare as ``_users_named_by_member_value`` describes. Two rows are- enough to tell one account from several, so the read stops there. Only a full- read that lacks the row keyed by the value leaves that row's existence open, and- only then is the id read on its own.- """- subject: Final = value.strip()- email: Final[_CaseInsensitiveMatch] = {"equals": subject, "mode": "insensitive"}- users: Final = _table(UserRepository(prisma_client))- rows: Final = await users.find_many(- where={- "OR": [- {"user_id": value},- {"sso_user_id": subject},- {"user_email": email},- ],- },- take=2,+ teams: Final = await find_many_in(+ _table(TeamRepository(prisma_client)),+ "team_id",+ unique_values,+ chunk_size=SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZE,)- named: Final = tuple(dict.fromkeys(row.user_id for row in rows))- if len(named) < 2 or value in named:- return named- keyed: Final = await users.find_unique(where={"user_id": value})- return named if keyed is None else (value, *named)+ return frozenset(team.team_id for team in teams if _team_metadata_has_scim_provenance(team.metadata))-async def _classify_group_member(member: SCIMMember, prisma_client: PrismaClient) -> _ClassifiedGroupMember:+def _classify_group_member(+ member: SCIMMember, named: tuple[str, ...], scim_team_ids: frozenset[str]+) -> _ClassifiedGroupMember:"""Decide what a single SCIM group member refers to.A LiteLLM team only holds users, so a member is dropped when it declares a typeother than ``User`` or when its id names an existing team. Both of those checks- are placed around the user lookup rather than before it, because the id of a+ are placed around identity resolution rather than before it, because the id of areal user is the one thing that outranks them:- ``"type": "Group"`` (what Entra sends for a nested group) is dropped without- a lookup. This bug provisioned nested group GUIDs as users, so those rows+ identity resolution. This bug provisioned nested group GUIDs as users, so those rowsexist in the wild and would otherwise resolve as members all over again.- - any other unrecognized type is dropped only after the user lookup misses.+ - any other unrecognized type is dropped only after identity resolution misses.Clients do send non-canonical types on real members (RFC 7643 defines``direct`` for ``User.groups``), and dropping a live user over one wouldrevoke that user's team access on the next full sync.@@ -650,8 +742,8 @@ async def _classify_group_member(member: SCIMMember, prisma_client: PrismaClienteven if some team happens to share the id, and a team created here ratherthan through SCIM is not evidence of anything about the member.- When those checks miss on an otherwise user-shaped member, its value is looked- up as an SSO identity or an email, and a match resolves to that user's+ When those checks miss on an otherwise user-shaped member, its value is matched+ against SSO identities or emails, and a match resolves to that user's``user_id``. A value that names more than one account is ambiguous rather thanunknown: it names a real person we cannot identify, so it is neither guessed atnor provisioned.@@ -670,7 +762,6 @@ async def _classify_group_member(member: SCIMMember, prisma_client: PrismaClientif member_type == "group":return _SkippedGroupMember(value=value, reason="nested_group")- named: Final = await _accounts_named_by_member_value(value, prisma_client)if value in named:shared_with: Final = tuple(other for other in named if other != value)if shared_with:@@ -687,10 +778,8 @@ async def _classify_group_member(member: SCIMMember, prisma_client: PrismaClientif member_type is not None and member_type != "user":return _SkippedGroupMember(value=value, reason="non_user_type")- if member_type is None:- team: Final = await _table(TeamRepository(prisma_client)).find_unique(where={"team_id": value})- if team is not None and _team_metadata_has_scim_provenance(team.metadata):- return _SkippedGroupMember(value=value, reason="existing_team")+ if member_type is None and value in scim_team_ids:+ return _SkippedGroupMember(value=value, reason="existing_team")if len(named) == 1:verbose_proxy_logger.info(@@ -837,16 +926,10 @@ async def _member_ids_to_drop(HTTPException: 400 when a member id names more than one current member."""written: Final = frozenset(_member_value(member) for member in members)+ rows: Final = await _identity_rows_for_values(sorted(written), prisma_client)+ index: Final = _identity_index(rows)matched: Final = tuple(- [- (- value,- _roster_entries_named_by(- value, roster, await _users_named_by_member_value(value, prisma_client, take=None)- ),- )- for value in sorted(written)- ]+ (value, _roster_entries_named_by(value, roster, _users_named_by(value, index))) for value in sorted(written))undecidable: Final = tuple(value for value, entries in matched if len(entries) > 1)if undecidable:@@ -881,7 +964,22 @@ async def _resolve_group_member_ids(other than a user. 500 when a member's user row can neither be created norfound."""- classified: Final = tuple([await _classify_group_member(member, prisma_client) for member in members])+ values: Final = tuple(_member_value(member) for member in members)+ identity_values: Final = tuple(+ value for member, value in zip(members, values) if _normalized_member_type(member) != "group"+ )+ rows: Final = await _identity_rows_for_values(identity_values, prisma_client)+ index: Final = _identity_index(rows)+ named_by: Final = MappingProxyType({value: _accounts_named_by(value, index) for value in dict.fromkeys(values)})+ team_candidates: Final = tuple(+ value+ for member, value in zip(members, values)+ if _normalized_member_type(member) is None and value not in named_by[value]+ )+ scim_teams: Final = await _scim_team_ids_among(team_candidates, prisma_client)+ classified: Final = tuple(+ _classify_group_member(member, named_by[value], scim_teams) for member, value in zip(members, values)+ )partition: Final = _partition_classified_members(classified)for skipped in partition.skipped:diff --git a/tests/unit/proxy/management_endpoints/scim/test_scim_v2_endpoints.py b/tests/unit/proxy/management_endpoints/scim/test_scim_v2_endpoints.pyindex 88b28fffbd3..85b3baed01d 100644--- a/tests/unit/proxy/management_endpoints/scim/test_scim_v2_endpoints.py+++ b/tests/unit/proxy/management_endpoints/scim/test_scim_v2_endpoints.py@@ -5,13 +5,15 @@ from collections.abc import Callable, Mapping, Sequencefrom itertools import chainfrom types import MappingProxyTypefrom typing import Final-from unittest.mock import AsyncMock, MagicMock, call+from unittest.mock import AsyncMock, MagicMock+from unittest.mock import call as mock_callimport pytestfrom fastapi import FastAPI, HTTPExceptionfrom httpx import ASGITransport, AsyncClientfrom pytest_mock import MockerFixture+import litellm.proxy.management_endpoints.scim.scim_v2 as scim_v2_modulefrom litellm.proxy._types import (LiteLLM_TeamTable,LiteLLM_UserTable,@@ -24,6 +26,7 @@ from litellm.proxy._types import (UserAPIKeyAuth,)from litellm.proxy.management_endpoints.scim.scim_v2 import (+ GroupMemberExtractionResult,SCIMRosterSyncError,UserProvisionerHelpers,_apply_group_patch_updates,@@ -32,8 +35,8 @@ from litellm.proxy.management_endpoints.scim.scim_v2 import (_extract_ids_from_path_filter,_handle_group_membership_changes,_handle_team_membership_changes,+ _member_ids_to_drop,_parse_member_entries,- premium_user_check,_process_group_patch_operations,_recompute_scim_member_roles,_resolve_group_member_ids,@@ -48,6 +51,7 @@ from litellm.proxy.management_endpoints.scim.scim_v2 import (patch_group,patch_team_membership,patch_user,+ premium_user_check,scim_router,update_group,update_user,@@ -1616,6 +1620,7 @@ async def test_update_group_metadata_serialization_issue(mocker):# Mock team operationsmock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=mock_existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=mock_updated_team)# Mock user operations@@ -1624,7 +1629,7 @@ async def test_update_group_metadata_serialization_issue(mocker):mock_user.user_email = "[email protected]" # Add proper string value for user_emailmock_user.teams = [group_id]mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mock_user)- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=(mock_user,))mock_prisma_client.db.litellm_usertable.update = AsyncMock(return_value=mock_user)# Mock the _get_prisma_client_or_raise_exception to return our mock@@ -1805,6 +1810,7 @@ async def test_update_group_e2e(mocker):# Mock database operationsmock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock the updated team that gets returned from databaseupdated_team = LiteLLM_TeamTable(@@ -1825,9 +1831,9 @@ async def test_update_group_e2e(mocker):# Mock user validation (all users exist)mock_user = mocker.MagicMock()- mock_user.user_id = "test-user"+ mock_users: Final = tuple(LiteLLM_UserTable(user_id=user_id) for user_id in ("user2", "user3", "user4"))mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mock_user)- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=mock_users)# Mock dependenciesmocker.patch(@@ -1913,13 +1919,20 @@ async def test_update_group_e2e(mocker):def _rows_by_exact_id(user_row: Callable[[Mapping[str, str]], LiteLLM_UserTable | MagicMock | None],) -> Callable[..., tuple[LiteLLM_UserTable | MagicMock, ...]]:- """``find_many`` stand-in for the classifier's cross-field read on a table where a- member value only ever matches as an exact ``user_id``."""+ """``find_many`` stand-in for a table where members match only exact user ids."""def rows(where: Mapping[str, object], take: int | None = None) -> tuple[LiteLLM_UserTable | MagicMock, ...]:clauses: Final = where["OR"]assert isinstance(clauses, list)- found: Final = tuple(user_row(clause) for clause in clauses if "user_id" in clause)+ fields: Final = tuple(next(iter(clause)) for clause in clauses)+ assert fields == ("user_id", "sso_user_id", "user_email"), fields+ user_filter: Final = clauses[0]["user_id"]+ assert isinstance(user_filter, dict)+ assert tuple(user_filter) == ("in",)+ values: Final = user_filter["in"]+ assert isinstance(values, list)+ assert all(isinstance(value, str) for value in values)+ found: Final = tuple(user_row({"user_id": value}) for value in values)return tuple(row for row in found if row is not None)return rows@@ -1970,6 +1983,7 @@ async def test_create_group_with_nonexistent_users_rejects(mocker, monkeypatch):# Mock team operations - team doesn't exist yetmock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock user lookup - only existing-user existsdef mock_user_lookup(where):@@ -2048,6 +2062,7 @@ async def test_update_group_with_nonexistent_users_rejects(mocker, monkeypatch):return mock_existing_team if where["team_id"] == group_id else Nonemock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(side_effect=mock_team_lookup)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock updated team responsemock_updated_team = mocker.MagicMock()@@ -2125,6 +2140,7 @@ async def test_create_group_with_nonexistent_users_creates_when_flag_true(mocker# Mock team operations - team doesn't exist yetmock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock user lookup - only existing-user exists initiallydef mock_user_lookup(where):@@ -2214,6 +2230,7 @@ async def test_extract_group_member_ids_with_flag_true_creates_users(mocker, monmock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock user lookup - only existing-user exists initiallydef mock_user_lookup(where):@@ -2283,6 +2300,7 @@ async def test_extract_group_member_ids_with_flag_false_rejects(mocker, monkeypamock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock user lookup - only existing-user existsdef mock_user_lookup(where):@@ -2347,6 +2365,7 @@ async def test_process_group_patch_operations_with_flag_true_creates_users(mockemock_prisma_client.db.litellm_usertable.find_first = AsyncMock(return_value=None)mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=[])mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Mock user creationcreated_user = NewUserResponse(user_id="new-user-1", key="test-key-1")@@ -2404,6 +2423,7 @@ async def test_process_group_patch_operations_with_flag_false_rejects(mocker, momock_prisma_client.db.litellm_usertable.find_first = AsyncMock(return_value=None)mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=[])mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())# Execute the function - should raise HTTPExceptionwith pytest.raises(HTTPException) as exc_info:@@ -2933,10 +2953,13 @@ async def test_update_group_recomputes_roles_for_changed_members(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=existing_team)mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(+ return_value=tuple(LiteLLM_UserTable(user_id=user_id) for user_id in ("user2", "user3"))+ )mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -2991,10 +3014,13 @@ async def test_patch_group_recomputes_roles_for_changed_members(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=existing_team)mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(+ return_value=(LiteLLM_UserTable(user_id="user1"), LiteLLM_UserTable(user_id="user2"))+ )mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -3220,9 +3246,12 @@ async def test_create_group_recomputes_roles_for_members(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=None)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(+ return_value=(LiteLLM_UserTable(user_id="user1"), LiteLLM_UserTable(user_id="user2"))+ )mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -3277,10 +3306,11 @@ async def test_update_group_rename_recomputes_retained_members(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=existing_team)mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=(LiteLLM_UserTable(user_id="user1"),))mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -3332,10 +3362,13 @@ async def test_patch_group_rename_recomputes_retained_members(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=existing_team)mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(+ return_value=(LiteLLM_UserTable(user_id="user1"), LiteLLM_UserTable(user_id="user2"))+ )mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -4001,69 +4034,80 @@ def _member_resolution_prisma(else ({email: (user_id,) for email, user_id in email_to_user_id.items()} if email_to_user_id else {}))ssos_to_ids: Final[Mapping[str, str]] = dict(sso_user_id_to_user_id) if sso_user_id_to_user_id else {}+ user_ids: Final = tuple(+ dict.fromkeys(chain(users, chain.from_iterable(emails_to_ids.values()), ssos_to_ids.values()))+ )+ user_rows: Final = tuple(+ LiteLLM_UserTable(+ user_id=user_id,+ user_email=next((email for email, ids in emails_to_ids.items() if user_id in ids), None),+ sso_user_id=next((subject for subject, mapped_id in ssos_to_ids.items() if mapped_id == user_id), None),+ )+ for user_id in user_ids+ )def identity_rows(where: Mapping[str, object], take: int | None = None) -> tuple[LiteLLM_UserTable, ...]:- """Stand-in for the cross-field lookup, honouring the comparison mode- production actually asks for per field, so a field that stops folding case, or- starts folding it, fails here instead of passing.-- A caller that must know which accounts match rather than merely how many- passes take=None, so an unbounded read returns every match. The row keyed by- the value comes last, the order a bounded read is least prepared for, since- the database promises no order at all.- """+ """Stand-in for the cross-field lookup with the production comparison modes."""clauses: Final = where["OR"]assert isinstance(clauses, list)fields: Final = tuple(next(iter(clause)) for clause in clauses)assert fields in (("user_id", "sso_user_id", "user_email"), ("sso_user_id", "user_email")), fields- def comparison(clause: Mapping[str, object]) -> tuple[str, bool]:- """The needle and whether production asked for a case-insensitive compare,- read per field so a field that stops folding case fails here."""- criterion = next(iter(clause.values()))+ def values_for(clause: Mapping[str, object], field: str, *, insensitive: bool) -> tuple[str, ...]:+ criterion: Final = clause[field]if isinstance(criterion, str):- return criterion, False+ assert not insensitive+ return (criterion,)assert isinstance(criterion, dict), criterion- return criterion["equals"], criterion.get("mode") == "insensitive"-- by_field: Final = dict(zip(fields, (comparison(clause) for clause in clauses)))- sso_needle, sso_insensitive = by_field["sso_user_id"]- email_needle, email_insensitive = by_field["user_email"]-- def same(stored: str, needle: str, insensitive: bool) -> bool:- return stored.casefold() == needle.casefold() if insensitive else stored == needle-+ if "in" in criterion:+ expected_keys: Final = ("in", "mode") if insensitive else ("in",)+ assert tuple(criterion) == expected_keys+ if insensitive:+ assert criterion["mode"] == "insensitive"+ values: Final = criterion["in"]+ assert isinstance(values, list)+ assert all(isinstance(value, str) for value in values)+ return tuple(values)+ assert insensitive+ assert tuple(criterion) == ("equals", "mode")+ assert criterion["mode"] == "insensitive"+ value: Final = criterion["equals"]+ assert isinstance(value, str)+ return (value,)++ by_field: Final = dict(zip(fields, clauses))+ user_id_values: Final = (+ values_for(by_field["user_id"], "user_id", insensitive=False) if "user_id" in by_field else ()+ )+ sso_values: Final = values_for(by_field["sso_user_id"], "sso_user_id", insensitive=False)+ email_values: Final = values_for(by_field["user_email"], "user_email", insensitive=True)matched: Final = tuple(- chain(- (- user_id- for sso_user_id, user_id in ssos_to_ids.items()- if same(sso_user_id, sso_needle, sso_insensitive)- ),- (- user_id- for email, user_ids in emails_to_ids.items()- if same(email, email_needle, email_insensitive)- for user_id in user_ids- ),- (- user_id- for user_id in users- if "user_id" in by_field and same(user_id, by_field["user_id"][0], by_field["user_id"][1])- ),- )+ row+ for row in user_rows+ if row.user_id in user_id_values+ or row.sso_user_id in sso_values+ or (row.user_email or "").casefold() in {value.casefold() for value in email_values})- found: Final = tuple(dict.fromkeys(matched))- return tuple(LiteLLM_UserTable(user_id=user_id) for user_id in (found[:take] if take else found))+ return matched[:take] if take else matcheddef team_lookup(where: Mapping[str, str]) -> LiteLLM_TeamTable | None:team_id: Final = where["team_id"]return team_row(team_id)+ async def team_rows(where: Mapping[str, object]) -> tuple[LiteLLM_TeamTable, ...]:+ criterion: Final = where["team_id"]+ assert isinstance(criterion, dict)+ assert tuple(criterion) == ("in",)+ team_ids: Final = criterion["in"]+ assert isinstance(team_ids, list)+ assert all(isinstance(team_id, str) for team_id in team_ids)+ return tuple(team for team_id in team_ids if (team := team_row(team_id)) is not None)+prisma_client.db.litellm_usertable.find_first = AsyncMock(return_value=None)prisma_client.db.litellm_usertable.find_many = AsyncMock(side_effect=identity_rows)prisma_client.db.litellm_teamtable = mocker.MagicMock()prisma_client.db.litellm_teamtable.find_unique = AsyncMock(side_effect=team_lookup)+ prisma_client.db.litellm_teamtable.find_many = AsyncMock(side_effect=team_rows)return prisma_client@@ -4620,8 +4664,8 @@ async def test_process_group_patch_team_match_needs_scim_provenance(members_with_roles=[],)prisma_client = _member_resolution_prisma(mocker, users=set(), teams=set())- prisma_client.db.litellm_teamtable.find_unique = AsyncMock(- return_value=LiteLLM_TeamTable(team_id="child-team", metadata=team_metadata)+ prisma_client.db.litellm_teamtable.find_many = AsyncMock(+ return_value=(LiteLLM_TeamTable(team_id="child-team", metadata=team_metadata),))create_user_mock = mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._create_user_if_not_exists",@@ -4895,17 +4939,17 @@ async def test_resolve_group_member_ids_dedupes_repeated_member(mocker, scim_upsassert result.all_member_ids == ["dup-user"]-def _identity_lookup(value: str) -> object:- """The single cross-field lookup the classifier is expected to issue per member."""- return call(+def _identity_lookup(*values: str) -> object:+ unique_values: Final = tuple(dict.fromkeys(values))+ subjects: Final = list(dict.fromkeys(value.strip() for value in unique_values))+ return mock_call(where={"OR": [- {"user_id": value},- {"sso_user_id": value},- {"user_email": {"equals": value, "mode": "insensitive"}},+ {"user_id": {"in": list(unique_values)}},+ {"sso_user_id": {"in": subjects}},+ {"user_email": {"in": subjects, "mode": "insensitive"}},]- },- take=2,+ })@@ -4925,7 +4969,7 @@ async def test_resolve_group_member_ids_matches_sso_user_id(mocker, scim_upsert_)result = await _resolve_group_member_ids(- members=[SCIMMember(value="member-sub")],+ members=[SCIMMember(value=" member-sub ")],created_via="scim_group_membership",prisma_client=prisma_client,)@@ -4934,7 +4978,7 @@ async def test_resolve_group_member_ids_matches_sso_user_id(mocker, scim_upsert_assert result.existing_member_ids == ["sso-user"]assert result.created_users == []assert result.all_member_ids == ["sso-user"]- assert prisma_client.db.litellm_usertable.find_many.await_args_list == [_identity_lookup("member-sub")]+ assert prisma_client.db.litellm_usertable.find_many.await_args_list == [_identity_lookup(" member-sub ")]@pytest.mark.asyncio@@ -5440,13 +5484,10 @@ async def test_resolve_group_member_ids_refuses_a_user_id_that_names_another_acc@pytest.mark.asyncio-async def test_resolve_group_member_ids_reads_the_exact_id_when_two_other_accounts_fill_the_lookup(+async def test_resolve_group_member_ids_detects_all_identity_collisions_with_an_exact_id(mocker, scim_upsert_user_enabled):- """A value that is one account's id and two other accounts' identities fills the- bounded lookup with the other two. The account keyed by the value must still be- found, or the id would lose its precedence and a non-canonical type would skip- a member that names a real user."""+ """A value that names three accounts remains ambiguous, even when one account's id matches exactly."""prisma_client = _member_resolution_prisma(mocker,users={"shared"},@@ -5470,15 +5511,11 @@ async def test_resolve_group_member_ids_reads_the_exact_id_when_two_other_accounassert "shared" in str(exc_info.value.detail)create_user_mock.assert_not_called()assert prisma_client.db.litellm_usertable.find_many.await_args_list == [_identity_lookup("shared")]- prisma_client.db.litellm_usertable.find_unique.assert_awaited_once_with(where={"user_id": "shared"})+ prisma_client.db.litellm_usertable.find_unique.assert_not_awaited()@pytest.mark.asyncio-async def test_resolve_group_member_ids_reads_the_user_table_once_per_member(mocker, scim_upsert_user_enabled):- """Every member costs one read of the user table, however it resolves: by its exact- id (which still outranks a non-canonical type), by identity, as a SCIM team, or not- at all. Looking the exact id up on its own before the identity read doubled the- reads of a push, and the identity read is a scan."""+async def test_resolve_group_member_ids_reads_the_user_table_once_per_push(mocker, scim_upsert_user_enabled):prisma_client = _member_resolution_prisma(mocker,users={"by-id"},@@ -5503,11 +5540,122 @@ async def test_resolve_group_member_ids_reads_the_user_table_once_per_member(mocassert result.all_member_ids == ["by-id", "email-user", "nobody"]prisma_client.db.litellm_usertable.find_unique.assert_not_awaited()+ prisma_client.db.litellm_teamtable.find_unique.assert_not_awaited()+ assert prisma_client.db.litellm_usertable.find_many.await_args_list == [+ _identity_lookup("by-id", "[email protected]", "by-team", "nobody")+ ]+ assert prisma_client.db.litellm_teamtable.find_many.await_args_list == [+ mock_call(where={"team_id": {"in": ["[email protected]", "by-team", "nobody"]}})+ ]+++async def test_resolve_group_member_ids_chunks_member_lookups_by_batch_size(+ mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch, scim_upsert_user_enabled: None+):+ members: Final = (+ SCIMMember(value="member-id", type="direct"),+ SCIMMember(value="[email protected]"),+ SCIMMember(value="member-sub"),+ SCIMMember(value="unknown-one"),+ SCIMMember(value="unknown-two"),+ SCIMMember(value="member-id", type="direct"),+ )++ async def resolve() -> tuple[GroupMemberExtractionResult, MagicMock]:+ prisma_client: Final = _member_resolution_prisma(+ mocker,+ users={"member-id"},+ teams=set(),+ email_to_user_id={"[email protected]": "email-user"},+ sso_user_id_to_user_id={"member-sub": "sso-user"},+ )+ mocker.patch(+ "litellm.proxy.management_endpoints.scim.scim_v2._create_user_if_not_exists",+ AsyncMock(+ side_effect=[+ NewUserResponse(user_id="unknown-one", key="key-one"),+ NewUserResponse(user_id="unknown-two", key="key-two"),+ ]+ ),+ )+ result: Final = await _resolve_group_member_ids(+ members=members,+ created_via="scim_group_membership",+ prisma_client=prisma_client,+ )+ return result, prisma_client++ default_result, _ = await resolve()+ with monkeypatch.context() as context:+ context.setattr(scim_v2_module, "SCIM_GROUP_MEMBER_LOOKUP_BATCH_SIZE", 2)+ chunked_result, prisma_client = await resolve()++ assert (+ chunked_result.all_member_ids+ == default_result.all_member_ids+ == [+ "member-id",+ "email-user",+ "sso-user",+ "unknown-one",+ "unknown-two",+ ]+ )+ assert (+ chunked_result.existing_member_ids+ == default_result.existing_member_ids+ == [+ "member-id",+ "email-user",+ "sso-user",+ "member-id",+ ]+ )+ assert (+ chunked_result.created_users+ == default_result.created_users+ == [+ NewUserResponse(user_id="unknown-one", key="key-one"),+ NewUserResponse(user_id="unknown-two", key="key-two"),+ ]+ )+ user_calls: Final = prisma_client.db.litellm_usertable.find_many.await_args_list+ assert len(user_calls) == 3+ user_id_chunks: Final = tuple(tuple(entry.kwargs["where"]["OR"][0]["user_id"]["in"]) for entry in user_calls)+ assert all(len(chunk) <= 2 for chunk in user_id_chunks)+ assert tuple(chain.from_iterable(user_id_chunks)) == (+ "member-id",+ "[email protected]",+ "member-sub",+ "unknown-one",+ "unknown-two",+ )+++async def test_member_ids_to_drop_batches_user_id_email_and_sso_resolution(mocker: MockerFixture):+ prisma_client: Final = _member_resolution_prisma(+ mocker,+ users={"user-id"},+ teams=set(),+ email_to_user_id={"[email protected]": "email-user"},+ sso_user_id_to_user_id={"member-sub": "sso-user"},+ )++ dropped: Final = await _member_ids_to_drop(+ members=[+ SCIMMember(value="user-id"),+ SCIMMember(value="[email protected]"),+ SCIMMember(value="member-sub"),+ ],+ roster=frozenset({"user-id", "email-user", "sso-user"}),+ prisma_client=prisma_client,+ )++ assert dropped == frozenset({"user-id", "email-user", "sso-user"})assert prisma_client.db.litellm_usertable.find_many.await_args_list == [- _identity_lookup("by-id"),- _identity_lookup("[email protected]"),- _identity_lookup("by-team"),- _identity_lookup("nobody"),+ _identity_lookup("[email protected]", "member-sub", "user-id")]@@ -5841,10 +5989,13 @@ async def test_update_group_roster_failure_propagates(mocker):mock_prisma_client.db = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable = mocker.MagicMock()mock_prisma_client.db.litellm_teamtable.find_unique = AsyncMock(return_value=existing_team)+ mock_prisma_client.db.litellm_teamtable.find_many = AsyncMock(return_value=())mock_prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=existing_team)mock_prisma_client.db.litellm_usertable = mocker.MagicMock()mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(return_value=mocker.MagicMock())- mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(return_value=())+ mock_prisma_client.db.litellm_usertable.find_many = AsyncMock(+ return_value=(LiteLLM_UserTable(user_id="user1"), LiteLLM_UserTable(user_id="user2"))+ )mocker.patch("litellm.proxy.management_endpoints.scim.scim_v2._get_prisma_client_or_raise_exception",@@ -6007,18 +6158,44 @@ def _shadow_tenant_prisma(async def find_unique(where: Mapping[str, str]) -> LiteLLM_UserTable | None:return users.get(where["user_id"])+ def values_for(clause: Mapping[str, object], field: str, *, insensitive: bool) -> tuple[str, ...]:+ criterion: Final = clause[field]+ if isinstance(criterion, str):+ assert not insensitive+ return (criterion,)+ assert isinstance(criterion, dict), criterion+ if "in" in criterion:+ expected_keys: Final = ("in", "mode") if insensitive else ("in",)+ assert tuple(criterion) == expected_keys+ if insensitive:+ assert criterion["mode"] == "insensitive"+ values: Final = criterion["in"]+ assert isinstance(values, list)+ assert all(isinstance(value, str) for value in values)+ return tuple(values)+ assert insensitive+ assert tuple(criterion) == ("equals", "mode")+ assert criterion["mode"] == "insensitive"+ value: Final = criterion["equals"]+ assert isinstance(value, str)+ return (value,)+def clause_matches(row: LiteLLM_UserTable, clause: Mapping[str, object]) -> bool:- if "user_id" in clause:- return row.user_id == clause["user_id"]- if "sso_user_id" in clause:- return row.sso_user_id == clause["sso_user_id"]- email_filter: Final = clause["user_email"]- assert isinstance(email_filter, dict)- return (row.user_email or "").casefold() == str(email_filter["equals"]).casefold()+ field: Final = next(iter(clause))+ if field == "user_id":+ return row.user_id in values_for(clause, field, insensitive=False)+ if field == "sso_user_id":+ return row.sso_user_id in values_for(clause, field, insensitive=False)+ email_values: Final = frozenset(+ value.casefold() for value in values_for(clause, "user_email", insensitive=True)+ )+ return (row.user_email or "").casefold() in email_valuesasync def identity_rows(where: Mapping[str, object], take: int | None = None) -> tuple[LiteLLM_UserTable, ...]:clauses: Final = where["OR"]assert isinstance(clauses, list)+ fields: Final = tuple(next(iter(clause)) for clause in clauses)+ assert fields in (("user_id", "sso_user_id", "user_email"), ("sso_user_id", "user_email")), fieldsmatched: Final = tuple(row for row in users.values() if any(clause_matches(row, clause) for clause in clauses))return matched[:take] if take else matched@@ -6031,6 +6208,15 @@ def _shadow_tenant_prisma(async def team_lookup(where: Mapping[str, str]) -> LiteLLM_TeamTable | None:return team if where["team_id"] == team.team_id else None+ async def team_rows(where: Mapping[str, object]) -> tuple[LiteLLM_TeamTable, ...]:+ criterion: Final = where["team_id"]+ assert isinstance(criterion, dict)+ assert tuple(criterion) == ("in",)+ team_ids: Final = criterion["in"]+ assert isinstance(team_ids, list)+ assert all(isinstance(team_id, str) for team_id in team_ids)+ return (team,) if team.team_id in team_ids else ()+prisma_client = mocker.MagicMock()prisma_client.db = mocker.MagicMock()prisma_client.db.litellm_usertable = mocker.MagicMock()@@ -6039,6 +6225,7 @@ def _shadow_tenant_prisma(prisma_client.db.litellm_usertable.delete = AsyncMock(side_effect=delete)prisma_client.db.litellm_teamtable = mocker.MagicMock()prisma_client.db.litellm_teamtable.find_unique = AsyncMock(side_effect=team_lookup)+ prisma_client.db.litellm_teamtable.find_many = AsyncMock(side_effect=team_rows)prisma_client.db.litellm_teamtable.update = AsyncMock(return_value=team)prisma_client.db.litellm_verificationtoken = mocker.MagicMock()prisma_client.db.litellm_verificationtoken.find_many = AsyncMock(side_effect=keys_for)@@ -6179,7 +6366,9 @@ class _PatchedTeamRow:async def update(self, *, where: dict[str, object], data: dict[str, object]) -> LiteLLM_TeamTable:self.written = data- self.team = LiteLLM_TeamTable(**{**self.team.model_dump(), **data, "metadata": json.loads(str(data["metadata"]))})+ self.team = LiteLLM_TeamTable(+ **{**self.team.model_dump(), **data, "metadata": json.loads(str(data["metadata"]))}+ )return self.team