Skip to content

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]

fix(guardrails): type the request payload handed to the Anthropic write-back

mateo-berriae90f1a45891
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,