Backtestslitellmrun-20261111-220245#48760

Issue #48760 Merged fix PR #51226 Run 11 Nov 2026 · run-20261111-220245 BerriAI/litellm

key model allowlist is enforced per-route, so generateContent, streamGenerateContent and image routes still serve a denied model (gap left by #41852)

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches close the three routes named in the issue, and the independent tests confirm it: all four denied-URL cases go from FAIL-ASSERT on base to PASS on both A and B, with the controls unchanged. The difference is in how each gets there and what else it risks.

Patch A changes `get_model_from_request` to prefer the URL model whenever the FastAPI-dispatched handler actually binds a `model`/`model_name` path parameter or a `model` query parameter, read from `scope["route"]`. That is route-agnostic in the sense the issue asks for: generateContent, streamGenerateContent, the deployment image routes, `/engines/{model}/...`, `/openai/deployments/{model}/chat/...` and `?model=` are all covered without a template list, and any future `{model}` handler is covered automatically. It also only trusts path params the handler declares, so the budget-fallback's planted `path_params["model"]` does not leak into routes that never read it (tested explicitly). It keeps the rest of the existing resolution flow intact by assigning into `model` and letting the remaining post-processing run. The change is confined to auth_utils, and its tests are end-to-end through the real app with TestClient, asserting both the 403 and that the premium deployment's mock output never appears, plus positive cases (allowed URL model with denied body model is served, streaming, multipart edits). Those are exactly the issue's reproduction scenarios.

Patch B hardcodes three regexes for the affected routes and replays `resolve_inference_model` with general_settings/user_model to mirror the handler's precedence, then threads `path_params` through the budget-reservation estimators. It is correct for the listed routes and the reservation work is a reasonable adjacent improvement, but it is the template-list approach the issue explicitly warns leaves other routes (e.g. `/engines/{model}/`, `?model=`) and future routes open. It also returns early before the remainder of `get_model_from_request`'s candidate/post-processing logic, skipping whatever normalization follows, and it spreads the change across five production files (including a new `user_model_view` in proxy_server and an intra-function import of proxy_server), which widens the blast radius for a fix the issue scopes to the access check. Its tests are solid unit tests but mostly exercise `get_model_from_request` directly rather than the served path.

On correctness both resolve the reported bypass; A addresses the root cause more generally and with less churn, and its tests more directly reproduce the issue. B's extra reservation plumbing is neutral-to-slightly-risky scope. A wins on a moderate margin.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

1h 20m

Wall-clock time in the container

Full test suite

Not run

The harness did not reach the test stage

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

256vs 326 merged

Changed lines, ours vs merged

what happens

a virtual key is scoped to one model with models: ["cheap-model"]. the proxy correctly blocks the denied model when it is named in the request body (403 key_model_access_denied). but on three routes the key is served the denied model anyway, as long as the denied model is in the url and an allowed model is in the body:

  • POST /v1beta/models/{model}:generateContent
  • POST /v1beta/models/{model}:streamGenerateContent
  • POST /openai/deployments/{model}/images/generations (and /images/edits)

the model that actually runs is not bounded by the key's allowed list on these routes.

this is the same class as the open report #41810 (chat route) and its open fix #41852. that fix only lists the chat / completions / embeddings routes, so these routes stay open after it. i applied #41852 and re-tested: the chat routes become 403, but generateContent, streamGenerateContent and images still return 200 and serve the denied model.

why it happens

two parts of the proxy read the requested model from different places.

flowchart TB
    R["one request"] --> B["body says: cheap-model (allowed)"]
    R --> U["url path says: premium-model (denied)"]
    B --> G{"access check reads the BODY<br/>get_model_from_request -> allow"}
    U --> H{"handler routes on the URL<br/>resolve_inference_model prefers endpoint model"}
    G --> P["request passes the check"]
    H --> S["denied model is served and billed"]
    P --> S
  • the access check get_model_from_request (litellm/proxy/auth/auth_utils.py) reads the body model first, and only falls back to the url model when the body has none. with an allowed model in the body, it approves.
  • the handler passes the url path model into the request processor (litellm/proxy/google_endpoints/endpoints.py, litellm/proxy/image_endpoints/endpoints.py), and resolve_inference_model (litellm/proxy/common_utils/http_parsing_utils.py) prefers that endpoint model over the body. so routing uses the denied model.

the safety net only works when the body is silent: with no body model the access check reads the url model and correctly returns 403. the bypass needs both fields, an allowed body model to pass the check and a denied url model for the handler to route on.

exact reproduction

tested on main 1176e608b19 (2026-10-28), also on cfe0100e / v1.106.4. no provider keys needed; the two upstreams below are local stubs, so the served model is visible in the forwarded request.

config:

model_list:
  - model_name: cheap-model
    litellm_params:
      model: openai/cheap-upstream
      api_base: http://127.0.0.1:4190/v1
      api_key: dummy
  - model_name: premium-model
    litellm_params:
      model: openai/premium-upstream
      api_base: http://127.0.0.1:4190/v1
      api_key: dummy
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

make a key scoped to the cheap model only:

curl -s $PROXY/key/generate -H "Authorization: Bearer $MASTER_KEY" \
  -H 'content-type: application/json' \
  -d '{"models":["cheap-model"]}'

control, denied model in the body, correctly blocked:

curl -s $PROXY/v1/chat/completions -H "Authorization: Bearer $KEY" \
  -H 'content-type: application/json' \
  -d '{"model":"premium-model","messages":[{"role":"user","content":"hi"}]}'
# HTTP 403
# {"error":{"message":"The requested model 'premium-model' is not available for this API key ...","type":"key_model_access_denied","code":"403"}}

bypass, denied model in the url, allowed model in the body:

curl -s "$PROXY/v1beta/models/premium-model:generateContent" -H "x-goog-api-key: $KEY" \
  -H 'content-type: application/json' \
  -d '{"model":"cheap-model","contents":[{"role":"user","parts":[{"text":"hi"}]}]}'
# HTTP 200

the forwarded request to the upstream shows the denied model actually ran:

POST /v1/chat/completions
{"model":"premium-upstream","messages":[{"role":"user","content":"hi"}]}

the same works on :streamGenerateContent and on POST /openai/deployments/premium-model/images/generations with body {"model":"cheap-model","prompt":"..."}.

results

each case run 5 times, on stock main and on main with the open fix #41852 applied. "served" is the model id in the forwarded upstream request.

route, with denied model in url and allowed in bodystock mainmain + #41852
/v1/chat/completions?model=200, served denied403 (fixed)
/openai/deployments/{denied}/chat/completions200, served denied403 (fixed)
:generateContent200, served denied200, still served denied
:streamGenerateContent200, served denied200, still served denied
/openai/deployments/{denied}/images/generations200, served denied200, still served denied
control: denied model in body403403
control: allowed in url and body200, served allowed200, served allowed
control: /v1/messages?model=denied body allowed200, served allowed (not affected)200, served allowed

billing follows the served model: on generateContent the denied model is billed to the key at its rate. a fresh key scoped to the cheap model with max_budget: 1.0 is drained by five generateContent calls and the sixth returns 422 budget_exceeded, so the denied model also spends against the key's budget.

a full writeup with the raw request/response/forwarded bytes, the 5-of-5 matrix, the fix-control run, and the spend rows is here, with plain-language diagrams: https://kairo-allowlist-explainer.vercel.app

why this matters

the per-key models list is the operator's way to bound cost and model access for a key (for example a key limited to one cheap or local model for a tenant). on these routes that boundary is not enforced, so a key holder reaches and spends on models the operator never granted. it is a 403 on the body today, so the gap is a per-route inconsistency, not a design choice.

honest scope: this needs a valid authenticated key and a hand-written request (the google genai sdks send the model only in the url, no body model, which is correctly blocked). so it is a scoped key holder bypassing their own scope, not an unauthenticated attack or a cross-tenant data leak. by the categories in SECURITY.md this is a p2 (authenticated action beyond permissions). i am raising it here because the same class is already public in #41810 and #41852; happy to move it to a private advisory with a screen recording if you prefer that channel.

suggested fix (route-agnostic, so new routes are covered too)

#41852 fixes this by listing route templates. that works for the routes it lists, but any route not on the list (these three, and any added later) stays open.

flowchart LR
    Fix["#41852 route list"] -->|covers| Chat["chat / completions / embeddings"]
    Fix -.->|misses| Gc["generateContent / streamGenerateContent"]
    Fix -.->|misses| Img["images routes"]
    Fix -.->|misses| New["any future route"]

every model-serving route funnels through one place, ProxyBaseLLMRequestProcessing.base_process_llm_request in litellm/proxy/common_request_processing.py, right after resolve_inference_model sets self.data["model"] to the model that will actually run. re-checking the key and team allowlist against that resolved model there (reusing can_key_call_model / can_team_access_model from auth_checks.py, and honoring the existing router-fallback access rules) closes every route at once, because it checks the model that is truly dispatched rather than guessing per route.

flowchart TB
    A["chat"] --> F
    B["generateContent / stream"] --> F
    C["images"] --> F
    D["responses, messages, video, ocr, ... (one funnel)"] --> F
    F["base_process_llm_request<br/>self.data['model'] = resolve_inference_model(...)"] --> K{"re-check the resolved model<br/>against the key/team allowlist"}
    K -->|allowed| Run["dispatch"]
    K -->|not allowed| Deny["403 key_model_access_denied"]

i have this fix working locally and can open a pr with tests that cover generateContent, images and a chat route if that direction is welcome. i am also glad to add the image and generateContent templates to #41852 instead if you would rather keep the current approach.

The two patches

Our patch+255 −1
litellm/proxy/auth/auth_utils.pyindex 7413490e44..d7acd2dd83 100644@@ -11,6 +11,7 @@ from types import MappingProxyType from typing import Any, Final, Literal, Protocol, TypeAlias  from fastapi import HTTPException, Request, status+from fastapi.routing import APIRoute from pydantic import BaseModel, ConfigDict, PositiveInt, TypeAdapter, ValidationError  import litellm@@ -2698,7 +2699,10 @@ def get_model_from_request(         llm_router=llm_router,         team_id=team_id,     )-    model = _format_model_candidates(candidates)+    endpoint_model: Final = _served_endpoint_model(+        route=route, request=request, request_query_params=request_query_params+    )+    model = endpoint_model if endpoint_model is not None else _format_model_candidates(candidates)     if model is None:         model = _model_from_route_path(route) @@ -2719,6 +2723,60 @@ def get_model_from_request(     return model  +_ENDPOINT_MODEL_PATH_PARAMS: Final = ("model", "model_name")+++def _dispatched_handler_route(request: Request) -> APIRoute | None:+    """The FastAPI route that dispatched this request (``scope["route"]``), if any."""+    scope: Final[Mapping[str, object] | None] = getattr(request, "scope", None)+    route: Final = scope.get("route") if scope is not None else None+    return route if isinstance(route, APIRoute) else None+++def _dispatched_handler_query_params(request: Request) -> frozenset[str]:+    """Names of the query parameters the FastAPI-dispatched handler declares (``scope["route"]``)."""+    route: Final = _dispatched_handler_route(request)+    return frozenset() if route is None else frozenset(param.alias for param in route.dependant.query_params)+++def _dispatched_handler_path_params(request: Request, path_params: Mapping[str, object]) -> frozenset[str]:+    """Path parameters the dispatched handler binds.++    Auth rewrites (the budget fallback) plant ``path_params["model"]`` on every route; only a parameter the+    handler declares reaches it, so a planted name the handler never reads must not stand in for its model.+    """+    route: Final = _dispatched_handler_route(request)+    return frozenset(path_params) if route is None else frozenset(param.alias for param in route.dependant.path_params)+++def _served_endpoint_model(+    route: str, request: Request | None, request_query_params: Mapping[str, object] | None+) -> str | None:+    """The URL model an LLM handler routes on, which outranks the body ``model``.++    Handlers bound to a ``{model}`` / ``{model_name}`` path parameter (``/openai/deployments/{model}/...``,+    ``/engines/{model}/...``, ``/v1beta/models/{model_name}:generateContent``, ...), or that declare a ``model``+    query parameter (``/v1/chat/completions?model=``), hand that value to ``resolve_inference_model`` as the+    endpoint model, and it wins over the body. Model-access checks must judge that model, not the body one,+    or an allowed body model would admit a denied URL model. Read from the dispatched route, so every handler+    with such a parameter is covered without listing route templates.+    """+    if request is None or not RouteChecks.is_llm_api_route(route=route):+        return None+    path_params: Final[Mapping[str, object]] = getattr(request, "path_params", None) or {}+    handler_path_params: Final = _dispatched_handler_path_params(request, path_params)+    for name in _ENDPOINT_MODEL_PATH_PARAMS:+        if name not in handler_path_params:+            continue+        path_model = path_params.get(name)+        if isinstance(path_model, str) and path_model:+            return path_model+    if request_query_params is None or "model" not in _dispatched_handler_query_params(request):+        return None+    query_model: Final = request_query_params.get("model")+    return query_model if isinstance(query_model, str) and query_model else None++ def _model_from_route_path(route: str) -> str | None:     """     Google generateContent-style routes allow "/" inside the model id, e.g.tests/unit/proxy/auth/test_url_model_allowlist.pynew file mode 100644index 0000000000..1195094586@@ -0,0 +1,196 @@+"""A key's model allowlist judges the model a route serves, including a model named in the URL.++Handlers bound to ``{model}`` / ``{model_name}`` path parameters or a ``model`` query parameter route on that+URL model ahead of the body ``model``, so an allowed body model must not admit a denied URL model.+"""++import json+import time+from typing import Final+from unittest.mock import MagicMock++import httpx+import pytest+from fastapi import Request+from fastapi.routing import APIRoute+from fastapi.testclient import TestClient++import litellm+from litellm import Router+from litellm.proxy import proxy_server+from litellm.proxy._types import UserAPIKeyAuth+from litellm.proxy.auth.auth_utils import get_model_from_request+from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache+from litellm.proxy.proxy_server import app, hash_token++_KEY: Final = "sk-cheap-only-key"+_CHAT_BODY: Final = {"model": "cheap-model", "messages": [{"role": "user", "content": "hi"}]}+_GOOGLE_BODY: Final = {"model": "cheap-model", "contents": [{"role": "user", "parts": [{"text": "hi"}]}]}+_IMAGE_BODY: Final = {"model": "cheap-model", "prompt": "a cat"}+_EMBEDDING_BODY: Final = {"model": "cheap-model", "input": "hi"}+++@pytest.fixture+def cheap_only_key(monkeypatch: pytest.MonkeyPatch) -> str:+    """A virtual key scoped to ``cheap-model``; each deployment's mock response names the model it serves."""+    router: Final = Router(+        model_list=[+            {+                "model_name": name,+                "litellm_params": {+                    "model": f"openai/{name}-upstream",+                    "api_key": "dummy",+                    "mock_response": f"served-by-{name}",+                },+            }+            for name in ("cheap-model", "premium-model")+        ]+    )+    cache: Final = UserApiKeyCache()+    cache.set_cache(+        key=hash_token(_KEY),+        value=UserAPIKeyAuth(token=hash_token(_KEY), models=["cheap-model"], last_refreshed_at=time.time()),+    )+    monkeypatch.setattr(proxy_server, "master_key", "sk-master-1234")+    monkeypatch.setattr(proxy_server, "prisma_client", MagicMock())+    monkeypatch.setattr(proxy_server, "user_api_key_cache", cache)+    monkeypatch.setattr(proxy_server, "llm_router", router)+    monkeypatch.setattr(proxy_server, "llm_model_list", router.model_list)+    monkeypatch.setattr(proxy_server, "general_settings", {})+    monkeypatch.setattr(proxy_server, "user_model", None)+    monkeypatch.setattr(litellm, "callbacks", [])+    return _KEY+++def _post(path: str, body: dict, key: str) -> httpx.Response:+    return TestClient(app, raise_server_exceptions=False).post(+        path, json=body, headers={"Authorization": f"Bearer {key}", "x-goog-api-key": key}+    )+++def _streamed_text(response: httpx.Response) -> str:+    """The text of a streamGenerateContent SSE body, reassembled from its chunked parts."""+    events: Final = (+        json.loads(line.removeprefix("data: ")) for line in response.text.splitlines() if line.startswith("data: ")+    )+    return "".join(+        part.get("text", "")+        for event in events+        for candidate in event["candidates"]+        for part in candidate["content"]["parts"]+    )+++@pytest.mark.parametrize(+    ("path", "body"),+    [+        ("/v1beta/models/premium-model:generateContent", _GOOGLE_BODY),+        ("/models/premium-model:generateContent", _GOOGLE_BODY),+        ("/v1beta/models/premium-model:streamGenerateContent", _GOOGLE_BODY),+        ("/models/premium-model:streamGenerateContent", _GOOGLE_BODY),+        ("/openai/deployments/premium-model/images/generations", _IMAGE_BODY),+        ("/openai/deployments/premium-model/chat/completions", _CHAT_BODY),+        ("/engines/premium-model/chat/completions", _CHAT_BODY),+        ("/openai/deployments/premium-model/embeddings", _EMBEDDING_BODY),+        ("/v1/chat/completions?model=premium-model", _CHAT_BODY),+        ("/v1/embeddings?model=premium-model", _EMBEDDING_BODY),+        ("/v1/images/generations?model=premium-model", _IMAGE_BODY),+    ],+)+def test_denied_url_model_with_allowed_body_model_is_rejected(cheap_only_key: str, path: str, body: dict) -> None:+    response: Final = _post(path, body, cheap_only_key)++    assert response.status_code == 403, response.text+    assert response.json()["error"]["type"] == "key_model_access_denied", response.text+    assert "served-by-premium-model" not in response.text+++def test_image_edit_with_denied_deployment_model_is_rejected(cheap_only_key: str) -> None:+    response: Final = TestClient(app, raise_server_exceptions=False).post(+        "/openai/deployments/premium-model/images/edits",+        data={"model": "cheap-model", "prompt": "a cat"},+        files={"image": ("cat.png", b"\x89PNG\r\n\x1a\n", "image/png")},+        headers={"Authorization": f"Bearer {cheap_only_key}"},+    )++    assert response.status_code == 403, response.text+    assert response.json()["error"]["type"] == "key_model_access_denied", response.text+++@pytest.mark.parametrize(+    ("path", "body"),+    [+        ("/v1beta/models/cheap-model:generateContent", {**_GOOGLE_BODY, "model": "premium-model"}),+        ("/v1beta/models/cheap-model:generateContent", {"contents": _GOOGLE_BODY["contents"]}),+        ("/openai/deployments/cheap-model/images/generations", {**_IMAGE_BODY, "model": "premium-model"}),+        ("/openai/deployments/cheap-model/chat/completions", {**_CHAT_BODY, "model": "premium-model"}),+        ("/v1/chat/completions?model=cheap-model", {**_CHAT_BODY, "model": "premium-model"}),+        ("/v1/chat/completions", _CHAT_BODY),+    ],+)+def test_allowed_served_model_is_answered(cheap_only_key: str, path: str, body: dict) -> None:+    response: Final = _post(path, body, cheap_only_key)++    assert response.status_code == 200, response.text+    assert "served-by-cheap-model" in response.text+++def test_allowed_url_model_is_streamed(cheap_only_key: str) -> None:+    response: Final = _post(+        "/v1beta/models/cheap-model:streamGenerateContent", {**_GOOGLE_BODY, "model": "premium-model"}, cheap_only_key+    )++    assert response.status_code == 200, response.text+    assert _streamed_text(response) == "served-by-cheap-model", response.text+++def test_denied_body_model_without_url_model_is_still_rejected(cheap_only_key: str) -> None:+    response: Final = _post("/v1/chat/completions", {**_CHAT_BODY, "model": "premium-model"}, cheap_only_key)++    assert response.status_code == 403, response.text+    assert response.json()["error"]["type"] == "key_model_access_denied", response.text+++def _dispatched_request(path: str, query: str, path_params: dict[str, str]) -> Request:+    """A request as auth sees it after FastAPI dispatched it to the real handler for ``path``."""+    route: Final = next(+        r for r in app.routes if isinstance(r, APIRoute) and r.path == path and "POST" in (r.methods or set())+    )+    return Request(+        {+            "type": "http",+            "method": "POST",+            "path": path,+            "headers": [],+            "query_string": query.encode(),+            "route": route,+            "path_params": path_params,+        }+    )+++@pytest.mark.parametrize(+    ("path", "query", "path_params"),+    [+        ("/v1/chat/completions", "model=premium-model", {"model": "cheap-model"}),+        (+            "/v1beta/models/{model_name:path}:generateContent",+            "",+            {"model_name": "premium-model", "model": "cheap-model"},+        ),+    ],+)+def test_planted_path_model_the_handler_does_not_bind_is_ignored(+    path: str, query: str, path_params: dict[str, str]+) -> None:+    """The budget fallback plants ``path_params["model"]`` on every route; the handler here never reads it."""+    request: Final = _dispatched_request(path, query, path_params)++    model: Final = get_model_from_request(+        request_data={"model": "cheap-model"},+        route=path,+        request_query_params=dict(request.query_params),+        request=request,+    )++    assert model == "premium-model" 
Merged fix · PR #51226+320 −6
litellm/proxy/auth/auth_utils.pyindex 7413490e44f..900e993cadb 100644@@ -37,7 +37,11 @@ from litellm.llms.azure.passthrough.transformation import azure_router_model_in_ from litellm.llms.nvidia_nim.passthrough.transformation import nvidia_nim_model_group_in_path from litellm.proxy._types import * from litellm.proxy.auth.route_checks import RouteChecks-from litellm.proxy.common_utils.http_parsing_utils import extract_nested_form_metadata+from litellm.proxy.common_utils.http_parsing_utils import (+    extract_nested_form_metadata,+    read_request_path_params,+    resolve_inference_model,+) from litellm.types.passthrough_endpoints.pass_through_endpoints import (     LITELLM_PASS_THROUGH_ENDPOINT_MARKER, )@@ -2672,6 +2676,7 @@ def get_model_from_request(     llm_router: Router | None = None,     request: Request | None = None,     team_id: str | None = None,+    path_params: Mapping[str, object] | None = None, ) -> str | list[str] | None:     """Resolve the model(s) a request targets, for model-access and budget checks. @@ -2690,6 +2695,12 @@ def get_model_from_request(     if request_dispatched_to_pass_through_endpoint(request):         return None +    handler_request_data: Final = _MODEL_REQUEST_DATA.validate_python(request_data)+    handler_path_params: Final = read_request_path_params(request) if request is not None else path_params+    handler_model: Final = _handler_selected_model(route, handler_request_data, handler_path_params)+    if handler_model is not None:+        return handler_model+     candidates: Final = _extract_model_candidates_from_request(         request_data=request_data,         route=route,@@ -2719,6 +2730,39 @@ def get_model_from_request(     return model  +_HandlerModelKind: TypeAlias = Literal["completion", "image_generation", "image_edit"]+_MODEL_REQUEST_DATA: Final = TypeAdapter(Mapping[str, object])+_HANDLER_MODEL_ROUTES: Final[tuple[tuple[re.Pattern[str], str, _HandlerModelKind], ...]] = (+    (re.compile(r"^(?:/v1beta)?/models/(.+):(?:generateContent|streamGenerateContent)$"), "model_name", "completion"),+    (re.compile(r"^/openai/deployments/(.+)/images/generations$"), "model", "image_generation"),+    (re.compile(r"^/openai/deployments/(.+)/images/edits$"), "model", "image_edit"),+)+++def _handler_selected_model(+    route: str, request_data: Mapping[str, object], path_params: Mapping[str, object] | None+) -> str | None:+    selection: Final[tuple[re.Match[str], str, _HandlerModelKind] | None] = next(+        ((match, name, kind) for pattern, name, kind in _HANDLER_MODEL_ROUTES if (match := pattern.match(route))),+        None,+    )+    if selection is None:+        return None+    from litellm.proxy.proxy_server import general_settings_view, user_model_view++    route_match, path_param_name, kind = selection+    route_model: Final = route_match.group(1)+    path_model: Final = path_params.get(path_param_name) if path_params is not None else None+    model: Final = resolve_inference_model(+        request_data.get("model"),+        general_settings_view(),+        user_model_view(),+        path_model if isinstance(path_model, str) and path_model else route_model,+        kind=kind,+    )+    return model if isinstance(model, str) and model else route_model++ def _model_from_route_path(route: str) -> str | None:     """     Google generateContent-style routes allow "/" inside the model id, e.g.litellm/proxy/auth/user_api_key_auth.pyindex d7a8d4a0700..1b2e4e791e3 100644@@ -153,6 +153,7 @@ from litellm.proxy.common_utils.http_parsing_utils import (  # noqa: F401  # leg     populate_request_with_path_params,     read_raw_json_body,     read_request_body,+    read_request_path_params,     resolve_realtime_route_model,     rewrite_request_model,     safe_get_request_headers,@@ -4252,6 +4253,7 @@ async def _reserve_budget_after_common_checks(             reserve_budget_for_request,         ) +        reservation_path_params: Final = read_request_path_params(request)         user_api_key_auth_obj.budget_reservation = await reserve_budget_for_request(             request_body=request_data,             route=route,@@ -4270,6 +4272,7 @@ async def _reserve_budget_after_common_checks(                 or user_api_key_auth_obj.billing_agent_policy is not None             ),             raw_body=await read_raw_json_body(request=request),+            path_params=reservation_path_params,         )     if request is not None:         reservation: Final = user_api_key_auth_obj.budget_reservationlitellm/proxy/common_utils/http_parsing_utils.pyindex 80ba2f49745..2786b4271e4 100644@@ -37,6 +37,12 @@ _BINARY_CONTENT_TYPES: Final[frozenset[str]] = frozenset({"application/x-protobu _ANNOTATION_QUALIFIERS: Final[frozenset[object]] = frozenset({Annotated, NotRequired, ReadOnly, Required}) _DECODED_JSON: Final = TypeAdapter(object) _JSON_REQUEST_BODY: Final = TypeAdapter(Mapping[str, object])+_REQUEST_PATH_PARAMS: Final = TypeAdapter(Mapping[str, object])+++def read_request_path_params(request: Request | None) -> Mapping[str, object] | None:+    path_params: Final = _DECODED_JSON.validate_python(getattr(request, "path_params", None))+    return _REQUEST_PATH_PARAMS.validate_python(path_params) if isinstance(path_params, Mapping) else None   class _ReadableUpload(Protocol):litellm/proxy/proxy_server.pyindex 23d45e07dfa..6fbe562b5d1 100644@@ -3183,6 +3183,10 @@ def general_settings_view() -> Mapping[str, object]:     return _GENERAL_SETTINGS_VIEW.validate_python(general_settings)  +def user_model_view() -> str | None:+    return user_model if isinstance(user_model, str) else None++ config_passthrough_endpoints: list[dict[str, object]] | None = None log_file: Final = "api_log.json" worker_config: Final = Nonelitellm/proxy/spend_tracking/budget_reservation.pyindex 8f0584bd0d0..096578264e3 100644@@ -262,12 +262,13 @@ async def reserve_budget_for_request(     apply_user_budget_to_team_keys: bool = False,     fail_closed_budget_enforcement: bool = False,     raw_body: bytes | None = None,+    path_params: Mapping[str, object] | None = None, ) -> dict | None:     if valid_token is None or not RouteChecks.is_llm_api_route(route=route):         return None     if RouteChecks.is_unbilled_route(route):         return None-    if get_model_from_request(request_body, route, llm_router=llm_router) is None:+    if get_model_from_request(request_body, route, llm_router=llm_router, path_params=path_params) is None:         return None      counters: Final = await _get_budget_counters(@@ -291,6 +292,7 @@ async def reserve_budget_for_request(         llm_router=llm_router,         team_id=valid_token.team_id,         raw_body=raw_body,+        path_params=path_params,     )      reservation_cost = estimate_request_max_cost(@@ -298,6 +300,7 @@ async def reserve_budget_for_request(         route=route,         llm_router=llm_router,         input_token_counts=input_token_counts,+        path_params=path_params,     )     # estimate_request_max_cost still returns None when the model is unknown     # to the cost map (no token-priced cost fields, e.g. image/audio routes).@@ -334,6 +337,7 @@ async def reserve_budget_for_request(         route=route,         llm_router=llm_router,         input_token_counts=input_token_counts,+        path_params=path_params,     )     budget_reservation: Final = {         "reserved_cost": reservation_cost,@@ -1319,6 +1323,7 @@ def estimate_request_max_cost(     route: str,     llm_router: Router | None,     input_token_counts: Mapping[str, int] | None = None,+    path_params: Mapping[str, object] | None = None, ) -> float | None:     estimates = [         _estimate_request_max_cost_for_model(@@ -1328,7 +1333,9 @@ def estimate_request_max_cost(             llm_router=llm_router,             input_tokens=(input_token_counts or {}).get(model_name),         )-        for model_name in _get_request_models(request_body=request_body, route=route, llm_router=llm_router)+        for model_name in _get_request_models(+            request_body=request_body, route=route, llm_router=llm_router, path_params=path_params+        )     ]     estimates = [estimate for estimate in estimates if estimate is not None]     if not estimates:@@ -1341,6 +1348,7 @@ def estimate_request_input_cost(     route: str,     llm_router: Router | None,     input_token_counts: Mapping[str, int] | None = None,+    path_params: Mapping[str, object] | None = None, ) -> float | None:     """Cost of the request's input tokens alone. @@ -1357,7 +1365,9 @@ def estimate_request_input_cost(             llm_router=llm_router,             input_tokens=(input_token_counts or {}).get(model_name),         )-        for model_name in _get_request_models(request_body=request_body, route=route, llm_router=llm_router)+        for model_name in _get_request_models(+            request_body=request_body, route=route, llm_router=llm_router, path_params=path_params+        )     ]     estimates = [estimate for estimate in estimates if estimate is not None]     if not estimates:@@ -1609,8 +1619,9 @@ def _get_request_models(     request_body: dict,     route: str,     llm_router: Router | None,+    path_params: Mapping[str, object] | None = None, ) -> Sequence[str]:-    model: Final = get_model_from_request(request_body, route, llm_router=llm_router)+    model: Final = get_model_from_request(request_body, route, llm_router=llm_router, path_params=path_params)     if model is None:         return ()     return (model,) if isinstance(model, str) else tuple(model)@@ -1622,11 +1633,14 @@ async def count_request_input_tokens(     llm_router: Router | None,     team_id: str | None,     raw_body: bytes | None = None,+    path_params: Mapping[str, object] | None = None, ) -> Mapping[str, int]:     """Input-token count per candidate model, counted once per request.      The counts are reused by both the max-cost and the input-cost estimate."""-    models: Final = _get_request_models(request_body=request_body, route=route, llm_router=llm_router)+    models: Final = _get_request_models(+        request_body=request_body, route=route, llm_router=llm_router, path_params=path_params+    )     if not models:         return MappingProxyType({})     counted_models: Final = MappingProxyType(tests/unit/proxy/auth/test_auth_utils.pyindex 73a2c534628..58a78aede3c 100644@@ -1379,6 +1379,107 @@ def test_get_model_from_request_supports_google_model_names_with_slashes():     )  +_URL_SELECTED_MODEL_ROUTES: Final = (+    ("/v1beta/models/premium-model:generateContent", "premium-model"),+    ("/models/premium-model:generateContent", "premium-model"),+    ("/v1beta/models/premium-model:streamGenerateContent", "premium-model"),+    ("/models/provider/premium-model:7b:streamGenerateContent", "provider/premium-model:7b"),+    ("/openai/deployments/premium-model/images/generations", "premium-model"),+    ("/openai/deployments/provider/premium-model/images/edits", "provider/premium-model"),+)+++@pytest.fixture+def no_inference_model_override(monkeypatch: pytest.MonkeyPatch) -> None:+    from litellm.proxy import proxy_server++    monkeypatch.setattr(proxy_server, "general_settings", {})+    monkeypatch.setattr(proxy_server, "user_model", None)+++@pytest.mark.usefixtures("no_inference_model_override")+@pytest.mark.parametrize("route, url_model", _URL_SELECTED_MODEL_ROUTES)+@pytest.mark.parametrize(+    "request_data",+    [+        {"model": "cheap-model"},+        {"target_model_names": ["cheap-model"]},+        {"model": "cheap-model", "target_model_names": "cheap-model"},+    ],+)+def test_get_model_from_request_checks_the_url_model_on_routes_served_from_the_url(+    route: str, url_model: str, request_data: dict[str, object]+) -> None:+    assert get_model_from_request(request_data=request_data, route=route) == url_model+++@pytest.mark.usefixtures("no_inference_model_override")+@pytest.mark.parametrize(+    "route, path_params",+    [+        ("/v1beta/models/premium-model:generateContent", {"model_name": "cheap-model"}),+        ("/models/premium-model:streamGenerateContent", {"model_name": "cheap-model"}),+        ("/openai/deployments/premium-model/images/generations", {"model": "cheap-model"}),+        ("/openai/deployments/premium-model/images/edits", {"model": "cheap-model"}),+    ],+)+def test_get_model_from_request_follows_the_handlers_path_param_over_route_and_body(+    route: str, path_params: dict[str, str]+) -> None:+    request: Final = Request(scope={"type": "http", "path": route, "path_params": path_params, "headers": []})+    body: Final = {"model": "body-model"}++    assert get_model_from_request(request_data=body, route=route, request=request) == "cheap-model"+    assert get_model_from_request(request_data=body, route=route, path_params=path_params) == "cheap-model"+    assert get_model_from_request(request_data=body, route=route, path_params={"other": "x"}) == "premium-model"+++@pytest.mark.parametrize(+    "route, settings, cli_model, expected",+    [+        ("/v1beta/models/url-model:generateContent", {"completion_model": "configured"}, None, "configured"),+        ("/models/url-model:streamGenerateContent", {}, "cli", "cli"),+        ("/v1beta/models/url-model:generateContent", {"image_generation_model": "configured"}, None, "url-model"),+        ("/openai/deployments/url-model/images/generations", {}, "cli", "cli"),+        ("/openai/deployments/url-model/images/generations", {"completion_model": "configured"}, None, "url-model"),+        (+            "/openai/deployments/url-model/images/generations",+            {"image_generation_model": "configured"},+            None,+            "url-model",+        ),+        ("/openai/deployments/url-model/images/edits", {"completion_model": "configured"}, "cli", "configured"),+        ("/openai/deployments/url-model/images/edits", {}, "cli", "cli"),+        ("/openai/deployments/url-model/images/edits", {"image_generation_model": "configured"}, None, "url-model"),+    ],+)+def test_get_model_from_request_applies_the_handlers_server_model_precedence(+    monkeypatch: pytest.MonkeyPatch,+    route: str,+    settings: dict[str, str],+    cli_model: str | None,+    expected: str,+) -> None:+    from litellm.proxy import proxy_server++    monkeypatch.setattr(proxy_server, "general_settings", settings)+    monkeypatch.setattr(proxy_server, "user_model", cli_model)++    assert get_model_from_request(request_data={"model": "body-model"}, route=route) == expected+++@pytest.mark.usefixtures("no_inference_model_override")+@pytest.mark.parametrize("route", ["/v1/images/generations", "/images/edits", "/v1/chat/completions"])+def test_get_model_from_request_keeps_the_body_model_where_the_url_names_none(route: str) -> None:+    assert get_model_from_request(request_data={"model": "cheap-model"}, route=route) == "cheap-model"+++@pytest.mark.parametrize("route, url_model", _URL_SELECTED_MODEL_ROUTES)+def test_get_model_from_request_skips_url_model_on_configured_pass_through(route: str, url_model: str) -> None:+    request: Final = _request_dispatched_to(_pass_through_endpoint())+    assert get_model_from_request(request_data={"model": url_model}, route=route, request=request) is None++ def test_get_model_from_request_vertex_passthrough_still_works():     route = "/vertex_ai/v1/projects/p/locations/l/publishers/google/models/gemini-1.5-pro:generateContent"     assert get_model_from_request(request_data={}, route=route) == "gemini-1.5-pro"tests/unit/proxy/auth/test_user_api_key_auth.pyindex ce4a844550b..7746fc96673 100644@@ -1293,6 +1293,54 @@ def test_get_model_from_request(route, request_data, expected_model):     assert get_model_from_request(request_data, route) == expected_model  +@pytest.mark.asyncio+@pytest.mark.parametrize(+    "route_template",+    [+        "/v1beta/models/{model}:generateContent",+        "/models/{model}:streamGenerateContent",+        "/openai/deployments/{model}/images/generations",+        "/openai/deployments/{model}/images/edits",+    ],+)+@pytest.mark.parametrize("body", [{"model": "cheap-model"}, {"target_model_names": ["cheap-model"]}])+async def test_key_model_allowlist_applies_to_the_model_selected_by_the_url(+    monkeypatch: pytest.MonkeyPatch, route_template: str, body: dict[str, object]+) -> None:+    from litellm.proxy._types import ProxyErrorTypes+    from litellm.proxy.auth.user_api_key_auth import enforce_key_and_fallback_model_access++    monkeypatch.setattr(litellm.proxy.proxy_server, "llm_router", None)+    monkeypatch.setattr(litellm.proxy.proxy_server, "general_settings", {})+    monkeypatch.setattr(litellm.proxy.proxy_server, "user_model", None)+    router: Final = litellm.Router(+        model_list=[+            {"model_name": name, "litellm_params": {"model": f"openai/{name}", "api_key": "sk-fake"}}+            for name in ("cheap-model", "premium-model")+        ]+    )+    token: Final = UserAPIKeyAuth(models=["cheap-model"])++    async def authorize(url_model: str) -> None:+        route: Final = route_template.format(model=url_model)+        request: Final = StarletteRequest(+            {"type": "http", "method": "POST", "path": route, "headers": [], "query_string": b""}+        )+        await enforce_key_and_fallback_model_access(+            valid_token=token,+            request_data=dict(body),+            route=route,+            request=request,+            llm_model_list=list(router.model_list),+            llm_router=router,+        )++    await authorize("cheap-model")+    with pytest.raises(ProxyException) as exc_info:+        await authorize("premium-model")+    assert exc_info.value.type == ProxyErrorTypes.key_model_access_denied++ @pytest.mark.asyncio async def test_jwt_non_admin_team_route_access(monkeypatch):     """tests/unit/proxy/auth/test_user_api_key_auth_request_flow.pyindex 0d2433d9707..5824b5fccab 100644@@ -10952,6 +10952,42 @@ class TestCheckTeamMemberModelBudgetWithFallback:          assert request_data["model"] == "gpt-4o-mini" +    @pytest.mark.asyncio+    @pytest.mark.parametrize("operation", ["generations", "edits"])+    async def test_image_deployment_key_fallback_is_the_model_later_checks_and_reservation_see(+        self, monkeypatch: pytest.MonkeyPatch, operation: str+    ) -> None:+        from litellm.proxy import proxy_server+        from litellm.proxy.auth.auth_utils import get_model_from_request++        monkeypatch.setattr(proxy_server, "general_settings", {})+        monkeypatch.setattr(proxy_server, "user_model", None)+        route: Final = f"/openai/deployments/gpt-4o/images/{operation}"+        valid_token: Final = UserAPIKeyAuth(+            token="test-key",+            models=["gpt-4o", "gpt-4o-mini"],+            budget_fallbacks={"gpt-4o": ["gpt-4o-mini"]},+            model_max_budget={+                "gpt-4o": {"max_budget": 5.0, "budget_duration": "1d"},+                "gpt-4o-mini": {"max_budget": 100.0, "budget_duration": "1d"},+            },+        )+        limiter, _ = await _real_model_budget_limiter(key_spend={"gpt-4o": 10.0})+        request_data: Final[dict[str, object]] = {"model": "gpt-4o"}+        request: Final = self._request("gpt-4o")+        assert get_model_from_request(request_data, route, request=request) == "gpt-4o"++        await _check_key_model_budget_with_fallback(+            valid_token=valid_token,+            model_max_budget_limiter=limiter,+            model_name="gpt-4o",+            request_data=request_data,+            request=request,+        )++        assert get_model_from_request(request_data, route, request=request) == "gpt-4o-mini"+        assert get_model_from_request(request_data, route, path_params=request.path_params) == "gpt-4o-mini"+     @pytest.mark.asyncio     async def test_fallback_outside_member_allowed_models_is_rejected(self):         valid_token: Final = UserAPIKeyAuth(@@ -13440,6 +13476,10 @@ async def test_managed_actor_cannot_access_provider_resource_routes(monkeypatch,         ("/v1/chat/completions", {}, "forbidden-model"),         ("/v1/images/generations", {"image_generation_model": "forbidden-model"}, None),         ("/v1/images/edits", {"image_generation_model": "forbidden-model"}, None),+        ("/v1beta/models/test-model:generateContent", {"completion_model": "forbidden-model"}, None),+        ("/models/test-model:streamGenerateContent", {}, "forbidden-model"),+        ("/openai/deployments/test-model/images/generations", {}, "forbidden-model"),+        ("/openai/deployments/test-model/images/edits", {"completion_model": "forbidden-model"}, None),     ], ) async def test_managed_agent_cannot_bypass_grants_with_server_default(tests/unit/proxy/common_utils/test_http_parsing_utils.pyindex 8423dee9886..b8a686d3f67 100644@@ -31,6 +31,7 @@ from litellm.proxy.common_utils.http_parsing_utils import (     numeric_form_fields,     populate_request_with_path_params,     read_raw_json_body,+    read_request_path_params, ) from fastapi import Request as Request_http_parsing from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER@@ -1786,3 +1787,22 @@ async def test_convert_upload_files_to_file_data_reads_every_upload_into_file_tu         "tags": ["red", "blue"],         "display_title": "My skill",     }+++@pytest.mark.parametrize(+    "scope_path_params, expected",+    [+        ({"model": "deployment/name"}, {"model": "deployment/name"}),+        ({}, {}),+        (None, None),+    ],+)+def test_read_request_path_params_returns_mappings_and_drops_non_mapping_metadata(+    scope_path_params: Mapping[str, object] | None, expected: Mapping[str, object] | None+) -> None:+    request: Final = Request({"type": "http", "path_params": scope_path_params})+    assert read_request_path_params(request) == expected+++def test_read_request_path_params_without_request_is_none() -> None:+    assert read_request_path_params(None) is Nonetests/unit/proxy/spend_tracking/test_budget_reservation.pyindex 72927fa0197..087434c981f 100644@@ -223,6 +223,40 @@ def test_deployment_pricing_update_invalidates_cached_estimate() -> None:     assert math.isclose(after, before * 1000)  +def _priced_group(model_name: str, cost_per_token: float) -> dict[str, object]:+    return Deployment(+        model_name=model_name,+        litellm_params=LiteLLM_Params(model="dashscope/qwen3-max", api_key="sk-fake"),+        model_info=ModelInfo(+            id=model_name,+            max_output_tokens=1000,+            tiered_pricing=[+                {"input_cost_per_token": cost_per_token, "output_cost_per_token": cost_per_token, "range": [0, 128000]}+            ],+        ),+    ).model_dump()+++def test_google_route_reserves_on_the_model_its_path_param_selects(monkeypatch: pytest.MonkeyPatch) -> None:+    monkeypatch.setattr(proxy_server, "general_settings", {})+    monkeypatch.setattr(proxy_server, "user_model", None)+    router: Final = Router(model_list=[_priced_group("cheap-group", 1e-06), _priced_group("premium-group", 1e-03)])+    route: Final = "/v1beta/models/premium-group:generateContent"+    body: Final = {"model": "cheap-group", "messages": [{"role": "user", "content": "hello"}], "max_tokens": 10}++    url_model_cost: Final = estimate_request_max_cost(request_body=body, route=route, llm_router=router)+    path_param_cost: Final = estimate_request_max_cost(+        request_body=body, route=route, llm_router=router, path_params={"model_name": "cheap-group"}+    )+    cheap_cost: Final = estimate_request_max_cost(+        request_body=body, route="/v1beta/models/cheap-group:generateContent", llm_router=router+    )++    assert url_model_cost is not None and cheap_cost is not None+    assert path_param_cost == cheap_cost+    assert math.isclose(url_model_cost, cheap_cost * 1000)++ @pytest.mark.asyncio @pytest.mark.parametrize(     "expiry_offset, expected_max_budget",