Skip to content

fix(scim): fail group sync when a member add or user creation fails (LIT-5105) - #37688

Merged
yassin-berriai merged 2 commits into
litellm_internal_stagingfrom
devin/lit-5105-scim-provisioning-misses-some-users-in-larger-teams
Aug 20, 2026
Merged

yassin-berriai merged 2 commits into
litellm_internal_stagingfrom
devin/lit-5105-scim-provisioning-misses-some-users-in-larger-teams

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • SCIM group sync reported success while some members never landed on the team
  • Larger groups (10+, often 50+) ended up with users missing from teams
  • A member whose user row failed to create was silently dropped

How it solves it:

  • Group membership adds and removes now propagate genuine failures to the IdP
  • A member whose user can neither be found nor created fails the request with 500
  • Idempotent no-ops (already in team, already absent) are still swallowed
  • Repeated member ids in one request are created once instead of twice

User Flow

Before: an admin provisions a 50-member group from their identity provider and the IdP reports success, but the team is missing users

  1. The admin assigns a 50-member group to the SCIM app and triggers provisioning
  2. The IdP sends PUT https://litellm-domain/scim/v2/Groups/{group_id} with all 50 members and gets 200 back
  3. The IdP marks every member as provisioned
  4. The admin opens https://litellm-domain/ui/?page=teams and the team shows fewer than 50 members
  5. The admin has to add the missing users to the team by hand

After: the same sync either lands every member or surfaces an error the IdP retries

  1. The admin assigns a 50-member group to the SCIM app and triggers provisioning
  2. The IdP sends PUT https://litellm-domain/scim/v2/Groups/{group_id} with all 50 members
  3. If every member lands, the IdP gets 200 and https://litellm-domain/ui/?page=teams shows all 50 members
  4. If any member fails to enroll, the IdP gets a 5xx naming the failed user and retries that sync instead of recording a false success

Relevant issues

Linear ticket

Resolves LIT-5105

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Captured against a live proxy on 127.0.0.1:4171 backed by its own Postgres. The proxy ran from a directory outside every checkout so it could not pick up a repo .env, with PYTHONPATH pointed at the branch worktree, so the two legs really do serve different code. No LLM provider is involved: SCIM provisioning is a management-plane path, so the whole capture is /scim/v2/* plus /team/info

Every case pushes a 24-member group, comfortably past the size the ticket describes. In cases 2 and 3, 8 of those 24 already have a LiteLLM user row that the pushed member id does not resolve to. Four were onboarded by SSO, so their row is keyed by the identity provider's object id while the group lists them by email, and four arrived through POST /scim/v2/Users in lowercase while the group lists them with the casing the directory displays. LiteLLM itself emits group members by email on GET /scim/v2/Groups/{id}, so a directory that reads a group back and re-pushes it sends exactly these values

The two legs use disjoint user and team ids (...before... / ...after...) so neither leg can inherit rows the other created

Shared setup, run identically inside each leg:

API=http://127.0.0.1:4171
V=before   # "after" on the second leg

# 4 people already onboarded by SSO: their row is keyed by the IdP object id
for i in 1 2 3 4; do
  curl -s -o /dev/null -w "%{http_code}\n" -X POST $API/user/new \
    -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/json' \
    -d "{\"user_id\":\"c9p37688-$V-oid-$i\",\"user_email\":\"c9p37688.$V.sso$i@example.com\",\"user_role\":\"internal_user\"}"
done

# 4 people already provisioned through SCIM, userName in lowercase
for i in 1 2 3 4; do
  curl -s -o /dev/null -w "%{http_code}\n" -X POST $API/scim/v2/Users \
    -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' \
    -d "{\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"],\"userName\":\"c9p37688.$V.case$i@example.com\",\"active\":true,\"emails\":[{\"value\":\"c9p37688.$V.case$i@example.com\",\"primary\":true}]}"
done

Both legs printed 200 200 200 200 then 201 201 201 201, and select count(*) from "LiteLLM_UserTable" where user_id like 'c9p37688-$V-%' or user_id like 'c9p37688.$V.%' returned 8

Group pushes are built from a for loop over the member list:

members() { local out=""; for v in "$@"; do out="$out{\"value\":\"$v\"},"; done; echo "[${out%,}]"; }
PAYLOAD=$(printf '{"schemas":["urn:ietf:params:scim:schemas:core:2.0:Group"],"id":"%s","displayName":"%s","members":%s}' "$TEAM" "$NAME" "$(members "${MEMBERS[@]}")")

Each leg re-printed the identity of the code it was actually serving, so every number below carries proof of which tree produced it:

                                                             Before    After
$ grep -c _ensure_group_member_user .../scim/scim_v2.py         0        2
$ grep -c 'raise_on_error=True'     .../scim/scim_v2.py         1        3
$ shasum -a 256                     .../scim/scim_v2.py     67f9d761  2b566279
$ python -c 'import litellm, os; print(bool(os.environ.get("OPENAI_API_KEY")))'
                                                             False    False
$ curl -s $API/scim/v2/ServiceProviderConfig -o /dev/null -w '%{http_code}'
                                                              200      200

Before (3a31331)

Case 1: a 24-member group where nobody exists yet

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 201
    {"id":"c9p37688-before-clean","displayName":"C9 Clean Team (before)","members_written":24}
    
  2. curl -s $API/scim/v2/Groups/c9p37688-before-clean -H "Authorization: Bearer $SCIM_TOKEN" | jq '{id, members_on_team: (.members|length)}'

    {"id":"c9p37688-before-clean","members_on_team":24}
    
  3. curl -s "$API/team/info?team_id=c9p37688-before-clean" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    24
    

Case 2: a 24-member group where 8 members already have a user row under another id

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 201
    {"id":"c9p37688-before-mixed","displayName":"C9 Mixed Team (before)","members_written":16}
    

    The push is reported as a success and the group comes back with 16 of the 24 members that were sent

  2. curl -s $API/scim/v2/Groups/c9p37688-before-mixed -H "Authorization: Bearer $SCIM_TOKEN" | jq '{id, members_on_team: (.members|length)}'

    {"id":"c9p37688-before-mixed","members_on_team":16}
    
  3. curl -s "$API/team/info?team_id=c9p37688-before-mixed" -H "Authorization: Bearer $SCIM_TOKEN" | jq -r '.team_info.members_with_roles[].user_id' | sort

    c9p37688.before.new01@example.com
    ... 16 lines, new01 through new16 ...
    c9p37688.before.new16@example.com
    

    All 8 people who already existed are missing from the team, and nothing in the response says so

  4. grep -c "Failed to create user" proxy-before.log

    8
    
    ERROR: scim_v2.py:884 - Failed to create user c9p37688.before.sso1@example.com: {'error': 'User with email c9p37688.before.sso1@example.com already exists'}
    ERROR: scim_v2.py:884 - Failed to create user C9P37688.before.Case1@example.com: {'error': 'User with email c9p37688.before.case1@example.com already exists'}
    ... 8 lines total ...
    

    The proxy knew about all 8 failures and still answered 201

Case 3: the same 24-member set pushed with PUT onto an existing group

  1. Seed the group with one member: POST $API/scim/v2/Groups returns HTTP 201

  2. curl -s -w '\nHTTP %{http_code}' -X PUT $API/scim/v2/Groups/c9p37688-before-put -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 200
    
  3. curl -s "$API/team/info?team_id=c9p37688-before-put" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    16
    

    Same silent loss on the route an identity provider uses for every re-sync

Case 4: the same 24 people, addressed by ids that already resolve

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 201
    {"id":"c9p37688-before-ctl","displayName":"C9 Control Team (before)","members_written":24}
    
  2. curl -s "$API/team/info?team_id=c9p37688-before-ctl" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    24
    

    The 8 people dropped in case 2 are real, enrollable users, so the loss there is a loss of people and not a rejected payload

After (74a409b)

Case 1: a 24-member group where nobody exists yet

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 201
    {"id":"c9p37688-after-clean","displayName":"C9 Clean Team (after)","members_written":24}
    
  2. curl -s $API/scim/v2/Groups/c9p37688-after-clean -H "Authorization: Bearer $SCIM_TOKEN" | jq '{id, members_on_team: (.members|length)}'

    {"id":"c9p37688-after-clean","members_on_team":24}
    
  3. curl -s "$API/team/info?team_id=c9p37688-after-clean" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    24
    

Case 2: a 24-member group where 8 members already have a user row under another id

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 500
    {"error":{"message":"{'error': \"Failed to create user 'c9p37688.after.sso1@example.com' while provisioning group membership.\"}","type":"internal_server_error","param":"None","code":"500"}}
    

    The failure now reaches the caller and names the member it could not provision, so the identity provider retries instead of recording 24 people as synced

  2. curl -s -w '\nHTTP %{http_code}' $API/scim/v2/Groups/c9p37688-after-mixed -H "Authorization: Bearer $SCIM_TOKEN"

    HTTP 404
    {"error":{"message":"{'error': 'Group not found with ID: c9p37688-after-mixed'}",...,"code":"404"}}
    
  3. curl -s "$API/team/info?team_id=c9p37688-after-mixed" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    0
    

    No half-populated team is left behind claiming to be the group

Case 3: the same 24-member set pushed with PUT onto an existing group

  1. Seed the group with one member: POST $API/scim/v2/Groups returns HTTP 201

  2. curl -s -w '\nHTTP %{http_code}' -X PUT $API/scim/v2/Groups/c9p37688-after-put -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 500
    {"error":{"message":"{'error': \"Failed to create user 'c9p37688.after.sso1@example.com' while provisioning group membership.\"}",...,"code":"500"}}
    
  3. curl -s "$API/team/info?team_id=c9p37688-after-put" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    1
    

    The group keeps the member it already had rather than being rewritten to a roster that silently lost 8 people

Case 4: the same 24 people, addressed by ids that already resolve

  1. curl -s -w '\nHTTP %{http_code}' -X POST $API/scim/v2/Groups -H "Authorization: Bearer $SCIM_TOKEN" -H 'Content-Type: application/scim+json' -d "$PAYLOAD"

    HTTP 201
    {"id":"c9p37688-after-ctl","displayName":"C9 Control Team (after)","members_written":24}
    
  2. curl -s "$API/team/info?team_id=c9p37688-after-ctl" -H "Authorization: Bearer $SCIM_TOKEN" | jq '.team_info.members_with_roles | length'

    24
    

    A 24-member sync that can land still lands in full, so the strict path costs nothing when the ids resolve

Both legs answered /health/liveliness with 200 immediately before and immediately after every case, so no result straddles a restarted proxy

Type

🐛 Bug Fix

Caveats (if any)

  • A failing member now fails the whole group sync; IdPs retry these
  • test_update_group_metadata_serialization_issue now mocks membership writes, since its unmocked DB call was previously swallowed by the bug this PR removes

Link to Devin session: https://app.devin.ai/sessions/79e76557aa114b229b47fd35e18bf1f2
Requested by: @yassin-berriai

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR makes SCIM group synchronization report user-creation and membership-write failures instead of silently omitting members.

  • Rechecks user existence when creation returns no user and fails unresolved provisioning.
  • Deduplicates unknown member IDs within a request.
  • Enables strict error propagation for group membership additions and removals while retaining idempotent no-ops.
  • Adds focused regression coverage for failed creation, concurrent creation, failed additions, and duplicate enrollment.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/proxy/management_endpoints/scim/scim_v2.py Adds strict SCIM member resolution and membership failure propagation; no follow-up-eligible blocking defect was established.
tests/test_litellm/proxy/management_endpoints/scim/test_scim_v2_endpoints.py Adds focused regression tests and isolates an existing metadata serialization test from unrelated membership persistence.

Reviews (2): Last reviewed commit: "style(scim): apply ruff format" | Re-trigger Greptile

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing devin/lit-5105-scim-provisioning-misses-some-users-in-larger-teams (74a409b) with litellm_internal_staging (e07a712)

Open in CodSpeed

@yassin-berriai

Copy link
Copy Markdown
Contributor

@greptileai please review the current head 74a409bf2b. Your score is anchored to the earlier commit 183e5b9e58, so it predates the latest changes

@tin-berri tin-berri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auth-adjacent (SCIM provisioning) so reviewed the diff directly. The core fix is correct and carefully scoped: patch_team_membership now runs with raise_on_error=True so a genuine add/remove failure fails the whole group sync (so the IdP retries instead of recording false success), while idempotent no-ops (already-in-team, already-removed) are still swallowed internally by patch_team_membership — verified by test_handle_group_membership_changes_already_in_team_is_noop specifically asserting the sync continues to the second member rather than aborting. The concurrent-creation race is handled right too: if user creation fails because another request already created the same user, that's treated as success (id resolves to a real row) rather than a false failure — tested. Bonus fix: duplicate member ids in one request are now deduped before creation instead of double-creating. Honest about test-only proof (no live SCIM sync run, but regression tests verified to fail at merge base and pass at tip — a reasonable substitute given IdP-driven live testing isn't practical here). This is an intentional behavior change (previously-silent partial failures now surface as 5xx) which is exactly the fix, not a regression. CI green.

@yassin-berriai
yassin-berriai merged commit a030b33 into litellm_internal_staging Aug 20, 2026
71 checks passed
@yassin-berriai
yassin-berriai deleted the devin/lit-5105-scim-provisioning-misses-some-users-in-larger-teams branch August 20, 2026 23:00
yassin-berriai added a commit that referenced this pull request Aug 21, 2026
…ting a placeholder

SCIM group members were matched against litellm user ids only. An identity
provider that lists people by email or by the OIDC subject therefore matched
nothing, and the member fell through to placeholder creation.

Since #37688 made a failed member creation fail the group sync rather than drop
the member, that fallthrough is no longer quiet: the placeholder is created with
user_email set to the member value, the duplicate-email check rejects it, and the
whole group push answers 500. So on current staging a group listing anyone by
their email fails outright, every other member in the payload included.

An unmatched member id is now looked up across sso_user_id and user_email in one
query. Searching either field first would hide a value that names one account by
its SSO identity and another by its email, and hand the group to whichever was
searched first. The two are not compared alike: an email is matched the way
new_user matches one before accepting a new account, case-insensitively, because
matching more strictly than the layer that would reject the placeholder is what
turned an id whose casing differed from the stored email into that same 500. An
SSO identity is matched exactly, since OIDC defines sub as case-sensitive and
nothing folds its case on the way in.

An exact user_id hit is checked the same way rather than trusted outright, since a
value can be one account's id and another's SSO identity or email. That is not a
corner case: the placeholders this bug provisioned are keyed by the very id the
provider keeps pushing, so on a tenant that already has them the placeholder wins
the id lookup and the real account can never be matched. Refusing names the
problem instead of silently landing on the placeholder again. Those rows still
have to be deleted before the real account resolves; making the sync heal itself
needs a trustworthy way to tell a placeholder from an account someone created, and
created_via lives in caller-writable metadata, so it is left to a follow-up.

A value that names more than one account is refused with a 400 naming the id
rather than attributed to one of them.

Removals resolve too, since the roster holds canonical user ids and a directory
removes people by the id it added them with. A removal counts the members one
value names: the id as written when the roster holds it verbatim, which is how an
earlier release recorded a member it could not match, together with the members it
resolves to. Counting only the accounts on the roster keeps someone removable
after a second account takes their email, which resolving table-wide would not,
and counting both ways of naming a member together stops one value revoking two
people when it is one member's canonical id and another's email. A value naming
two of the group's own members is undecidable and fails rather than guessing or
reporting a removal it did not perform.

Resolves LIT-5383
yassin-berriai added a commit that referenced this pull request Aug 21, 2026
…ting a placeholder (#37686)

SCIM group members were matched against litellm user ids only. An identity
provider that lists people by email or by the OIDC subject therefore matched
nothing, and the member fell through to placeholder creation.

Since #37688 made a failed member creation fail the group sync rather than drop
the member, that fallthrough is no longer quiet: the placeholder is created with
user_email set to the member value, the duplicate-email check rejects it, and the
whole group push answers 500. So on current staging a group listing anyone by
their email fails outright, every other member in the payload included.

An unmatched member id is now looked up across sso_user_id and user_email in one
query. Searching either field first would hide a value that names one account by
its SSO identity and another by its email, and hand the group to whichever was
searched first. The two are not compared alike: an email is matched the way
new_user matches one before accepting a new account, case-insensitively, because
matching more strictly than the layer that would reject the placeholder is what
turned an id whose casing differed from the stored email into that same 500. An
SSO identity is matched exactly, since OIDC defines sub as case-sensitive and
nothing folds its case on the way in.

An exact user_id hit is checked the same way rather than trusted outright, since a
value can be one account's id and another's SSO identity or email. That is not a
corner case: the placeholders this bug provisioned are keyed by the very id the
provider keeps pushing, so on a tenant that already has them the placeholder wins
the id lookup and the real account can never be matched. Refusing names the
problem instead of silently landing on the placeholder again. Those rows still
have to be deleted before the real account resolves; making the sync heal itself
needs a trustworthy way to tell a placeholder from an account someone created, and
created_via lives in caller-writable metadata, so it is left to a follow-up.

A value that names more than one account is refused with a 400 naming the id
rather than attributed to one of them.

Removals resolve too, since the roster holds canonical user ids and a directory
removes people by the id it added them with. A removal counts the members one
value names: the id as written when the roster holds it verbatim, which is how an
earlier release recorded a member it could not match, together with the members it
resolves to. Counting only the accounts on the roster keeps someone removable
after a second account takes their email, which resolving table-wide would not,
and counting both ways of naming a member together stops one value revoking two
people when it is one member's canonical id and another's email. A value naming
two of the group's own members is undecidable and fails rather than guessing or
reporting a removal it did not perform.

Resolves LIT-5383

Co-authored-by: Yassin Kortam <yassin@berri.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants