Skip to content

fix(proxy): reject model writes that corrupt an auto-router pseudo-model - #34151

Merged
tin-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_lit4663_autorouter_prefix
Jul 29, 2026
Merged

tin-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_lit4663_autorouter_prefix

Conversation

@tin-berri

@tin-berri tin-berri commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

  • Rejects /model/new, /model/update, and /model/{model_id}/update writes that would mangle an auto-router deployment's litellm_params.model discriminator (doubled prefix, stripped prefix, or missing required strategy config), returning an actionable 400 instead of letting the router silently void the model
  • Extracts the strategy-router classification (semantic / complexity / adaptive / quality by auto_router/ prefix) into a shared router_utils/auto_router_model_naming.py helper that both Router._is_*_router_deployment and the new write validation use, so the naming contract is single-sourced
  • Public model_name renames (including to auto_router/<name>) stay legal, and already-corrupted rows stay healable by writing the correct pseudo-model back through the same field

Linear ticket

Resolves LIT-4663

Pre-Submission checklist

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

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • 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)

One CI red is inherited from the base branch, not this diff: osv-scan (pyasn1 0.6.3 in uv.lock, GHSA-8ppf-4f7h-5ppj; fails identically on sibling PRs, the lockfile bump belongs in its own PR). Locally, make pre-commit also reds on the pre-existing e2e basedpyright errors in tests/e2e/llm_translation (fix open in #34090); that gate does not run in CI. Everything this diff touches is green: ruff format, strict/LIT budgets, basedpyright budget, gen:api sync, and both mapped test files

Screenshots / Proof of Fix

A customer edited the "LiteLLM Model Name" field of a working complexity auto-router in the dashboard. That field is litellm_params.model, which for auto-routers is not a provider model at all; it is the internal discriminator (auto_router/complexity_router) the router loads the deployment by. The edit persisted auto_router/auto_router/complexity_router verbatim, router init then failed on the next load, ignore_invalid_deployments swallowed the failure, and the model vanished: calls fail "no healthy deployments", and after a restart the model is gone from /v1/models entirely. There is no guard on this field in the UI or the API, so one keystroke in a free-text box silently destroys a working router

Repro and verify below ran against a live proxy (port 4663, real Postgres, real Anthropic calls), config: three claude tier models plus a DB-stored complexity auto-router created via /model/new

Before the fix (staging HEAD 01d624e):

# baseline: auto-router routes fine
curl -sX POST localhost:4663/v1/chat/completions -H "Authorization: Bearer $MASTER_KEY" \
  -d '{"model": "router-a", "messages": [{"role": "user", "content": "hi"}], "max_tokens": 5}'
# -> 200, routed to a tier model

# the customer's edit: add the prefix to the "LiteLLM Model Name" (litellm_params.model)
curl -sX PATCH localhost:4663/model/$ID/update -H "Authorization: Bearer $MASTER_KEY" \
  -d '{"litellm_params": {"model": "auto_router/auto_router/complexity_router"}}'
# -> HTTP 200 (accepted verbatim)

curl -sX POST localhost:4663/v1/chat/completions ... '{"model": "router-a", ...}'
# -> "litellm.BadRequestError: You passed in model=router-a. There are no healthy
#     deployments for this model. Received Model Group=router-a"

# after proxy restart the row is silently dropped at load: router-a is gone from /v1/models

The stripped variant ("model": "complexity_router") voids the model the same way

After the fix (same commands):

curl -sX PATCH localhost:4663/model/$ID/update ... '{"litellm_params": {"model": "auto_router/auto_router/complexity_router"}}'
# -> HTTP 400 {"error": {"message": "litellm_params.model='auto_router/auto_router/complexity_router'
#    repeats the 'auto_router/' prefix, so the router could not load it. Use
#    'auto_router/complexity_router'; to change the name clients call, edit the public
#    model_name instead.", "param": "litellm_params.model"}}

curl -sX PATCH localhost:4663/model/$ID/update ... '{"litellm_params": {"model": "complexity_router"}}'
# -> HTTP 400 "... does not start with 'auto_router/' but the deployment carries auto-router
#    settings (complexity_router_config, complexity_router_default_model) ..."

# the router keeps working after the rejected edits
curl -sX POST localhost:4663/v1/chat/completions ... '{"model": "router-a", ...}'   # -> 200

# renaming what clients call still works: PATCH model_name (public name) to auto_router/router-a
curl -sX PATCH localhost:4663/model/$ID/update ... '{"model_name": "auto_router/router-a"}'  # -> 200
curl -sX POST localhost:4663/v1/chat/completions ... '{"model": "auto_router/router-a", ...}'  # -> 200

# a row corrupted before the fix heals through the same field
curl -sX PATCH localhost:4663/model/$CORRUPTED_ID/update ... '{"litellm_params": {"model": "auto_router/complexity_router"}}'  # -> 200, calls route again

# creates are validated too, on both malformed strings and missing required config
curl -sX POST localhost:4663/model/new ... '{"litellm_params": {"model": "auto_router/auto_router/x", ...}}'  # -> 400
curl -sX POST localhost:4663/model/new ... '{"litellm_params": {"model": "auto_router/sem-router", "auto_router_config": "{}", "auto_router_default_model": "claude-haiku"}}'
# -> 400 "... selects the semantic router, which requires auto_router_embedding_model in litellm_params."

# the legacy POST /model/update endpoint enforces the same rule
curl -sX POST localhost:4663/model/update ... '{"litellm_params": {"model": "complexity_router"}, "model_info": {"id": "$ID"}}'  # -> 400

Type

🐛 Bug Fix

Changes

  • New litellm/router_utils/auto_router_model_naming.py: classify_strategy_router_model (faithful to existing Router registration semantics) and validate_strategy_router_model_write, which checks a written litellm_params.model against the strategy fields present on the merged deployment
  • litellm/router.py: the four _is_*_router_deployment predicates now delegate to the shared classifier; no behavior change
  • litellm/proxy/management_endpoints/model_management_endpoints.py: add_new_model, update_model, and patch_model reject incoherent writes with a 400 naming the violation and the fix; only writes that supply litellm_params.model are judged, so partial patches and restores of corrupted rows stay legal
  • Tests: new tests/test_litellm/router_utils/test_auto_router_model_naming.py (classification + malformed/coherent write families) and TestStrategyRouterWriteValidation in the mapped management-endpoints test file (all three endpoint seams, restore path, patch-without-model)

What does not change: routing and dispatch, the strategy registries, the dedicated Edit Auto Router modal (it never touches litellm_params.model), config-file loading, and public model_name renames

Things a reviewer will ask about: the validator judges the merged final state rather than the transition, which is what keeps a restore of an already-corrupted row legal while still blocking every corruption; and it only runs when the write supplies litellm_params.model, so it needs no decryption of stored values (presence of strategy fields is unaffected by encryption at rest)

QA runbook

  1. Start a proxy with a DB and three real models; create a complexity auto-router in the UI (Add Model, Auto Router tab) and confirm a chat completion against it returns 200
  2. Models page, open the auto-router, Edit, change "LiteLLM Model Name" to auto_router/auto_router/complexity_router, Save: expect a red toast with the "repeats the 'auto_router/' prefix" message, and the model keeps serving requests
  3. Same edit but set it to complexity_router: expect the "does not start with 'auto_router/'" toast
  4. Change the public "Model Name" to auto_router/my-router instead and Save: expect success, and {"model": "auto_router/my-router"} chat completions return 200
  5. curl -X POST /model/new with litellm_params.model="auto_router/sem-router" and no auto_router_embedding_model: expect 400 naming the missing field

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

Note

Medium Risk
Touches proxy model-management write paths for all DB-backed models; logic is narrow (only when litellm_params.model is supplied) but incorrect validation could block legitimate creates/updates.

Overview
Prevents silent loss of auto-router deployments when admins edit litellm_params.model (the internal auto_router/... discriminator) via /model/new, /model/update, or PATCH /model/{id}/update. Invalid values—doubled prefix, stripped prefix, missing router name, or strategy config that does not match the chosen router kind—now fail with a 400 and an actionable message instead of persisting a row the router drops under ignore_invalid_deployments.

Introduces litellm/router_utils/auto_router_model_naming.py as the single source for classify_strategy_router_model and validate_strategy_router_model_write. Router._is_*_router_deployment delegates to the classifier (behavior unchanged). Management endpoints merge stored and incoming strategy-router fields and only validate when the write includes litellm_params.model, so partial patches and fixing already-corrupted rows stay allowed; public model_name renames are unaffected.

Reviewed by Cursor Bugbot for commit 1e04aee. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR prevents model-management writes from corrupting auto-router pseudo-models. The main changes are:

  • Shared classification for semantic, complexity, adaptive, and quality routers
  • Validation for create, update, and patch model endpoints
  • Actionable validation errors for malformed names and missing strategy settings
  • Tests for classification, rejected writes, partial patches, and row repair

Confidence Score: 5/5

This looks safe to merge.

  • The shared classifier preserves the existing prefix-based behavior.
  • Validation runs before persistence on each changed endpoint.
  • The tests cover malformed names, missing settings, partial patches, and repair writes.
  • No blocking issues were found in the changed code.

Important Files Changed

Filename Overview
litellm/router_utils/auto_router_model_naming.py Adds shared strategy-router classification and validation rules.
litellm/proxy/management_endpoints/model_management_endpoints.py Validates auto-router model writes before create, update, and patch persistence.
litellm/router.py Uses the shared classifier for the four strategy-router predicates.
tests/test_litellm/router_utils/test_auto_router_model_naming.py Tests router classification and coherent or malformed model values.
tests/test_litellm/proxy/management_endpoints/test_model_management_endpoints.py Tests validation across all three model-management write paths.

Reviews (2): Last reviewed commit: "fix(proxy): reject model writes that cor..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 74.64789% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...management_endpoints/model_management_endpoints.py 45.45% 18 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_lit4663_autorouter_prefix (1e04aee) with litellm_internal_staging (1a6642e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (1e04aee) during the generation of this report, so 1a6642e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit dcbff18. Configure here.

An auto-router deployment's litellm_params.model (auto_router/...) is the
discriminator the router loads it by, but the model management endpoints
accepted any client-supplied value verbatim; a doubled or stripped prefix
made router init fail on the next load and ignore_invalid_deployments
silently dropped the deployment. Validate writes that supply
litellm_params.model at all three endpoints against the merged params and
reject incoherent values with an actionable 400. Classification is
extracted to router_utils/auto_router_model_naming.py so the Router
predicates and the validation share one source
@tin-berri
tin-berri force-pushed the litellm_lit4663_autorouter_prefix branch from dcbff18 to 1e04aee Compare July 29, 2026 03:25
@tin-berri
tin-berri enabled auto-merge July 29, 2026 03:35
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1e04aee. Configure here.

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

LGTM. Thanks!

@tin-berri
tin-berri merged commit 9b48bf6 into litellm_internal_staging Jul 29, 2026
80 checks passed
@tin-berri
tin-berri deleted the litellm_lit4663_autorouter_prefix branch July 29, 2026 04:01
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.

2 participants