Skip to content

fix(litellm_model): read the proxy version from a one-token probe whe… - #4869

Merged
cmunley1 merged 3 commits into
mainfrom
rshahbazyan/litellm-model-version-header-fallback
Oct 25, 2026
Merged

cmunley1 merged 3 commits into
mainfrom
rshahbazyan/litellm-model-version-header-fallback

Conversation

@rimashahbazyan

Copy link
Copy Markdown
Contributor

What does this PR do?

Lets litellm_model start behind a LiteLLM proxy whose virtual key cannot call the health route, without weakening the minimum-version guard.

Why. LiteLLMModelServer._check_proxy_version GETs /health/readiness/details and requires litellm_version >= 1.83.0 (the guard added after the March 2026 supply-chain incident). LiteLLM virtual keys scoped to allowed_routes: ['llm_api_routes'] get HTTP 403 on that route ("Virtual key is not allowed to call this route. Only allowed to call routes: ['llm_api_routes']"), so the server raised RuntimeError and refused to start even though the proxy was current. Origin: the review thread on #4727 (#4727 (comment)), where reading the version from the proxy's x-litellm-version response header was agreed as the preferred fix instead of skipping the check.

Why not GET /v1/models. An earlier draft of this change read the header from GET /v1/models. On a real deployment that does not work: /v1/models, /models and /health/liveliness all answer 200 without x-litellm-version; the proxy attaches the x-litellm-* header family to LLM-call responses only (POST /v1/chat/completions, POST /v1/responses). With that draft the server failed closed at startup (fallback GET .../v1/models returned no x-litellm-version header), which is the correct failure mode but the wrong fallback route.

Design. When the health route answers 401 or 403 (and only then), POST a one-token chat completion to <base_url>/v1/chat/completions with the same Authorization / x-litellm-key headers under the same 5 s timeout: {"model": <configured openai_model>, "messages": [{"role": "user", "content": "ping"}], "max_tokens": 1}. The x-litellm-version header is read from the response whatever its HTTP status is: a model-side 4xx (for example a thinking model rejecting max_tokens=1) still carries the proxy header. The value feeds the existing parse, compromised-version and >= 1.83.0 checks. Only a missing header or a transport failure fails closed (RuntimeError, server does not start). Other health failures (5xx, timeouts) still fail immediately without a fallback call. No new config knob.

Trade-off. The probe is a billed one-token LLM call at server start (observed cost on the verified route: x-litellm-response-cost: 4.14e-05). It is logged at WARNING, and an INFO line records the version and that it came from the probe because the health route is forbidden. Operators who object to the probe can instead grant the key the health route, in which case no probe is sent and behaviour is identical to today. /v1/responses also carries the header; chat completions was chosen as the smallest request every route-scoped key can make.

Changes.

  • responses_api_models/litellm_model/app.py: _version_from_probe_header plus the 401/403 branch in _check_proxy_version; module docstring and constant comment explain why /v1/models cannot be used. The health-route success path is unchanged.
  • responses_api_models/litellm_model/README.md: one paragraph describing the check, the probe, why /v1/models is not usable, the billing trade-off and the health-route alternative.
  • responses_api_models/litellm_model/tests/test_app.py: six fallback cases in TestVersionCheck (five new, one guard).

Validation

uv run pytest responses_api_models/litellm_model/tests/test_app.py   # 44 passed (38 existing + 6 fallback)
pre-commit run --files responses_api_models/litellm_model/app.py responses_api_models/litellm_model/README.md responses_api_models/litellm_model/tests/test_app.py   # all passed
git diff --name-only --diff-filter=d --merge-base origin/main -z | xargs -0 detect-secrets-hook --baseline .github/workflows/config/.secrets.baseline   # exit 0

Fallback tests:

  • health 403 + probe header 1.105.1 -> passes; asserts exactly two requests, the second a POST to <base>/v1/chat/completions with identical auth headers, max_tokens: 1 and the configured model
  • health 403 + probe HTTP 400 with header 1.105.1 -> passes (header read regardless of status)
  • health 403 + header 1.82.9 -> RuntimeError naming the minimum safe version 1.83.0
  • health 403 + no header -> RuntimeError naming the missing x-litellm-version header
  • health 403 + probe transport error (OSError) -> RuntimeError ("Could not verify") after two requests
  • health 500 -> RuntimeError after a single request (no fallback attempted)

Verified negatively: with app.py reverted to main, the first five fail; against the earlier /v1/models draft the first two fail; with the fallback widened to every non-OK health status the last one fails. The pre-existing test_safe_version_allows_startup still asserts a single request on the 200 path, and test_health_auth_error_blocks_startup (401 on both routes, no header) still fails closed.

Live verification against a route-scoped virtual key on a LiteLLM proxy (local evidence: hle-diamond-runs/_option2-smoke/REPORT.md): health route 403; GET /v1/models 200 with no x-litellm-version; one-token probe 200 with x-litellm-version: 1.105.1; _check_proxy_version() logs the probe-sourced version and passes; a 5-row HLE-Diamond smoke through litellm_model on a Claude route then ran 5/5 rollouts with no failures, where the same smoke with the /v1/models draft died at startup.

Rollouts

N/A for request/response handling: startup-time version check only. The 5/5 smoke above exercised the full path once the server started.

Compatibility and benchmark impact

None for deployments where the health route works (byte-identical behaviour; no probe is sent). Deployments with route-scoped keys that previously could not start now start, at the cost of one billed one-token call per server start, provided the proxy is >= 1.83.0. No config changes, no migration, no benchmark-result effect.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass).
  • New source files include the required Apache-2.0 SPDX header. (No new source files.)
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

…n the health route is forbidden

LiteLLM virtual keys scoped to `allowed_routes: ['llm_api_routes']` get
HTTP 403 on `/health/readiness/details`, so `_check_proxy_version` raised
and the model server refused to start even against a current proxy.

`GET /v1/models` cannot supply the version: on a real deployment it (and
`/models`, `/health/liveliness`) answers 200 without `x-litellm-version`;
the proxy attaches that header to LLM-call responses only.

When the health route answers 401 or 403 (and only then), POST a one-token
chat completion (`{model: <configured model>, messages: [one short user
message], max_tokens: 1}`) to `<base_url>/v1/chat/completions` with the same
auth headers and timeout, and read `x-litellm-version` from the response
headers whatever the HTTP status is: a model-side 4xx (for example a
thinking model rejecting `max_tokens=1`) still carries the proxy header.
The same parsing, compromised-version and `>= 1.83.0` checks apply; a
missing header or a transport failure still fails closed. A WARNING states
that the probe is a billed one-token call and an INFO line records the
version and that it came from the probe. Operators who object to the probe
can grant the key the health route instead. Behaviour is unchanged when the
health route works.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Rima Shahbazyan <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:model Model servers, inference providers, and model adapters bug Something isn't working labels Oct 21, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the needs-review PR is ready for code review and waiting on a reviewer label Oct 21, 2026
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 22, 2026
@cmunley1

Copy link
Copy Markdown
Contributor

/review

@cmunley1

Copy link
Copy Markdown
Contributor

/ok to test e403d40

@github-actions

Copy link
Copy Markdown
Contributor

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

SHIP

LGTM — no reliability concerns. Reviewed all three changed files and traced the startup fallback through the shared HTTP helper. The 401/403 branch retains the existing version parsing, compromised-version rejection, and minimum-version guard; successful health checks do not send a probe.

Static review only; tests were not executed in this read-only environment.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 23, 2026
@cmunley1

Copy link
Copy Markdown
Contributor

/ok to test 635ef50

@imxj

imxj commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix.

One startup correctness issue to address before merging:
The code and new test assume that a model rejecting max_tokens=1 still returns x-litellm-version. In LiteLLM v1.105.1, the chat-completion exception path omits the version argument, so the error handler drops that header. This means a model-side rejection (for example, from a Claude route with extended thinking enabled) can still prevent Gym from starting even when the proxy version is safe.
Could we use /v1/responses, whose exception path passes the version, and add a regression test reflecting the actual error-response headers?

…l-side errors keep the header

Review finding on #4869: the fallback probe and its test assumed that a
model rejecting the one-token chat completion still answers with
x-litellm-version. In LiteLLM 1.105.x the chat-completion exception path
omits the version argument to the error handler, so the header is dropped
on a model-side 4xx and Gym refuses to start even on a safe proxy. The
Responses API exception path passes the version, so its error responses
keep the header.

Reproduced live on a LiteLLM 1.106.2 proxy with a route-scoped key against
a model that rejects the probe: POST /v1/chat/completions 400 answered with
x-litellm-call-id, x-litellm-response-cost, x-litellm-key-max-budget,
x-litellm-key-spend and x-litellm-timeout only; the same 400 on
POST /v1/responses also carried x-litellm-model-id and
x-litellm-version. The one-token Responses probe returned 200 with the
header on three different model routes.

- _version_from_probe_header now POSTs <base_url>/v1/responses with
  {"model": <configured>, "input": "ping", "max_output_tokens": 1}; same
  auth headers and timeout; the header is still read regardless of HTTP
  status and a missing header or transport failure still fails closed.
- Module docstring, constant comment, WARNING/INFO log text and README
  say why neither /v1/models nor /v1/chat/completions is usable.
- Tests assert the /v1/responses URL and body, and two regression cases
  use the observed error-response header sets: the Responses API set
  (version present) passes, the chat-completions set (no version) fails
  closed with the "no x-litellm-version header" RuntimeError.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Rima Shahbazyan <[email protected]>
@imxj

imxj commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

LGTM. Thanks for switching the probe to /v1/responses

@cmunley1

Copy link
Copy Markdown
Contributor

/ok to test cdcdb5c

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/40155012653

Merged via the queue into main with commit 7b63267 Oct 25, 2026
66 of 69 checks passed
@cmunley1
cmunley1 deleted the rshahbazyan/litellm-model-version-header-fallback branch October 25, 2026 03:04

This branch was previously deployed

1 inactive deployment
public — cdcdb5ce Deployed Oct 25, 2026 by copy-pr-bot[bot] via release / finalize / notify #7123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants