The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(guardrails): reject per-message texts that cannot land on a string input or a Messages request
mateo-berri37447c98f771
diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.pyindex 9d50345d70d..656e9978eff 100644--- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py+++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py@@ -44,6 +44,7 @@ from litellm.llms.base_llm.guardrail_translation.utils import (scoped_structured_message_indices,stream_item_field,stream_item_fingerprint,+ unappliable_request_rewrite,)from litellm.proxy.pass_through_endpoints.llm_provider_handlers.anthropic_passthrough_logging_handler import (AnthropicPassthroughLoggingHandler,@@ -570,6 +571,8 @@ class AnthropicMessagesHandler(BaseTranslation):preserve_system_messages=has_midturn_system_message,)else:+ if guardrailed_texts and len(guardrailed_texts) != len(scanned):+ raise unappliable_request_rewrite(guardrail_to_apply.guardrail_name)# Step 3: Map guardrail responses back to original message structureawait self._apply_guardrail_responses_to_input(messages=messages,diff --git a/litellm/llms/base_llm/guardrail_translation/utils.py b/litellm/llms/base_llm/guardrail_translation/utils.pyindex 34d648cf184..a80e20c5404 100644--- a/litellm/llms/base_llm/guardrail_translation/utils.py+++ b/litellm/llms/base_llm/guardrail_translation/utils.py@@ -411,3 +411,9 @@ def message_with_slot_texts(message: AllMessageValues, texts: Sequence[str]) ->rewritten_content: Final = texts[0] if isinstance(content, str) else _content_with_slot_texts(content, texts)rewritten: Final = {**message, "content": rewritten_content} # mutable-ok: chat rows stay JSON-plain dictsreturn cast("AllMessageValues", rewritten) # cast-ok: the same row with only its text slots swapped+++def unappliable_request_rewrite(guardrail_name: str | None) -> Exception:+ from litellm.proxy.policy_engine.pipeline_executor import UnappliableRequestRewrite++ return UnappliableRequestRewrite(guardrail_name or "unknown")diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.pyindex 56fda636e9a..ee68f8f6546 100644--- a/litellm/llms/openai/chat/guardrail_translation/handler.py+++ b/litellm/llms/openai/chat/guardrail_translation/handler.py@@ -42,6 +42,7 @@ from litellm.llms.base_llm.guardrail_translation.utils import (stream_item_field,stream_item_fingerprint,stream_item_items,+ unappliable_request_rewrite,)from litellm.main import stream_chunk_builderfrom litellm.types.llms.openai import AllMessageValues, ChatCompletionToolParam@@ -197,9 +198,7 @@ class OpenAIChatCompletionsHandler(BaseTranslation):# Step 3: Map guardrail responses back to original message structureif guardrailed_texts and texts_to_check:if len(guardrailed_texts) != len(text_task_mappings):- from litellm.proxy.policy_engine.pipeline_executor import UnappliableRequestRewrite-- raise UnappliableRequestRewrite(guardrail_to_apply.guardrail_name or "unknown")+ raise unappliable_request_rewrite(guardrail_to_apply.guardrail_name)await self._apply_guardrail_responses_to_input_texts(messages=messages,responses=guardrailed_texts,diff --git a/litellm/llms/openai/responses/guardrail_translation/handler.py b/litellm/llms/openai/responses/guardrail_translation/handler.pyindex 2fe11d9f7bd..27ff55f120c 100644--- a/litellm/llms/openai/responses/guardrail_translation/handler.py+++ b/litellm/llms/openai/responses/guardrail_translation/handler.py@@ -56,6 +56,7 @@ from litellm.llms.base_llm.guardrail_translation.utils import (stream_item_field,stream_item_fingerprint,stream_item_items,+ unappliable_request_rewrite,)from litellm.llms.openai.responses.guardrail_translation.tool_merge import merge_guardrailed_toolsfrom litellm.responses.litellm_completion_transformation.transformation import (@@ -495,13 +496,13 @@ class OpenAIResponsesHandler(BaseTranslation):data["instructions"] = written_back.instructions # rebind-ok: data is an out-paramelif isinstance(input_data, str):guardrailed_texts: Final = guardrailed_inputs.get("texts") or ()+ if len(guardrailed_texts) > 1:+ raise unappliable_request_rewrite(guardrail_to_apply.guardrail_name)data["input"] = guardrailed_texts[0] if guardrailed_texts else input_data # rebind-ok: data is an out-paramelse:rewritten_texts: Final = guardrailed_inputs.get("texts") or ()if len(rewritten_texts) != len(extracted.task_mappings):- from litellm.proxy.policy_engine.pipeline_executor import UnappliableRequestRewrite-- raise UnappliableRequestRewrite(guardrail_to_apply.guardrail_name or "unknown")+ raise unappliable_request_rewrite(guardrail_to_apply.guardrail_name)await self._apply_guardrail_responses_to_input(messages=input_data,responses=rewritten_texts,diff --git a/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py b/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.pyindex 9fe56f4dc65..d6fd30638cc 100644--- a/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py+++ b/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py@@ -2272,6 +2272,58 @@ class TestAnthropicMessagesHandlerStreamingScanKey:assert ended_key != open_key+class PerRowTextGuardrail(CustomGuardrail):+ """Answers one redacted text per chat row it was shown, the way a guardrail+ that scans per message does, and hands back only texts."""++ def __init__(self):+ super().__init__(guardrail_name="per-row-redactor")++ async def apply_guardrail(+ self,+ inputs: GenericGuardrailAPIInputs,+ request_data: dict,+ input_type: Literal["request", "response"],+ logging_obj: Optional[Any] = None,+ ) -> GenericGuardrailAPIInputs:+ rows = inputs.get("structured_messages") or []+ return {**inputs, "texts": [str(row.get("content")).replace("123-45-6789", "<US_SSN>") for row in rows]}+++class TestPerMessageTextWriteBack:+ """Texts that no longer pair one-to-one with what the handler extracted must be+ rejected by name instead of sliding onto the wrong messages."""++ @pytest.mark.asyncio+ async def test_one_text_per_row_over_a_system_prompt_is_rejected_by_name(self):+ from litellm.proxy.policy_engine.pipeline_executor import UnappliableRequestRewrite++ data = {+ "model": "claude-sonnet-4-5",+ "system": "Reply with exactly the SSN you were given.",+ "messages": [{"role": "user", "content": "My SSN is 123-45-6789."}],+ }+ original = json.loads(json.dumps(data))++ with pytest.raises(UnappliableRequestRewrite) as excinfo:+ await AnthropicMessagesHandler().process_input_messages(data=data, guardrail_to_apply=PerRowTextGuardrail())++ assert excinfo.value.guardrail_name == "per-row-redactor"+ assert data["system"] == original["system"], "a rejected rewrite must leave the request untouched"+ assert data["messages"] == original["messages"], "a rejected rewrite must leave the request untouched"++ @pytest.mark.asyncio+ async def test_one_text_per_row_without_a_system_prompt_is_applied(self):+ data = {+ "model": "claude-sonnet-4-5",+ "messages": [{"role": "user", "content": "My SSN is 123-45-6789."}],+ }++ await AnthropicMessagesHandler().process_input_messages(data=data, guardrail_to_apply=PerRowTextGuardrail())++ assert data["messages"] == [{"role": "user", "content": "My SSN is <US_SSN>."}]++class TestAnthropicMessagesHandlerPostCallHookResponse:def test_openai_shaped_stream_assembly_reaches_the_hook_as_a_messages_response(self):from litellm.types.utils import Choices, Message, ModelResponse, Usagediff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.pyindex 394f134e99c..ac719da169c 100644--- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py+++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py@@ -2392,6 +2392,14 @@ def _tool_replay_request() -> dict:}+def _string_input_request() -> dict:+ return {+ "model": "gpt-5.6",+ "instructions": "Never repeat the SSN " + SSN + " back.",+ "input": "My SSN is " + SSN + ".",+ }++class TestPerMessageRewriteWriteBack:"""A guardrail that rewrites per chat row hands the rows back asstructured_messages, and the handler lands them on the instructions and the@@ -2432,6 +2440,33 @@ class TestPerMessageRewriteWriteBack:assert data["input"] == original["input"]assert data["instructions"] == original["instructions"]+ @pytest.mark.asyncio+ async def test_structured_rows_land_on_instructions_and_string_input(self):+ guardrail = _per_message_redactor()+ data = _string_input_request()++ with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(True)):+ result = await OpenAIResponsesHandler().process_input_messages(data, guardrail)++ assert result["instructions"] == "Never repeat the SSN " + REDACTED_SSN + " back."+ assert [_texts(item) for item in result["input"]] == [["My SSN is " + REDACTED_SSN + "."]]++ @pytest.mark.asyncio+ async def test_texts_only_per_message_answer_over_a_string_input_is_rejected_by_name(self):+ from litellm.proxy.policy_engine.pipeline_executor import UnappliableRequestRewrite++ guardrail = _per_message_redactor()+ data = _string_input_request()+ original = copy.deepcopy(data)++ with patch.object(guardrail.async_handler, "post", side_effect=_per_message_guardrail_server(False)):+ with pytest.raises(UnappliableRequestRewrite) as excinfo:+ await OpenAIResponsesHandler().process_input_messages(data, guardrail)++ assert excinfo.value.guardrail_name == "per-message-redactor"+ assert data["input"] == original["input"]+ assert data["instructions"] == original["instructions"]+class TestProvenancePatching:"""The O(n) provenance pass must keep patching rewritten rows in place for the