Repository navigation
fix(proxy): let configured pass-through endpoints outrank built-in provider prefix routes - #44133
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
1aebfea to
f4ad186
Compare
f4ad186 to
5d95ff8
Compare
|
@greptileai please re-review head 5d95ff8: opt-out flag added, auth docstrings updated, reorder made single pass, test helpers typed |
5d95ff8 to
44bbfdf
Compare
|
@greptileai please re-review head 44bbfdf, which only adds the new opt-out flag to the docs-test exclusion list after 5d95ff8 |
44bbfdf to
3f066ee
Compare
|
@greptileai please re-review head 3f066ee, a clean rebase of 44bbfdf onto current main with an identical patch |
3f066ee to
28dc700
Compare
|
bugbot run |
|
@greptileai please re-review head 28dc700, the same fix rebased onto current main as one commit. The only conflict was in |
28dc700 to
fbee6d5
Compare
|
@greptileai please review fbee6d5, the same single commit rebased onto main 0e26edf (clean rebase, no conflicts) |
|
bugbot run |
254b3c2 to
e820272
Compare
|
@greptileai please review head e820272, which reads the opt-out flag at call time and removes deleted pass-through routes, addressing both P1s |
|
bugbot run |
e820272 to
dab8c61
Compare
|
@greptileai please review dab8c61, which gates stale-route cleanup on the compatibility flag and drops a stray blank-line edit in constants.py |
|
bugbot run |
dab8c61 to
b467149
Compare
|
@greptileai please review b467149, where stale-route cleanup now matches path and overlapping methods, covering same-path endpoints with different methods |
|
bugbot run |
b467149 to
1e99c40
Compare
1e99c40 to
727e3dd
Compare
|
@greptileai please review 727e3dd, a typing-only follow-up to b467149 that keeps the route-cleanup logic identical and satisfies the type gates |
|
bugbot run |
…ovider prefix routes Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
727e3dd to
d6da2ee
Compare
|
@greptileai review please. Head d6da2ee is 727e3dd rebased onto main 30cbf6b with no patch change, verdict needs re-pinning |
TLDR
Problem this solves:
/typesafepass-through endpoint is answered by the built-in TypeSafe routemodelfrom the body and checks it against the keykey_model_access_deniedHow it solves it:
/<prefix>/{param:path}catch-all that would shadow themLITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=truerestores the previous order and the previous delete behavior, read when routes are ordered so it also works fromenvironment_variablesinconfig.yamlUser Flow
Before: a developer with a virtual key limited to one access group gets 403 from their own TypeSafe pass-through endpoint
/typesafetargetinghttps://api.typesafe.ai, with LiteLLM auth on, and grants the keyallowed_passthrough_routes: ["/typesafe"]"model": "openjev-4b"in the bodykey_model_access_denied, nothing reaches TypeSafe/typesafe/v1/systemoneAfter: the same request reaches TypeSafe through the configured endpoint
answersforjev-latest, TypeSafe's own 400 for a model it does not serve)pass_through_endpointand API basehttps://api.typesafe.ai/v1/...Captured on
30c0461bb7(this PR rebased onto main7ff5010674). The head727e3ddadbkeeps 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/typesaferoute lazy-loaded first, then/typesafecreated at runtime withPOST /config/pass_through_endpointon each proxy, all proxies writing to one Postgres databaseBefore, base main
7ff5010674(port 4011): the restricted key'sjev-latestrequest on the runtime-created/typesafe/v1/systemonelogged as Failure 403, call type/typesafe/v1/systemone, no API baseAfter, this PR (port 4012): the same key and byte-identical body logged as Success, call type
pass_through_endpoint, API basehttps://api.typesafe.ai/v1/...Earlier capture on
9f7d264aedwith the endpoint inconfig.yamlBefore, base
mainat6d4fa56ac4: the restricted key'sjev-latestrequest on/typesafe/v1/systemonelogged as Failure 403, call type/typesafe/v1/systemone, no API baseAfter, this PR: the same key and byte-identical body logged as Success, call type
pass_through_endpoint, API basehttps://api.typesafe.ai/v1/...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 withLITELLM_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/completionsAffected release
v1.102.1 is the first release with the built-in
/typesafe/{endpoint:path}route, v1.95.1 has none so a configured/typesafeendpoint served thereLinear ticket
Resolves LIT-9134
Files changed
litellm/proxy/route_priority.pyconfigured_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. Newshadowed_by_builtin_claim(path, builtin_routes)tells a runtime add whether the reorder is needed at all. Newbuiltin_pass_through_routes_first()reads the opt-out env flag each time routes are ordered. Leaf module, nolitellm.proxyimportslitellm/proxy/_lazy_features.py_register_featureand_restore_registry_orderbuild the route table through one_route_tablehelper that applies the new reorder beforehot_routes_first, skipped when the opt-out flag is setlitellm/proxy/pass_through_endpoints/pass_through_endpoints.pySafeRouteAdder.add_api_route_if_not_existsapplies 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. NewSafeRouteAdder.remove_unregistered_routesdrops marked pass-through routes that no entry in_registered_pass_through_routesbacks with the same path and an overlapping method, called at the end ofdelete_pass_through_endpointsandinitialize_pass_through_endpointsunless the opt-out flag is setlitellm/proxy/auth/auth_utils.pyrequest_dispatched_to_pass_through_endpointandget_model_from_requestnow describe which route wins on a collisionWhy 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 bodymodelas a LiteLLM model and enforces the key's model allowlist before anything is forwardedflowchart 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]Which built-in routes count as a claim on a prefix, with the configured route shown second:
/typesafe/{endpoint:path}/typesafe/{subpath:path}/typesafe/v1/systemone/zeta/{endpoint:path}/zeta/v1/thing/zeta/v1/thing/zeta/inner/{endpoint:path}/zeta/{subpath:path}/zeta/inner/x/zeta/same/zeta/same/zeta/same/zeta/{id:path}/search/zeta/{subpath:path}/zeta/a/searchPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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@greptileaito 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 headd6da2ee65b(727e3ddadbrebased onto main30cbf6bac7, same patch pergit range-diff). The built-in, runtime-created and delete rows were re-run onb467149d3d, 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 throughenvironment_variablesinconfig.yaml, and the deleted-endpoint cells ran on a proxy with no pass-through in configenvironment_variables: LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST: "true", configured/typesafe, openjev-4b403 key_model_access_denied, built-in route kept first403 key_model_access_denied/chat/completionsdefault-gpt200/typesafe, openjev-4b400 Unknown model: openjev-4bfrom TypeSafe200, modeljev-1.13.0/chat/completionsdefault-gpt200403 key_model_access_denied/typesafecreated at runtime, same request200, modeljev-1.13.0from TypeSafeDELETE /config/pass_through_endpoint?endpoint_id=..., restricted key403 key_model_access_denied, the built-in route serves the prefix again200, modeljev-1.13.0through the built-in routeUI screenshots are in User Flow above. Live run re-done on
30c0461bb7, this PR rebased onto main7ff5010674, with base main7ff5010674as the before arm;42f846e6afadds one test only, so the runtime tree is the same. Results match the earlier run on9f7d264aedcell for cell. Each proxy ran from source withPYTHONPATHpinned to its own checkout (base proxy importslitellm/proxy/route_priority.pyfrom the main checkout, which lacksconfigured_pass_through_routes_first; fixed proxy imports it from the PR worktree), one Postgres database, and the real TypeSafe API. The virtual keylit9134-restrictedhasmodels: ["default-models"](an access group holding onlydefault-gpt) andmetadata.allowed_passthrough_routes: ["/typesafe"]. Request bodies are byte-identical across arms (md51f70075cfor openjev-4b,9c790783for jev-latest)Config (keys redacted):
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>LITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=true/typesafe/v1/systemone, openjev-4b403 key_model_access_denied400 {"detail":{"error_type":"api_usage_error","message":"Unknown model: openjev-4b"}}from TypeSafe403 key_model_access_denied/typesafe/v1/systemone, jev-latest403 key_model_access_denied200 {"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-4b403 key_model_access_denied403 key_model_access_denied403 key_model_access_denied/chat/completions, default-gpt200200200No pass-through in config (built-in route only, ports 4011 base and 4012 this PR), then
/typesafecreated at runtime withPOST /config/pass_through_endpointafter the built-in route had lazy-loaded:/typesafe/v1/systemone, jev-latest, restricted key403 key_model_access_denied403 key_model_access_denied, the built-in route still enforces model access/typesafe/v1/systemone, jev-latest, master key200, modeljev-1.13.0200, modeljev-1.13.0/typesafe, openjev-4b403 key_model_access_denied400from TypeSafe/typesafe, jev-latest403 key_model_access_denied200from TypeSafe, modeljev-1.13.0Tests, all in existing mapped files:
tests/unit/proxy/test_route_priority.pydispatches real Starlette routes,tests/unit/proxy/test__lazy_features.pycovers a configured route added before and after a lazy feature loads plus the unmarked and unrelated-prefix cases, andtests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.pycoversadd_subpath_routeon a loaded lazy app. 2069 passed across the mapped files plustests/unit/proxy/auth/test_auth_utils.pyandtests/documentation_tests/test_env_keys.pyate820272200. Mutants run on that head:_route_tableignoring the reorder (killed by 2 tests), the runtime reorder inSafeRouteAdderdisabled (1),hoist_targetnever hoisting (4), the opt-out flag inverted (3). Dropping thebuiltin_idsguard so any/<prefix>/{name:path}route counts as a claim survived, so42f846e6afaddstest_a_prefix_catch_all_outside_the_builtin_routes_keeps_its_place, which kills it. Second-round mutants, all killed: cleanup removed fromdelete_pass_through_endpoints, cleanup removed frominitialize_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:
7ff5010674: clean. On the earlier rebase the only conflict wasSafeRouteAdder.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 bothLITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST, live-verified to restore the base behavior, including when set throughconfig.yaml(Greptile finding on import-time evaluation, fixed)(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/<prefix>/{name:path}routes, so unrelated/v1/...routes keep their order, pinned by the deeper-prefix and trailing-literal testsallowed_passthrough_routesgrant still gates them (a key without it gets 403 on both builds), and ordinary LLM routes still return 403SafeRouteAdderhook, config endpoints by the lazy feature hook, both proven liveOptional, baredictorAnyadded. LIT010: every variable the PR adds, tests included, carriesFinal(Greptile P2 on9f7d264aed, fixed in72892c73ed)route_priority.pyhas nolitellm.proxyimport_registered_pass_through_routesand the env flag throughmonkeypatchType
Bug Fix
Caveats (if any)
Medium
/<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 pathLITELLM_BUILTIN_PASS_THROUGH_ROUTES_FIRST=truerestores the previous order, a docs entry belongs in litellm-docsauth: truepass-through also needsallowed_passthrough_routeson the key or team/v1catch-all created at runtime still does not reach lazy/v1/access_groupLow
test_pass_through_endpoints.pyshows three formatter-only hunksLink 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