Skip to content

fix(rust_bridge): keep Messages, token counter and tokenizer routes on Python - #42517

Merged
yujonglee-berri merged 2 commits into
mainfrom
litellm_messages_route_python_only
Sep 22, 2026
Merged

yujonglee-berri merged 2 commits into
mainfrom
litellm_messages_route_python_only

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • With LITELLM_RUST=1, /v1/messages was dispatched to the Rust path
  • The Rust Messages path is not ready, and drops Anthropic beta inference
  • Claude Code compaction requests failed with context_management: Extra inputs are not permitted

How it solves it:

  • Route.MESSAGES goes back to Rollout.PYTHON_ONLY in the route catalog
  • Route.TOKEN_COUNTER and Route.TOKENIZER also move to PYTHON_ONLY
  • Only OCR and Bedrock transcription can reach Rust now, whatever LITELLM_RUST says
  • The catalog test asserts those three routes resolve to Python for every rust setting

User Flow

Before: a Claude Code user on a proxy running with LITELLM_RUST=1 gets a 400 as soon as the client sends a compaction edit

  1. They send POST https://litellm-domain/v1/messages for claude-opus-4-8 with "context_management": {"edits": [{"type": "compact_20260112"}]}
  2. They get HTTP 400 context_management: Extra inputs are not permitted
  3. Claude Code shows the error and the turn is lost

After: the same request succeeds

  1. They send the same POST https://litellm-domain/v1/messages with the same compaction edit
  2. They get HTTP 200 with a normal msg_... response and Claude Code continues

Relevant issues

Follow-up to #42509, which only improved the error text for this case. The unscoped opt-in was introduced in 1a52bae (#41885)

Affected release

Regression since v1.103.0-rc.1, only when LITELLM_RUST=1 is set. No stable release contains the opt-in rule

Linear ticket

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

Shared setup: proxy started with LITELLM_RUST=true and this config, real Anthropic key

model_list:
  - model_name: claude-opus-4-8
    litellm_params:
      model: anthropic/claude-opus-4-8
      api_key: os.environ/ANTHROPIC_API_KEY
general_settings:
  master_key: sk-1234

Request body compact.json

{"model":"claude-opus-4-8","max_tokens":32,"messages":[{"role":"user","content":"Reply with the single word ok"}],"context_management":{"edits":[{"type":"compact_20260112"}]}}

Before (4bcdaf3)

  1. curl -s -w "\nHTTP %{http_code}\n" localhost:4014/v1/messages -H "Authorization: Bearer sk-1234" -H "content-type: application/json" -d @compact.json
  2. Output:
{"type":"error","error":{"type":"invalid_request_error","message":"litellm.BadRequestError: AnthropicException - (400, '{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"context_management: Extra inputs are not permitted\"},\"request_id\":\"req_011CfJyU1E6wzVdhtbAvWqVv\"}')\n\nLiteLLM: model group 'claude-opus-4-8' failed with the error above. No fallback was attempted."}}
HTTP 400

After (0179256)

  1. curl -s -w "\nHTTP %{http_code}\n" localhost:4013/v1/messages -H "Authorization: Bearer sk-1234" -H "content-type: application/json" -d @compact.json
  2. Output:
{"model":"claude-opus-4-8","id":"msg_011CfJzA4USGLoCvEYiz6ij4","type":"message","role":"assistant","content":[{"type":"text","text":"ok"}],...,"stop_reason":"end_turn",...}
HTTP 200

Headful Claude Code before/after (same proxies, same Rust setting) is in the PR comment below

Unit check: tests/test_litellm/rust_bridge/test_catalog.py passes at the tip (1400 tests) and 378 cases fail when the catalog change is reverted with the updated test in place

Type

🐛 Bug Fix

Caveats (if any)

Low

  • The Rust Messages path still lacks Anthropic beta inference; re-enable opt-in only after that lands
  • Token counter and tokenizer lose their Rust opt-in too, per the Rust owner, until those paths are ready

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/dce6d3c521044e60bd6a40b4284b6016
Open in Devin Desktop: https://app.devin.ai/desktop/session/dce6d3c521044e60bd6a40b4284b6016?variant=devin

…th is ready

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 22, 2026 18:46
@devin-ai-integration

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

@devin-ai-integration devin-ai-integration Bot added the backport-stable P0 regression fix only (Urgent ticket): cherry-pick onto the baking rc line before the stable tag label Sep 22, 2026
@greptile-apps

greptile-apps Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge and keeps Messages on its available Python implementation as intended

Summary

Changes the Messages route from Rust opt-in to Python-only, preventing Rust admission while the native implementation lacks required behavior

  • Keeps /v1/messages on the established Python handler regardless of LITELLM_RUST or process overrides
  • Updates the catalog regression test to require Python routing across all environment, process, provider, and delivery combinations

Reviews (1) · Last reviewed commit: "fix(rust_bridge): keep the Messages rout..."

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@yujonglee-berri
yujonglee-berri enabled auto-merge (squash) September 22, 2026 18:55
@devin-ai-integration devin-ai-integration Bot changed the title fix(rust_bridge): keep the Messages route on Python until the Rust path is ready fix(rust_bridge): keep Messages, token counter and tokenizer routes on Python Sep 22, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

Headful Claude Code with LITELLM_RUST=true: baseline 4bcdaf3 shows the 400, tip 9cd3147 replies normally on the same prompt

Before 4bcdaf3 After 9cd3147
Before After

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_messages_route_python_only (0179256) with main (f3a0bb7)

Open in CodSpeed

@yujonglee-berri
yujonglee-berri merged commit 85ed18e into main Sep 22, 2026
94 of 97 checks passed
@yujonglee-berri
yujonglee-berri deleted the litellm_messages_route_python_only branch September 22, 2026 19:02
@tin-berri tin-berri mentioned this pull request Sep 22, 2026
4 of 6 tasks
yujonglee-berri added a commit that referenced this pull request Sep 22, 2026
…plicitly

#42517 made the Messages, token counter and tokenizer routes Python-only, so
tests/test_litellm_rust silently exercised the Python path or failed outright.
Each suite now prepends a RUST_OPT_IN rule for its route, keeping native
coverage without changing the shipped default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yujonglee-berri added a commit that referenced this pull request Sep 22, 2026
…ive backend (#42530)

* refactor(rust): align the cache crates with Python and activate every backend

The cache port had drifted: lifecycle and Redis-only operations sat on
`BaseCache`, counters were pinned to `f64`, each semantic backend defined its
own embedder and prompt handling, and only the in-memory backend could be
selected natively.

- Split `disconnect` and `test_connection` out of `BaseCache` into optional
  capabilities, implemented only where the Python class defines them, and give
  every Redis-only operation its own capability trait.
- Decouple counters from the stored value type, so one backend can serve both
  responses and counters as Python's `RedisCache` does.
- Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and
  make the Redis and Valkey semantic backends generic over their codec.
- Port the Python operations that were missing: `async_refresh_ttl`,
  `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache
  pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity
  write-back.
- Take the HTTP client from the host pool in the GCS, S3 and Azure backends.
- Activate all nine backends through the Rust catalog, whose rules all stay
  `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native
  runtime when one is selected.
- Give every crate the same layout, move all tests to `tests/` on rstest, and
  add the shared `litellm-cache-testing` contract suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix: freeze native cache request kwargs and batch entries for type discipline

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: declare semantic lookup methods in the native stub

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor(rust): align the cache crates with Python and activate every backend

The cache port had drifted: lifecycle and Redis-only operations sat on
`BaseCache`, counters were pinned to `f64`, each semantic backend defined its
own embedder and prompt handling, and only the in-memory backend could be
selected natively.

- Split `disconnect` and `test_connection` out of `BaseCache` into optional
  capabilities, implemented only where the Python class defines them, and give
  every Redis-only operation its own capability trait.
- Decouple counters from the stored value type, so one backend can serve both
  responses and counters as Python's `RedisCache` does.
- Share one `Embedder` and prompt contract in `litellm_cache::semantic`, and
  make the Redis and Valkey semantic backends generic over their codec.
- Port the Python operations that were missing: `async_refresh_ttl`,
  `async_rpush_and_trim`, `async_set_cache_pipeline_with_ttls`, the DualCache
  pipeline, sadd, bulk delete and TTL reads, and the semantic-similarity
  write-back.
- Take the HTTP client from the host pool in the GCS, S3 and Azure backends.
- Activate all nine backends through the Rust catalog, whose rules all stay
  `PYTHON_ONLY`, and route the `Cache` facade's storage calls to the native
  runtime when one is selected.
- Give every crate the same layout, move all tests to `tests/` on rstest, and
  add the shared `litellm-cache-testing` contract suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix: freeze native cache request kwargs and batch entries for type discipline

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: declare semantic lookup methods in the native stub

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(rust): opt the native Messages and tokenizer suites into Rust explicitly

#42517 made the Messages, token counter and tokenizer routes Python-only, so
tests/test_litellm_rust silently exercised the Python path or failed outright.
Each suite now prepends a RUST_OPT_IN rule for its route, keeping native
coverage without changing the shipped default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(rust): pop one at a time in the Redis 6 lpop pipeline and drop explanatory comments

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
yuneng-berri added a commit that referenced this pull request Sep 26, 2026
* fix: apply configured cache_control_injection_points beside client cache_control marks (#41956)

(cherry picked from commit d8267d5)

* fix(token_counter): count replayed redacted_thinking blocks so prompt_caching keeps pinning (#42069)

(cherry picked from commit 0e7cf51)

* fix(mcp): return camelCase tool keys from /v1/mcp/tools after the SDK 2 upgrade (#42352)

(cherry picked from commit 5eb4e30)

* fix(rust_bridge): keep the Messages route on Python until the Rust path is ready (#42517)

rc/1.103.0 has no token counter or tokenizer routes in the catalog, so only the Messages rule moves to PYTHON_ONLY

(cherry picked from commit 85ed18e)

* fix(proxy): stop leaking periodic tasks on every DB config reload (#42784)

(cherry picked from commit fc87a06)

---------

Co-authored-by: Mateo Wang <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri mateo-berri removed the backport-stable P0 regression fix only (Urgent ticket): cherry-pick onto the baking rc line before the stable tag label Sep 26, 2026
jsvisa added a commit to jsvisa/litellm that referenced this pull request Nov 11, 2026
Token counter and tokenizer were shipped as RUST_OPT_IN in BerriAI#42174 and
moved back to PYTHON_ONLY the same day in BerriAI#42517, whose actual bug was
Messages dropping the Anthropic beta inference header. The tokenizer
rollback was precautionary, bundled with the Messages fix, and never
revisited. The native implementation and its parity tests have been
green since, and the spend-tracking caller already falls back to Python
counting on RuntimeError and ValueError.

RUST_OPT_IN stays default-off, so nothing changes for callers that do
not set LITELLM_RUST.

Signed-off-by: Delweng <delweng@gmail.com>
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