Repository navigation
fix(team): make membership insert idempotent on /team/member_add - #39821
devin-ai-integration[bot] wants to merge 5 commits into
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Merging this PR will not alter performance
Comparing |
Greptile SummaryAdds an existing-membership lookup so team-member insertion can reuse the current membership and avoid duplicate budget creation
Confidence Score: 4/5The behavior appears correct, but the repository’s explicit database-access requirement must be satisfied before merging The transaction-scoped team lock makes the membership reuse effective for production callers, but the implementation adds a prohibited direct database request in a critical request path Files Needing Attention: litellm/proxy/management_helpers/utils.py
|
| Filename | Overview |
|---|---|
| litellm/proxy/management_helpers/utils.py | Adds idempotent membership reuse, but does so through a new direct database query in the first-use request path |
| tests/test_litellm/proxy/management_helpers/test_management_helpers_utils.py | Covers existing-membership reuse, skipped budget cloning, and normal creation when no membership exists |
Reviews (1): Last reviewed commit: "fix(team): make membership insert idempo..." | Re-trigger Greptile
| existing_membership: Final[_PrismaRecord | None] = await membership_table.find_unique( | ||
| where={"user_id_team_id": {"user_id": returned_user.user_id, "team_id": team_id}}, | ||
| include={"litellm_budget_table": True}, | ||
| ) |
There was a problem hiding this comment.
Direct Critical-Path Database Read
This adds a direct membership query to the first-use request path. Repository rules prohibit new database requests there and require database access through the approved object helpers, so this requirement must be satisfied before merging
Rule Used: What: In critical path of request, there should be... (source)
Knowledge Base Used: Database access and migrations
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Closing as superseded by #41349, which already made the membership write an upsert and shipped in v1.103.0 |
TLDR
Problem this solves:
/team/member_addreturns 500 "Unique constraint failed" when a membership row already existsHow it solves it:
User Flow
Before: a developer whose IdP group entitles them to a team with a per-member budget gets most of their first-use requests rejected, and a user with a mismatched account is rejected forever
{"error": "Unable to add user - user_id='...', to team - '...', for reason - Unique constraint failed on the fields: (user_id,team_id)", "type": "auth_error", "code": "500"}After: the same first-use burst puts them on the team once and all requests are served
Relevant issues
Linear ticket
Resolves LIT-6983
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Local proxy on Postgres:
uv run --no-sync litellm --config litellm/proxy/dev_config.yaml --detailed_debug --port 4000, master keysk-1234. Each leg creates a team with a per-member budget, which is what gates the membership insert:Then fires five concurrent adds of one new user:
Then drops only the roster entry, leaving the membership row (the desynced state older versions could produce), and retries a single add:
Requests that read the roster after the first add committed get the pre-existing 400
team_member_already_in_team, on both legs. That check is unchanged here; the difference is what happens to the requests that get past itBefore (d23bec8)
Concurrent adds, team with per-member budget
/team/new->team_id=832672bb-1499-40b4-8eb0-2f1f97f91dea,metadata.team_member_budget_id=team-lit-6983-before-budget-...;SELECT count(*) FROM "LiteLLM_BudgetTable"-> 4lit6983-user-b:{"detail":{"error":"Unable to add user - user_id='lit6983-user-b' user_email=None role='user', to team - 832672bb-1499-40b4-8eb0-2f1f97f91dea, for reason - Unique constraint failed on the fields: (user_id,team_id)"}}SELECT count(*) FROM "LiteLLM_BudgetTable"-> 6. Two new rows for one member: the failed request cloned a budget before its insert blew upWedged roster/membership desync
lit6983-user-b->UPDATE 1/team/member_addforlit6983-user-b->http=500, body{"detail":{"error":"Unable to add user - ... for reason - Unique constraint failed on the fields: (user_id,team_id)"}}. Every retry returns the sameAfter (b00e9e8, the fix commit; later commits b41302e and earlier only touch test mocks and a type-gate condition, no behavior change)
Concurrent adds, team with per-member budget
/team/new->team_id=d78bd31e-b818-487c-9fb0-49c3755d7a72,metadata.team_member_budget_id=team-lit-6983-after-budget-...;SELECT count(*) FROM "LiteLLM_BudgetTable"-> 2lit6983-user-a:budget_id=a7963fb1-6dc8-4ce4-9c93-868d6913fb90GET /user/info?user_id=lit6983-user-a->"teams":["d78bd31e-..."], one entry inteam_membershipswithbudget_id=a7963fb1-...SELECT count(*) FROM "LiteLLM_TeamMembership" WHERE user_id='lit6983-user-a'-> 1;SELECT count(*) FROM "LiteLLM_BudgetTable"-> 3, exactly one new rowWedged roster/membership desync
lit6983-user-a->UPDATE 1/team/member_addforlit6983-user-a->http=200; responsemembers_with_rolescontainslit6983-user-aagain and the membership reusesbudget_id=a7963fb1-...Type
🐛 Bug Fix
Caveats (if any)
Low
proxy-infra (Python 3.10)fails ontest_http_parsing_utils.py::TestNumericFormFields::test_qualifiers_and_optionality_are_unwrapped, which also fails onlitellm_internal_stagingat d23bec8 (run 33925150414), unrelated to this changeAn existing membership keeps its budget even if the request passes
max_budget_in_teamorallowed_models; the add is a no-op for that row, matching the existing roster-only behaviorThe pre-lock roster duplicate check still returns 400 for late concurrent requests; that is unchanged and tracked separately from the 500
Final Attestation
Link to Devin session: https://app.devin.ai/sessions/ba5b0b0b5f1e43cf942453cb51a57891
Open in Devin Desktop: https://app.devin.ai/desktop/session/ba5b0b0b5f1e43cf942453cb51a57891?variant=devin
Requested by: @shivamrawat1