Skip to content

fix(router): evict stale global pattern_router entries on upsert/delete - #39664

Merged
yassin-berriai merged 4 commits into
litellm_internal_stagingfrom
litellm_lit_3412_global_pattern_router_cleanup
Sep 4, 2026
Merged

yassin-berriai merged 4 commits into
litellm_internal_stagingfrom
litellm_lit_3412_global_pattern_router_cleanup

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Updating or deleting a wildcard deployment left it in the global pattern router
  • Wildcard requests kept round-robining onto the stale, pre-update deployment

How it solves it:

  • The removal path now evicts the model id from the global pattern router too, matching what already happens for team pattern routers

User Flow

Before: an admin fixes a broken wildcard model but half the requests still fail until the pods restart

  1. They send POST https://litellm-domain/model/new with model_name: "openai/*" and a mistyped litellm_params.model: "openai/openai/*"
  2. They send POST https://litellm-domain/v1/chat/completions with model: "openai/gpt-5.2" and get a 400 invalid model ID because of the double prefix
  3. They send PATCH https://litellm-domain/model/{id}/update with litellm_params.model: "openai/*" and get a 200
  4. After the proxy picks up the change, they send the same POST /v1/chat/completions ten times and roughly half still return the 400 invalid model ID

After: the same fix takes effect fully

  1. They send POST https://litellm-domain/model/new with model_name: "openai/*" and a mistyped litellm_params.model: "openai/openai/*"
  2. They send POST https://litellm-domain/v1/chat/completions with model: "openai/gpt-5.2" and get a 400 invalid model ID because of the double prefix
  3. They send PATCH https://litellm-domain/model/{id}/update with litellm_params.model: "openai/*" and get a 200
  4. After the proxy picks up the change, all ten POST /v1/chat/completions calls return 200

Relevant issues

Fixes #29064

Linear ticket

Resolves LIT-3412

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)

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

Earlier fixes (#29601 and 836bf08) covered team_pattern_routers; the global pattern_router was the remaining gap. Live proxy started with DISABLE_SCHEMA_UPDATE=True uv run litellm --config litellm/proxy/dev_config.yaml --port 4000 (STORE_MODEL_IN_DB=True, real OpenAI key), same steps both times:

H='Authorization: Bearer sk-1234'; J='Content-Type: application/json'
ID=$(curl -s -X POST localhost:4000/model/new -H "$H" -H "$J" -d '{"model_name":"openai/*","litellm_params":{"model":"openai/openai/*","api_key":"os.environ/OPENAI_API_KEY"}}' | python3 -c 'import sys,json; print(json.load(sys.stdin)["model_info"]["id"])')
sleep 12
curl -s -o /dev/null -w '%{http_code}\n' -X POST localhost:4000/v1/chat/completions -H "$H" -H "$J" -d '{"model":"openai/gpt-5.2","messages":[{"role":"user","content":"hi"}],"max_tokens":1}'
curl -s -o /dev/null -w '%{http_code}\n' -X PATCH "localhost:4000/model/$ID/update" -H "$H" -H "$J" -d '{"litellm_params":{"model":"openai/*"}}'
sleep 15
for i in $(seq 1 10); do curl -s -o /dev/null -w '%{http_code} ' -X POST localhost:4000/v1/chat/completions -H "$H" -H "$J" -d '{"model":"openai/gpt-5.2","messages":[{"role":"user","content":"hi"}],"max_tokens":1}'; done; echo
curl -s -o /dev/null -w '%{http_code}\n' -X POST localhost:4000/model/delete -H "$H" -H "$J" -d "{\"id\":\"$ID\"}"

Before (a5b3bc8)

  1. POST /model/new with openai/openai/*: model_id=3527f9b3-8f53-4196-b7bd-25a514e33d46
  2. POST /v1/chat/completions openai/gpt-5.2: 400 (double prefix, expected)
  3. PATCH /model/{id}/update to openai/*: 200
  4. Ten POST /v1/chat/completions after the fix: 400 200 200 200 400 200 400 200 200 400
  5. POST /model/delete: 200

After (e5facac)

  1. POST /model/new with openai/openai/*: model_id=cf24e223-ed0d-4032-8766-5b538719981f
  2. POST /v1/chat/completions openai/gpt-5.2: 400 (double prefix, expected)
  3. PATCH /model/{id}/update to openai/*: 200
  4. Ten POST /v1/chat/completions after the fix: 200 200 200 200 200 200 200 200 200 200
  5. POST /model/delete: 200

Type

🐛 Bug Fix

Caveats (if any)

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/22761ace217b49f8b907cc2c13033fa3
Open in Devin Desktop: https://app.devin.ai/desktop/session/22761ace217b49f8b907cc2c13033fa3?variant=devin
Requested by: @yassin-berriai

upsert_deployment and delete_deployment cleaned team_pattern_routers but left
the outgoing deployment in the global pattern_router, so wildcard requests kept
round-robining onto the stale entry after a PATCH /model/{id}/update.

Fixes #29064

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

@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.

…info name

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

codspeed Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_lit_3412_global_pattern_router_cleanup (e537ccc) with litellm_internal_staging (a53c550)

Open in CodSpeed

…us commit

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

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@greptile-apps

greptile-apps Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR keeps global wildcard routing state synchronized when deployments are updated or deleted.

  • Evicts the affected deployment ID from the global pattern router during the existing removal path.
  • Adds regression coverage for wildcard upsert and deletion, verifying that stale credentials disappear and the final pattern is removed.

Confidence Score: 5/5

The PR appears safe to merge, with the global wildcard cleanup now matching the existing deployment lifecycle.

The global pattern router is always initialized, normal upserts re-register the replacement deployment, failed upserts restore the prior deployment, and the regression test covers both replacement and deletion.

Important Files Changed

Filename Overview
litellm/router.py Extends deployment removal to evict matching entries from the global pattern router, aligning it with existing team-router cleanup.
tests/test_litellm/test_router.py Adds focused, network-free regression coverage for global wildcard replacement and deletion.

Reviews (1): Last reviewed commit: "fix(router): evict stale global pattern_..." | Re-trigger Greptile

…ttern_router_cleanup

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

upsert_deployment does not clean up stale entries in team_pattern_routers / pattern_router on model update

2 participants