Skip to content

fix(proxy): let configured pass-through endpoints outrank built-in provider prefix routes - #44133

Merged
yassin-berriai merged 1 commit into
mainfrom
litellm_typesafe_configured_passthrough_precedence
Oct 10, 2026
Merged

yassin-berriai merged 1 commit into
mainfrom
litellm_typesafe_configured_passthrough_precedence

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • A configured /typesafe pass-through endpoint is answered by the built-in TypeSafe route
  • That route reads model from the body and checks it against the key
  • Keys limited to an access group get 403 key_model_access_denied

How it solves it:

  • Configured pass-through routes now outrank a built-in /<prefix>/{param:path} catch-all that would shadow them
  • Applied when lazy features load and when an endpoint is created at runtime
  • Exact routes and other built-in shapes keep their current order
  • Deleting a configured endpoint, or dropping it from config, now removes its route, so the built-in route gets the prefix back
  • Setting LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=true restores the previous order and the previous delete behavior, read when routes are ordered so it also works from environment_variables in config.yaml

User Flow

Before: a developer with a virtual key limited to one access group gets 403 from their own TypeSafe pass-through endpoint

  1. The proxy admin configures a pass-through endpoint at /typesafe targeting https://api.typesafe.ai, with LiteLLM auth on, and grants the key allowed_passthrough_routes: ["/typesafe"]
  2. The developer sends POST https://litellm-domain/typesafe/v1/systemone with "model": "openjev-4b" in the body
  3. The proxy answers 403 key_model_access_denied, nothing reaches TypeSafe
  4. https://litellm-domain/ui/?page=logs shows the request as Failure with error code 403 and call type /typesafe/v1/systemone

After: the same request reaches TypeSafe through the configured endpoint

  1. The proxy admin configures the same pass-through endpoint and grant
  2. The developer sends the same POST https://litellm-domain/typesafe/v1/systemone
  3. The response comes from TypeSafe itself (200 with answers for jev-latest, TypeSafe's own 400 for a model it does not serve)
  4. https://litellm-domain/ui/?page=logs shows the request as Success with call type pass_through_endpoint and API base https://api.typesafe.ai/v1/...

Captured on 30c0461bb7 (this PR rebased onto main 7ff5010674). The head 727e3ddadb keeps this create flow unchanged and only adds route removal on delete and config sync, proven in the second-round table below. No pass-through in config, the built-in /typesafe route lazy-loaded first, then /typesafe created at runtime with POST /config/pass_through_endpoint on each proxy, all proxies writing to one Postgres database

Before, base main 7ff5010674 (port 4011): the restricted key's jev-latest request on the runtime-created /typesafe/v1/systemone logged as Failure 403, call type /typesafe/v1/systemone, no API base

Admin UI Logs, before on main 7ff5010674: runtime-created endpoint still answered 403 by the built-in route

After, this PR (port 4012): the same key and byte-identical body logged as Success, call type pass_through_endpoint, API base https://api.typesafe.ai/v1/...

Admin UI Logs, after on 30c0461bb7: Success, call type pass_through_endpoint, API base api.typesafe.ai

Admin UI Logs list on 30c0461bb7

Earlier capture on 9f7d264aed with the endpoint in config.yaml

Before, base main at 6d4fa56ac4: the restricted key's jev-latest request on /typesafe/v1/systemone logged as Failure 403, call type /typesafe/v1/systemone, no API base

Admin UI Logs, before: 403 key_model_access_denied for jev-latest

After, this PR: the same key and byte-identical body logged as Success, call type pass_through_endpoint, API base https://api.typesafe.ai/v1/...

Admin UI Logs, after: Success, call type pass_through_endpoint, API base api.typesafe.ai

Request Logs list from that run: 16:19:41 rows are base main, 16:19:46 rows are this PR, 16:19:50 rows are this PR with LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=true; each group is openjev-4b on /typesafe, jev-latest on /typesafe, openjev-4b on /chat/completions, default-gpt on /chat/completions

Admin UI Logs list with base, fixed and opt-out rows

Affected release

v1.102.1 is the first release with the built-in /typesafe/{endpoint:path} route, v1.95.1 has none so a configured /typesafe endpoint served there

Linear ticket

Resolves LIT-9134

Files changed

File What changed and why
litellm/proxy/route_priority.py New configured_pass_through_routes_first(routes, builtin_routes): moves a configured pass-through route just ahead of the first earlier built-in route shaped /<literal prefix>/{name:path} whose prefix it starts with, in one pass over a prefix -> earliest index map. New shadowed_by_builtin_claim(path, builtin_routes) tells a runtime add whether the reorder is needed at all. New builtin_pass_through_routes_first() reads the opt-out env flag each time routes are ordered. Leaf module, no litellm.proxy imports
litellm/proxy/_lazy_features.py _register_feature and _restore_registry_order build the route table through one _route_table helper that applies the new reorder before hot_routes_first, skipped when the opt-out flag is set
litellm/proxy/pass_through_endpoints/pass_through_endpoints.py SafeRouteAdder.add_api_route_if_not_exists applies the same reorder when an endpoint added after lazy routes loaded (DB or UI created endpoints) is shadowed by a built-in catch-all, unless the opt-out flag is set. It runs after main's exact-collision placement, so both orderings hold. New SafeRouteAdder.remove_unregistered_routes drops marked pass-through routes that no entry in _registered_pass_through_routes backs with the same path and an overlapping method, called at the end of delete_pass_through_endpoints and initialize_pass_through_endpoints unless the opt-out flag is set
litellm/proxy/auth/auth_utils.py Docstrings of request_dispatched_to_pass_through_endpoint and get_model_from_request now describe which route wins on a collision

Why the order matters: Starlette dispatches to the first route that fully matches. The built-in TypeSafe route /typesafe/{endpoint:path} lives in a lazy feature and was inserted ahead of the configured /typesafe/{subpath:path} route. The built-in handler does not carry the pass-through marker, so auth treats the body model as a LiteLLM model and enforces the key's model allowlist before anything is forwarded

flowchart LR
    R[POST /typesafe/v1/systemone] --> M{first full match}
    M -- before --> B[built-in /typesafe/endpoint:path] --> A[auth reads body model] --> D[403 key_model_access_denied]
    M -- after --> C[configured /typesafe/subpath:path] --> P[auth skips body model] --> U[forwarded to api.typesafe.ai]
Loading

Which built-in routes count as a claim on a prefix, with the configured route shown second:

Built-in route Configured route Request Winner after this PR
/typesafe/{endpoint:path} /typesafe/{subpath:path} /typesafe/v1/systemone configured
/zeta/{endpoint:path} /zeta/v1/thing /zeta/v1/thing configured
/zeta/inner/{endpoint:path} /zeta/{subpath:path} /zeta/inner/x built-in (configured path does not start with the deeper prefix)
/zeta/same /zeta/same /zeta/same built-in (exact routes keep order)
/zeta/{id:path}/search /zeta/{subpath:path} /zeta/a/search built-in (not a terminal catch-all)

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/unit/<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

Second round. The flag rows ran on e820272200, whose route ordering is unchanged at the head d6da2ee65b (727e3ddadb rebased onto main 30cbf6bac7, same patch per git range-diff). The built-in, runtime-created and delete rows were re-run on b467149d3d, which only differs from the head in type annotations. Each proxy ran from the PR worktree with its own Postgres database, the flag cells set it only through environment_variables in config.yaml, and the deleted-endpoint cells ran on a proxy with no pass-through in config

Cell Observable
environment_variables: LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST: "true", configured /typesafe, openjev-4b 403 key_model_access_denied, built-in route kept first
Same, jev-latest 403 key_model_access_denied
Same, /chat/completions default-gpt 200
No flag, configured /typesafe, openjev-4b 400 Unknown model: openjev-4b from TypeSafe
No flag, jev-latest 200, model jev-1.13.0
No flag, /chat/completions default-gpt 200
Built-in only, restricted key, jev-latest 403 key_model_access_denied
/typesafe created at runtime, same request 200, model jev-1.13.0 from TypeSafe
After DELETE /config/pass_through_endpoint?endpoint_id=..., restricted key 403 key_model_access_denied, the built-in route serves the prefix again
After delete, master key 200, model jev-1.13.0 through the built-in route

UI screenshots are in User Flow above. Live run re-done on 30c0461bb7, this PR rebased onto main 7ff5010674, with base main 7ff5010674 as the before arm; 42f846e6af adds one test only, so the runtime tree is the same. Results match the earlier run on 9f7d264aed cell for cell. Each proxy ran from source with PYTHONPATH pinned to its own checkout (base proxy imports litellm/proxy/route_priority.py from the main checkout, which lacks configured_pass_through_routes_first; fixed proxy imports it from the PR worktree), one Postgres database, and the real TypeSafe API. The virtual key lit9134-restricted has models: ["default-models"] (an access group holding only default-gpt) and metadata.allowed_passthrough_routes: ["/typesafe"]. Request bodies are byte-identical across arms (md5 1f70075c for openjev-4b, 9c790783 for jev-latest)

Config (keys redacted):

model_list:
  - model_name: default-gpt
    litellm_params:
      model: openai/gpt-5.4-mini
      api_key: os.environ/OPENAI_API_KEY
    model_info:
      access_groups: ["default-models"]
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  pass_through_endpoints:
    - path: "/typesafe"
      target: "https://api.typesafe.ai"
      headers:
        Authorization: "Bearer os.environ/TYPESAFE_API_KEY"
      auth: true
      include_subpath: true

The openjev-4b body, the jev-latest body differs only in model:

{"model": "openjev-4b", "state": "A dog is running through a park. There are no cats in the park.", "questions": {"animal": {"type": "choice", "instructions": "Which statement is supported by the state?", "criteria": {"dog": "A dog is running through a park.", "cat": "A cat is running through a park."}}}}

Each cell is curl -s localhost:<port><path> -H "Authorization: Bearer $VK" -H 'Content-Type: application/json' --data-binary @<fixture>

Cell Base main This PR This PR, LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=true
Configured /typesafe/v1/systemone, openjev-4b 403 key_model_access_denied 400 {"detail":{"error_type":"api_usage_error","message":"Unknown model: openjev-4b"}} from TypeSafe 403 key_model_access_denied
Configured /typesafe/v1/systemone, jev-latest 403 key_model_access_denied 200 {"model":"jev-1.13.0","answers":{"animal":{"choice":"dog","confidence":1.0,...}},"usage":{"input_tokens":338,"output_tokens":31}} 403 key_model_access_denied
/chat/completions, openjev-4b 403 key_model_access_denied 403 key_model_access_denied 403 key_model_access_denied
/chat/completions, default-gpt 200 200 200

No pass-through in config (built-in route only, ports 4011 base and 4012 this PR), then /typesafe created at runtime with POST /config/pass_through_endpoint after the built-in route had lazy-loaded:

Cell Base main This PR
Built-in only, /typesafe/v1/systemone, jev-latest, restricted key 403 key_model_access_denied 403 key_model_access_denied, the built-in route still enforces model access
Built-in only, /typesafe/v1/systemone, jev-latest, master key 200, model jev-1.13.0 200, model jev-1.13.0
Runtime-created /typesafe, openjev-4b 403 key_model_access_denied 400 from TypeSafe
Runtime-created /typesafe, jev-latest 403 key_model_access_denied 200 from TypeSafe, model jev-1.13.0

Tests, all in existing mapped files: tests/unit/proxy/test_route_priority.py dispatches real Starlette routes, tests/unit/proxy/test__lazy_features.py covers a configured route added before and after a lazy feature loads plus the unmarked and unrelated-prefix cases, and tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py covers add_subpath_route on a loaded lazy app. 2069 passed across the mapped files plus tests/unit/proxy/auth/test_auth_utils.py and tests/documentation_tests/test_env_keys.py at e820272200. Mutants run on that head: _route_table ignoring the reorder (killed by 2 tests), the runtime reorder in SafeRouteAdder disabled (1), hoist_target never hoisting (4), the opt-out flag inverted (3). Dropping the builtin_ids guard so any /<prefix>/{name:path} route counts as a claim survived, so 42f846e6af adds test_a_prefix_catch_all_outside_the_builtin_routes_keeps_its_place, which kills it. Second-round mutants, all killed: cleanup removed from delete_pass_through_endpoints, cleanup removed from initialize_pass_through_endpoints, cleanup dropping every marked route, the subpath path mapping broken, the flag read once at import, the flag guard dropped from the initialize call site, path-only matching (leaves a deleted GET route next to a live POST one at the same path), and exact method-set matching (drops a route still shared by an overlapping endpoint)

Taxonomy audit (A-BB) on the rebased diff:

  • Rebase onto 7ff5010674: clean. On the earlier rebase the only conflict was SafeRouteAdder.add_api_route_if_not_exists, where main added exact-collision placement (_placed_ahead). Both now run, exact collision first, then the catch-all reorder; the 641 mapped tests cover both
  • C1-C7: the precedence change ships with the opt-out flag LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST, live-verified to restore the base behavior, including when set through config.yaml (Greptile finding on import-time evaluation, fixed)
  • C2: a deleted pass-through path now answers 404 instead of the stale route's 401, because its route is removed (Greptile finding on the stale route blocking the built-in one). Two existing tests change (200, 401, 200) to (200, 404, 200) for that reason, the only removed assert lines. With the opt-out flag the stale route stays, pinned by a test
  • C5: claims are limited to terminal /<prefix>/{name:path} routes, so unrelated /v1/... routes keep their order, pinned by the deeper-prefix and trailing-literal tests
  • D, P, Z: auth code is unchanged. Configured routes already skip body-model extraction through the existing marker, main's allowed_passthrough_routes grant still gates them (a key without it gets 403 on both builds), and ordinary LLM routes still return 403
  • F3: runtime-created endpoints are covered by the SafeRouteAdder hook, config endpoints by the lazy feature hook, both proven live
  • H2: no new source comments. H4, H6: no Optional, bare dict or Any added. LIT010: every variable the PR adds, tests included, carries Final (Greptile P2 on 9f7d264aed, fixed in 72892c73ed)
  • CodeQL cyclic import: route_priority.py has no litellm.proxy import
  • T1-T5, T17: tests dispatch through real routes and restore _registered_pass_through_routes and the env flag through monkeypatch
  • A, B, E, G, M, N, O, U-Y, AA, BB: not applicable, no cache, spend, streaming, guardrail, OAuth or provider transformation code touched

Type

Bug Fix

Caveats (if any)

Medium

  • Route precedence changes only where a built-in catch-all shadowed a configured endpoint
    • An admin who configured /<provider> (for example /anthropic) while relying on the built-in route now gets their configured target, so model-access checks and built-in cost tracking no longer apply to that path
    • LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=true restores the previous order, a docs entry belongs in litellm-docs
  • On current main an auth: true pass-through also needs allowed_passthrough_routes on the key or team
    • That requirement comes from main, not this PR; without it the restored endpoint answers 403 asking for the grant
  • A configured /v1 catch-all created at runtime still does not reach lazy /v1/access_group
    • Same 404 on the base and on this PR, existing lazy-loading behavior, out of scope

Low

  • test_pass_through_endpoints.py shows three formatter-only hunks
    • CI checks ruff format on every touched file and the base file was not formatted

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

@devin-ai-integration

devin-ai-integration Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

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

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High impact] The PR appears safe to merge; no new blocking issue was established.

Summary

Configured pass-through endpoints now move ahead of overlapping built-in provider catch-alls.

  • Configured pass-through routes can take priority over matching built-in catch-alls.
  • Deleted pass-through endpoints stop leaving routes behind.

Reviews (16) · Last reviewed commit: "fix(proxy): let configured pass-through ..." · Reviewed by Greptile

Comment thread litellm/proxy/route_priority.py Resolved
Comment thread litellm/proxy/route_priority.py Resolved
Comment thread litellm/proxy/route_priority.py Resolved
Comment thread tests/unit/proxy/test_route_priority.py Resolved
@codspeed

codspeed Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_typesafe_configured_passthrough_precedence (d6da2ee) with main (9b53ee8)1

Open in CodSpeed

Footnotes

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

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.89041% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...y/pass_through_endpoints/pass_through_endpoints.py 90.32% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch 2 times, most recently from 1aebfea to f4ad186 Compare October 2, 2026 03:20
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from f4ad186 to 5d95ff8 Compare October 2, 2026 03:21
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review head 5d95ff8: opt-out flag added, auth docstrings updated, reorder made single pass, test helpers typed

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from 5d95ff8 to 44bbfdf Compare October 2, 2026 03:24
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review head 44bbfdf, which only adds the new opt-out flag to the docs-test exclusion list after 5d95ff8

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from 44bbfdf to 3f066ee Compare October 2, 2026 06:07
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review head 3f066ee, a clean rebase of 44bbfdf onto current main with an identical patch

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review head 28dc700, the same fix rebased onto current main as one commit. The only conflict was in SafeRouteAdder.add_api_route_if_not_exists, where main's exact-collision placement and this PR's lazy catch-all precedence now both 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.

Stale Bugbot comment from a previous run.

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from 28dc700 to fbee6d5 Compare October 8, 2026 10:53
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please review fbee6d5, the same single commit rebased onto main 0e26edf (clean rebase, no conflicts)

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please review head e820272, which reads the opt-out flag at call time and removes deleted pass-through routes, addressing both P1s

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

bugbot run

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from e820272 to dab8c61 Compare October 10, 2026 09:50
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please review dab8c61, which gates stale-route cleanup on the compatibility flag and drops a stray blank-line edit in constants.py

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

bugbot run

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from dab8c61 to b467149 Compare October 10, 2026 10:04
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please review b467149, where stale-route cleanup now matches path and overlapping methods, covering same-path endpoints with different methods

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

bugbot run

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from b467149 to 1e99c40 Compare October 10, 2026 12:12
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from 1e99c40 to 727e3dd Compare October 10, 2026 12:21
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai please review 727e3dd, a typing-only follow-up to b467149 that keeps the route-cleanup logic identical and satisfies the type gates

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

bugbot run

…ovider prefix routes

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_typesafe_configured_passthrough_precedence branch from 727e3dd to d6da2ee Compare October 10, 2026 18:34
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai review please. Head d6da2ee is 727e3dd rebased onto main 30cbf6b with no patch change, verdict needs re-pinning

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai

@yassin-berriai
yassin-berriai merged commit 77b6d04 into main Oct 10, 2026
86 of 87 checks passed
@yassin-berriai
yassin-berriai deleted the litellm_typesafe_configured_passthrough_precedence branch October 10, 2026 19:26

This branch had an error being deployed

1 failed deployment
e2e-changed — d6da2ee6 Deployed Oct 10, 2026 by devin-ai-integration[bot] via oauth #4413
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