From ae90f1a45891debf622d3cb531163e4fb7f21399 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Mon, 14 Sep 2026 21:28:47 -0700 Subject: [PATCH] fix(guardrails): type the request payload handed to the Anthropic write-back --- .../llms/anthropic/chat/guardrail_translation/handler.py | 7 ++++--- .../test_anthropic_guardrail_handler.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.py index b438d168b52..b8e179e7274 100644 --- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py +++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py @@ -1079,14 +1079,15 @@ class AnthropicMessagesHandler(BaseTranslation): async def _apply_guardrail_responses_to_input( self, - data: dict, - responses: list[str], + data: dict[str, object], # mutable-ok: API message payload + responses: Sequence[str], scanned: tuple[ScannedText, ...], ) -> None: """ Apply guardrail responses back to the top-level system prompt and the input messages. """ - messages: Final[Sequence[_WritableMessage]] = data.get("messages") or () + raw_messages: Final = data.get("messages") + messages: Final[Sequence[_WritableMessage]] = raw_messages if isinstance(raw_messages, list) else () for item, guardrail_response in zip(scanned, responses): match item.target: case SystemStringTarget(): 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.py index 4b47b7d8c4a..51c3751dcf8 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 @@ -2200,7 +2200,7 @@ class TestAnthropicMessagesTopLevelSystemAndToolUseInputs: inputs, the same way the chat completions handler hands over system messages and tool_calls.""" @staticmethod - def _tool_use_conversation(system): + def _tool_use_conversation(system: str) -> dict[str, Any]: return { "model": "claude-sonnet-4-5", "system": system,