From 74ccdd8540c7f9f5216e5d349684cf41a02a283b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:18:52 -0700 Subject: [PATCH] perf(rust-bridge): run the malformed user text rule only when a text block is not a string The native messages projection called the Python rule on every request, so a well-formed 501-turn request paid about 2 ms for a scan that found nothing. Rust now checks the converted messages first, user text and tool_result text included, and calls the rule only when some text value is not a string. --- .../python-bridge/src/routes/messages/host.rs | 23 +++++++++++++++++-- .../messages/test_requests.py | 12 ++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/litellm-rust/crates/python-bridge/src/routes/messages/host.rs b/litellm-rust/crates/python-bridge/src/routes/messages/host.rs index befd56a56f7..37247b13974 100644 --- a/litellm-rust/crates/python-bridge/src/routes/messages/host.rs +++ b/litellm-rust/crates/python-bridge/src/routes/messages/host.rs @@ -69,6 +69,21 @@ fn project_litellm_params<'py>( Ok(litellm_params(fields.into_iter().chain(folded).collect())) } +fn items(value: &Value) -> impl Iterator { + value.as_array().into_iter().flatten() +} + +fn non_string_text(block: &Value) -> bool { + block.get("type").and_then(Value::as_str) == Some("text") + && block.get("text").is_some_and(|text| !text.is_string()) +} + +fn has_non_string_text(messages: &Value) -> bool { + items(messages) + .flat_map(|message| items(&message["content"])) + .any(|block| non_string_text(block) || items(&block["content"]).any(non_string_text)) +} + /// The Python side of the Messages route: projects the prepared arguments and builds the /// public response, chunks and exceptions. pub(super) struct MessagesPythonHost { @@ -94,8 +109,12 @@ impl MessagesPythonHost { argument(name)?.map(|value| value.extract()).transpose() }; let model = string("model")?.ok_or(ArgumentError::Required("model"))?; - argument("messages")?.ok_or(ArgumentError::Required("messages"))?; - let messages = self.messages(py, arguments)?; + let sent = from_py(&argument("messages")?.ok_or(ArgumentError::Required("messages"))?)?; + let messages = if has_non_string_text(&sent) { + self.messages(py, arguments)? + } else { + sent + }; let fields = self.view.body_params(py, arguments, ROUTE_HOST_MODULE)?; let body = [ ("model".to_string(), Value::String(model.clone())), diff --git a/tests/test_litellm_rust/messages/test_requests.py b/tests/test_litellm_rust/messages/test_requests.py index 7be0d11757c..0a2abe497b7 100644 --- a/tests/test_litellm_rust/messages/test_requests.py +++ b/tests/test_litellm_rust/messages/test_requests.py @@ -187,6 +187,18 @@ async def test_drop_params_drops_non_string_user_text(server: RecordingServer, a assert body["messages"] == [{"role": "user", "content": [{"type": "text", "text": "hi"}]}] +@pytest.mark.asyncio +@BOTH_MODES +async def test_non_string_tool_result_text_answers_400_before_io(server: RecordingServer, asynchronous: bool) -> None: + server.expected_requests = 0 + tool_result: Final = {"type": "tool_result", "tool_use_id": "toolu_1", "content": [{"type": "text", "text": None}]} + + with pytest.raises(litellm.BadRequestError, match=r"tool_result toolu_1 content\.0\.text: Input should be a valid string"): + await call(server, asynchronous=asynchronous, messages=[{"role": "user", "content": [tool_result]}]) + + assert server.requests == [] + + @pytest.mark.asyncio @BOTH_MODES async def test_messages_emptied_by_drop_params_answer_400_before_io(server: RecordingServer, asynchronous: bool) -> None: