Repository navigation
fix(messages): answer 400 for a non-string user or system text block and keep count_tokens in step with inference (lts/1.108.x) - #54227
Conversation
…nd count_tokens unless drop_params (cherry picked from commit f34fe32) Folds the follow-up commits of the same change, adapted to lts/1.108.x: ebf299d (400 when dropping leaves no message), 10de3e1 (keep empty text in place so the 400 names the client index), 8f458da (answer the 400 locally on every provider, inference and count_tokens), 913580c (tool_result text keeps its existing path). LTS adaptations: the user text rule lives in litellm/llms/anthropic/pass_through/convertible_blocks.py and runs in prepare_native_messages, which on this line runs before every backend (native, Responses bridge, chat bridge, websearch), so main's adapter, responses adapter, websearch and provider count edits are not needed here. count_request_tokens applies the same rule before any provider count, taking drop_params from the request, then the router defaults, then the deployment, as inference does. The tool_result non-string text rule is not on this line, so tool_result handling is unchanged.
With Rust opted in, a null or non-string user text block went to the Rust handler, which strips it and answers 200. Python answers 400 for it unless drop_params drops it, so route those requests to Python. (cherry picked from commit a24a385) LTS adaptation: this line has no route feature catalog or detectors module, so the rule is a bypass in litellm/messages/dispatch.py _python_serves. Because every such request now stays on Python, the native Rust and rust_bridge host follow-ups of the same change are folded here and not carried: eb16928, db6801b, 74ccdd8, 0b4cc98, 6012bc2.
…e a non-string tool_result text A non-string text inside a tool_result skipped the empty-block strip for the whole request on Python-path providers, and turned off the web search short-circuit. Both now run as on main, and only a top-level user text skips the short-circuit, since the route rejects or drops it (cherry picked from commit 5f60fd8) LTS adaptation: the source half is already this line's behavior, since native_messages_to_send here never had the unstripped branch and the web search handler here never gained the guard, so only the three tests are ported, onto the pass_through paths.
The emptied-messages 400 now fires only when the client sent a non-string user text and nothing is left after the cleanup, so a blank-only body reaches Gemini as its blank user turn and count_tokens counts the client's body (cherry picked from commit c53d947) LTS adaptation: the source's checks live in pass_through/convertible_blocks.py here and read non_string_user_text_error. The non-string system text and Rust handler halves have no code on this line (no billing-header filter, no litellm/rust), so only the count_tokens, router and Gemini adapter tests are ported.
…es_non_string_text_400_lts
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…der drop_params (lts/1.108.x)
…es_non_string_text_400_lts
[Medium impact] The PR appears safe to merge, with the previous test coverage issue fixed and no new findings
|
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 1aaabcc. Configure here.
Handwritten 1-3 sentence description
Backport of #54165 to lts/1.108.x. A null or numeric text block in a user turn or the system prompt used to be dropped silently, sent to the provider, or crash with a 500, and count_tokens disagreed with inference. Now both routes answer a 400 naming the field, or drop the block when drop params is on
Details
TLDR
Problem this solves:
/v1/messagessilently drops null or numeric user text and answers 200How it solves it:
Intentional product change: a non-string user or system text block now answers 400 instead of being dropped unless drop_params is on, as Anthropic's API does. count_tokens now counts the messages inference sends, so it answers 200 for empty text blocks it refused
Every commit of #54165 but the Rust-test-only
8d5be2b938emaps to a commit here.Pick mapping
f34fe32a7c9f1dd70600d6ebf299d4ef4f1dd70600d610de3e17325f1dd70600d68f458da80a4f1dd70600d6913580c35f9f1dd70600d6a24a385e78e95112076e8deb16928919c95112076e8ddb6801bc74f95112076e8d74ccdd8540c95112076e8d0b4cc980a9d95112076e8d6012bc29fcf95112076e8d5f60fd84d0d63938d8b6f4c53d94721109df5d10025949bf95c0420821deb88a7af5084e46ac46c353fd21c18d5be2b938eonly moves that Rust test to#[rstest], so the pair block records it as main onlyUser Flow
Before: a user text block with
"text": nullis silently dropped on inference, while count_tokens refuses the same bodyanthropic/claude-haiku-5-5deployment with blocks{"type":"text","text":"Say hi"}and{"type":"text","text":null}"text": 123messages.0.content.1.text.text: Input should be a valid stringmessages: at least one message is required, the wrong cause"text": nullanswers 500 on MiniMax and DeepSeek and 200 on OpenAI and GeminiAfter: both routes refuse the bad block the same way, and drop_params drops it on both
messages.0.content.1.text: Input should be a valid stringon every providerdrop_params: trueon the deployment or in the request, both routes drop the block and answer 200messages: no message has content left after dropping empty or non-string text blockssystem.1.text: Input should be a valid stringon every provider, and drop_params drops itPre-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 moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Decisions
Choices made and the alternatives
On this line
LITELLM_RUST=truesends/v1/messagesto Rust, whose handler strips a null text as blank. The dispatcher keeps a body with a non-string user or system text on Python, which answers the 400 or drops the block, so only those malformed bodies leave Rust. The alternative was porting #54165's Rust change, which builds on a shaping option this line's Rust handler lacks. The standalone Rust gateway crate keeps stripping, since no workflow builds it and the shipped gateway image runs the Python proxyWithout drop_params, every provider answers a local 400 before any provider call, with the field path the OpenAI and Gemini adapters already used.
A malformed text inside a tool_result keeps the line's behavior, so rewriting and masking guardrails keep working on it. The rule only covers user turns and the system prompt, so a null text in a replayed assistant turn is still stripped and answers 200
Empty and whitespace-only text blocks keep being stripped on both routes, since Claude Code sends them. count_tokens on the Anthropic route now counts the messages inference sends, so it strips them too unless all are blank, rewrites tool_use ids Anthropic would refuse and flattens unencrypted web search results. The alternative was stripping only empty blocks, which kept count_tokens refusing bodies that inference sends fine
The 400 counts blocks in the client's own body, before empty text blocks are stripped, so the index points at the block the client sent. When drop_params drops a non-string user text and nothing is left, both routes answer a local 400 that says so
A non-string system text gets the same rule, with the message Anthropic's own API gives for that body (
direct-anthropic-system-null.txtin the gist). With drop_params the block is dropped, andsystemis left out when no block is left. When the system prompt and a user turn are both bad, the 400 names the user field, as Anthropic's API does (direct-anthropic-both-bad.txt)A user text block with no
textkey is still stripped, as this line already did and #54165 does, since it reads as empty text. Anthropic's API answers 400 for it. A system block with notextis sent as is, so the provider answers its own 400A null text in a
role: "system"entry insidemessagesis still dropped, at the start or mid-conversation, as on this line's base and on main after #54165, while Anthropic's API answers 400 (lts-system-role-null.txtin the gist). This PR only ports #54165, so that fix goes to main first and then gets picked hereThe check adds passes over the messages, about 2 ms more prep CPU at 500 turns and 8 ms at 2,000, the same passes #54165 added on main. With
LITELLM_RUST=truethe dispatcher's own scan adds 1.3 ms at 500 turns and up to 8 ms at 2,000. Folding them into one pass is a follow-up for both linesScreenshots / Proof of Fix
Both legs ran 28 curl cases and one Claude Code task on real provider APIs, plus 14 with
LITELLM_RUST=true, 16 on Bedrock and Vertex AI Claude, and 11 for numeric text and MiniMax and DeepSeek user text.1aaabcce03fonly changes a test, so this proof carries to itThe 14 cases with
LITELLM_RUST=trueFull output is in before and after, from cases-lts-rust-on.sh run as
bash cases-lts-rust-on.sh <port>against the config above. Every model is alitellm-qa-null-text-*deployment, and "Rust" means the answer carriedx-litellm-rust: true.text.text.textdrop_params: truedrop_params: falsestream: truestream: true, drop deploymentclaude_platform, and Gemini count_tokens
Full output is in before and after, from #54165's cases-claude-platform-gemini.sh and config. The rig has no AWS key for the one subscribed claude_platform account
Bedrock and Vertex AI Claude
Both providers answered every case alike. Full output is in before and after, from cases-lts-bedrock-vertex.sh and config
messages.0.content.1.text.text.textmessages.0.content.1.textmessages.0.content.0.textsystem.1.textNumeric text, and MiniMax and DeepSeek user text
Full output is in before and after, from cases-lts-numeric-minimax-deepseek.sh on the main cases' config
"text": 123user text, Anthropicmessages.0.content.1.textmessages.0.content.1.text.text.textmessages.0.content.1.text"text": 123system text, Anthropicsystem.1.textmessages.0.content.1.textmessages.0.content.1.textSetup to rerun every case
Each leg ran one proxy with 2 uvicorn workers and no database, booted from its own tree with the command below. Before ran at the line's tip
e552bffcaf7and After at6c353fd21c1Env var names:
ANTHROPIC_API_KEY,OPENAI_API_KEY,GEMINI_API_KEY,MINIMAX_API_KEY,DEEPSEEK_API_KEYThe config is config-lts.yaml, and each case comes from cases-lts.sh or system-lts.sh, run as
bash <script> <port>, which print each curl they runBefore (e552bff)
litellm-qa-null-text-anthropic null text beside a prompt, messages
The other 27 cases
litellm-qa-null-text-minimax null system text, messages
litellm-qa-null-text-anthropic null text beside a prompt, count_tokens
litellm-qa-null-text-openai null text beside a prompt, messages
litellm-qa-null-text-gemini null text beside a prompt, messages
litellm-qa-null-text-anthropic-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-openai-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-gemini-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-anthropic-drop only a null text with drop_params, messages
litellm-qa-null-text-gemini blank-only body, messages
litellm-qa-null-text-anthropic blank-only body, count_tokens
litellm-qa-null-text-anthropic null system text, messages
litellm-qa-null-text-openai null system text, messages
litellm-qa-null-text-gemini null system text, messages
litellm-qa-null-text-deepseek null system text, messages
litellm-qa-null-text-anthropic null system text, count_tokens
litellm-qa-null-text-openai null system text, count_tokens
litellm-qa-null-text-minimax null system text, count_tokens
litellm-qa-null-text-deepseek null system text, count_tokens
litellm-qa-null-text-anthropic-drop null system text with drop_params, messages
litellm-qa-null-text-openai-drop null system text with drop_params, messages
litellm-qa-null-text-gemini-drop null system text with drop_params, messages
litellm-qa-null-text-anthropic-drop null system text with drop_params, count_tokens
litellm-qa-null-text-anthropic-drop only a null system text with drop_params, messages
litellm-qa-null-text-anthropic null system text and null user text, messages
litellm-qa-null-text-anthropic null system text and null user text, count_tokens
litellm-qa-null-text-openai null system text and null user text, messages
litellm-qa-null-text-openai null system text and null user text, count_tokens
Claude Code
Claude Code pane
After (6c353fd)
litellm-qa-null-text-anthropic null text beside a prompt, messages
The other 27 cases
litellm-qa-null-text-minimax null system text, messages
litellm-qa-null-text-anthropic null text beside a prompt, count_tokens
litellm-qa-null-text-openai null text beside a prompt, messages
litellm-qa-null-text-gemini null text beside a prompt, messages
litellm-qa-null-text-anthropic-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-openai-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-gemini-drop null text beside a prompt with drop_params, messages
litellm-qa-null-text-anthropic-drop only a null text with drop_params, messages
litellm-qa-null-text-gemini blank-only body, messages
litellm-qa-null-text-anthropic blank-only body, count_tokens
litellm-qa-null-text-anthropic null system text, messages
litellm-qa-null-text-openai null system text, messages
litellm-qa-null-text-gemini null system text, messages
litellm-qa-null-text-deepseek null system text, messages
litellm-qa-null-text-anthropic null system text, count_tokens
litellm-qa-null-text-openai null system text, count_tokens
litellm-qa-null-text-minimax null system text, count_tokens
litellm-qa-null-text-deepseek null system text, count_tokens
litellm-qa-null-text-anthropic-drop null system text with drop_params, messages
litellm-qa-null-text-openai-drop null system text with drop_params, messages
litellm-qa-null-text-gemini-drop null system text with drop_params, messages
litellm-qa-null-text-anthropic-drop null system text with drop_params, count_tokens
litellm-qa-null-text-anthropic-drop only a null system text with drop_params, messages
litellm-qa-null-text-anthropic null system text and null user text, messages
litellm-qa-null-text-anthropic null system text and null user text, count_tokens
litellm-qa-null-text-openai null system text and null user text, messages
litellm-qa-null-text-openai null system text and null user text, count_tokens
Claude Code
Claude Code pane
Type
🐛 Bug Fix
Caveats (if any)
Medium
LITELLM_RUST=truetextkey is still dropped and answers 200, where Anthropic's API answers 400role: "system"message is still dropped with a 200, as on main, where Anthropic's API answers 400Low
.text, not Anthropic's.text.textLITELLM_RUST=truea body with a non-string text is served by PythonFinal Attestation
Bugbot summary