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
refactor(anthropic): drop bare generics and Any from new guardrail fold helpers
mateo-berri0cbec3f05c36
diff --git a/litellm/llms/anthropic/chat/guardrail_translation/handler.py b/litellm/llms/anthropic/chat/guardrail_translation/handler.pyindex 4bb21ad3e44..59a828da5c3 100644--- a/litellm/llms/anthropic/chat/guardrail_translation/handler.py+++ b/litellm/llms/anthropic/chat/guardrail_translation/handler.py@@ -13,7 +13,7 @@ Pattern Overview:"""import json-from collections.abc import Mapping+from collections.abc import Mapping, Sequencefrom copy import deepcopyfrom dataclasses import dataclassfrom typing import TYPE_CHECKING, Any, Final, cast@@ -498,13 +498,13 @@ class AnthropicMessagesHandler(BaseTranslation):@staticmethoddef _fold_leading_systems_into_top_level(- data: dict, # mutable-ok: API message payload- leading_systems: list, # mutable-ok: API message payload+ data: dict[str, object], # mutable-ok: API message payload+ leading_systems: Sequence[object],include_existing_system: bool,) -> None:"""Deliver leading system rows through Anthropic's top-level system param, which rejects them in messages."""existing: Final = data.get("system") if include_existing_system else None- existing_blocks: Final[list] = ( # mutable-ok: API message payload+ existing_blocks: Final[list[object]] = ( # mutable-ok: API message payload[{"type": "text", "text": existing}]if isinstance(existing, str) and existingelse list(existing)@@ -516,7 +516,7 @@ class AnthropicMessagesHandler(BaseTranslation):for message in leading_systemsif isinstance(message, dict))- folded: Final[list] = existing_blocks + [ # mutable-ok: API message payload+ folded: Final[list[object]] = existing_blocks + [ # mutable-ok: API message payloadblockfor row in converted_rowsif row is not Nonediff --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 26295c54cbe..b219dcba491 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@@ -131,7 +131,7 @@ class MockStructuredMaskingGuardrail(CustomGuardrail):def _mask(text: str) -> str:return text.replace("[email protected]", "<EMAIL>")- def _mask_content(self, content: Any) -> Any:+ def _mask_content(self, content: object) -> object:if isinstance(content, str):return self._mask(content)if not isinstance(content, list):