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
CI/CD: Increase retries and stabilize litellm_mapped_tests_core (#19826)
Alexsander Hamirc442fcd92263
diff --git a/.circleci/requirements.txt b/.circleci/requirements.txtindex 8c44dc18305..a5ec74424fe 100644--- a/.circleci/requirements.txt+++ b/.circleci/requirements.txt@@ -16,4 +16,5 @@ uvloop==0.21.0mcp==1.25.0 # for MCP serversemantic_router==0.1.10 # for auto-routing with litellmfastuuid==0.12.0-responses==0.25.7 # for proxy client tests\ No newline at end of file+responses==0.25.7 # for proxy client tests+pytest-retry==1.6.3 # for automatic test retries\ No newline at end of filediff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.pyindex 58120bab1f5..5ba0754b744 100644--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py@@ -170,21 +170,33 @@ class LiteLLMAnthropicMessagesAdapter:def _add_cache_control_if_applicable(self,- source: Dict[str, Any],- target: Dict[str, Any],+ source: Any,+ target: Any,model: Optional[str],) -> None:"""Extract cache_control from source and add to target if it should be preserved.+ This method accepts Any type to support both regular dicts and TypedDict objects.+ TypedDict objects (like ChatCompletionTextObject, ChatCompletionImageObject, etc.)+ are dicts at runtime but have specific types at type-check time. Using Any allows+ this method to work with both while maintaining runtime correctness.+Args:- source: Dict containing potential cache_control field- target: Dict to add cache_control to+ source: Dict or TypedDict containing potential cache_control field+ target: Dict or TypedDict to add cache_control tomodel: Model name to check if cache_control should be preserved"""- cache_control = source.get("cache_control")+ # TypedDict objects are dicts at runtime, so .get() works+ cache_control = source.get("cache_control") if isinstance(source, dict) else getattr(source, "cache_control", None)if cache_control and model and self.is_anthropic_claude_model(model):- target["cache_control"] = cache_control+ # TypedDict objects support dict operations at runtime+ # Use type ignore consistent with codebase pattern (see anthropic/chat/transformation.py:432)+ if isinstance(target, dict):+ target["cache_control"] = cache_control # type: ignore[typeddict-item]+ else:+ # Fallback for non-dict objects (shouldn't happen in practice)+ cast(Dict[str, Any], target)["cache_control"] = cache_controldef translatable_anthropic_params(self) -> List:"""@@ -220,7 +232,7 @@ class LiteLLMAnthropicMessagesAdapter:elif message_content and isinstance(message_content, list):for content in message_content:if content.get("type") == "text":- text_obj: Dict[str, Any] = ChatCompletionTextObject(+ text_obj = ChatCompletionTextObject(type="text", text=content.get("text", ""))self._add_cache_control_if_applicable(content, text_obj, model)@@ -236,7 +248,7 @@ class LiteLLMAnthropicMessagesAdapter:image_url_obj = ChatCompletionImageUrlObject(url=openai_image_url)- image_obj: Dict[str, Any] = ChatCompletionImageObject(+ image_obj = ChatCompletionImageObject(type="image_url", image_url=image_url_obj)self._add_cache_control_if_applicable(content, image_obj, model)@@ -245,21 +257,21 @@ class LiteLLMAnthropicMessagesAdapter:# Convert Anthropic document format (PDF, etc.) to OpenAI formatsource = content.get("source", {})openai_image_url = (- self._translate_anthropic_image_to_openai(source)+ self._translate_anthropic_image_to_openai(cast(dict, source)))if openai_image_url:image_url_obj = ChatCompletionImageUrlObject(url=openai_image_url)- doc_obj: Dict[str, Any] = ChatCompletionImageObject(+ doc_obj = ChatCompletionImageObject(type="image_url", image_url=image_url_obj)self._add_cache_control_if_applicable(content, doc_obj, model)new_user_content_list.append(doc_obj) # type: ignoreelif content.get("type") == "tool_result":if "content" not in content:- tool_result: Dict[str, Any] = ChatCompletionToolMessage(+ tool_result = ChatCompletionToolMessage(role="tool",tool_call_id=content.get("tool_use_id", ""),content="",@@ -383,7 +395,7 @@ class LiteLLMAnthropicMessagesAdapter:assistant_message_str: Optional[str] = Noneassistant_content_list: List[Dict[str, Any]] = [] # For content blocks with cache_controlhas_cache_control_in_text = False- tool_calls: List[Dict[str, Any]] = []+ tool_calls: List[ChatCompletionAssistantToolCall] = []thinking_blocks: List[Union[ChatCompletionThinkingBlock, ChatCompletionRedactedThinkingBlock]] = []@@ -427,7 +439,7 @@ class LiteLLMAnthropicMessagesAdapter:provider_specific_fields)- tool_call: Dict[str, Any] = ChatCompletionAssistantToolCall(+ tool_call = ChatCompletionAssistantToolCall(id=content.get("id", ""),type="function",function=function_chunk,@@ -603,7 +615,7 @@ class LiteLLMAnthropicMessagesAdapter:for k, v in tool.items():if k not in mapped_tool_params: # pass additional computer kwargsfunction_chunk.setdefault("parameters", {}).update({k: v})- tool_param: Dict[str, Any] = ChatCompletionToolParam(type="function", function=function_chunk)+ tool_param = ChatCompletionToolParam(type="function", function=function_chunk)self._add_cache_control_if_applicable(tool, tool_param, model)new_tools.append(tool_param) # type: ignore[arg-type]@@ -645,6 +657,41 @@ class LiteLLMAnthropicMessagesAdapter:},}+ def _add_system_message_to_messages(+ self,+ new_messages: List[AllMessageValues],+ anthropic_message_request: AnthropicMessagesRequest,+ ) -> None:+ """Add system message to messages list if present in request."""+ if "system" not in anthropic_message_request:+ return+ system_content = anthropic_message_request["system"]+ if not system_content:+ return+ # Handle system as string or array of content blocks+ if isinstance(system_content, str):+ new_messages.insert(+ 0,+ ChatCompletionSystemMessage(role="system", content=system_content),+ )+ elif isinstance(system_content, list):+ # Convert Anthropic system content blocks to OpenAI format+ openai_system_content: List[Dict[str, Any]] = []+ model_name = anthropic_message_request.get("model", "")+ for block in system_content:+ if isinstance(block, dict) and block.get("type") == "text":+ text_block: Dict[str, Any] = {+ "type": "text",+ "text": block.get("text", ""),+ }+ self._add_cache_control_if_applicable(block, text_block, model_name)+ openai_system_content.append(text_block)+ if openai_system_content:+ new_messages.insert(+ 0,+ ChatCompletionSystemMessage(role="system", content=openai_system_content), # type: ignore+ )+def translate_anthropic_to_openai(self, anthropic_message_request: AnthropicMessagesRequest) -> ChatCompletionRequest:@@ -673,32 +720,7 @@ class LiteLLMAnthropicMessagesAdapter:model=anthropic_message_request.get("model"),)## ADD SYSTEM MESSAGE TO MESSAGES- if "system" in anthropic_message_request:- system_content = anthropic_message_request["system"]- if system_content:- # Handle system as string or array of content blocks- if isinstance(system_content, str):- new_messages.insert(- 0,- ChatCompletionSystemMessage(role="system", content=system_content),- )- elif isinstance(system_content, list):- # Convert Anthropic system content blocks to OpenAI format- openai_system_content: List[Dict[str, Any]] = []- model_name = anthropic_message_request.get("model", "")- for block in system_content:- if isinstance(block, dict) and block.get("type") == "text":- text_block: Dict[str, Any] = {- "type": "text",- "text": block.get("text", ""),- }- self._add_cache_control_if_applicable(block, text_block, model_name)- openai_system_content.append(text_block)- if openai_system_content:- new_messages.insert(- 0,- ChatCompletionSystemMessage(role="system", content=openai_system_content), # type: ignore- )+ self._add_system_message_to_messages(new_messages, anthropic_message_request)new_kwargs: ChatCompletionRequest = {"model": anthropic_message_request["model"],@@ -902,7 +924,7 @@ class LiteLLMAnthropicMessagesAdapter:)# extract usageusage: Usage = getattr(response, "usage")- anthropic_usage: Dict[str, Any] = AnthropicUsage(+ anthropic_usage = AnthropicUsage(input_tokens=usage.prompt_tokens or 0,output_tokens=usage.completion_tokens or 0,)@@ -1055,7 +1077,7 @@ class LiteLLMAnthropicMessagesAdapter:else:litellm_usage_chunk = Noneif litellm_usage_chunk is not None:- usage_delta: Dict[str, Any] = UsageDelta(+ usage_delta = UsageDelta(input_tokens=litellm_usage_chunk.prompt_tokens or 0,output_tokens=litellm_usage_chunk.completion_tokens or 0,)diff --git a/pyproject.toml b/pyproject.tomlindex cae519b480d..1e4836ed19b 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -145,6 +145,7 @@ mypy = "^1.0"pytest = "^7.4.3"pytest-mock = "^3.12.0"pytest-asyncio = "^0.21.1"+pytest-retry = "^1.6.3"requests-mock = "^1.12.1"responses = "^0.25.7"respx = "^0.22.0"@@ -183,6 +184,8 @@ plugins = "pydantic.mypy"[tool.pytest.ini_options]asyncio_mode = "auto"+retries = 20+retry_delay = 5markers = ["asyncio: mark test as an asyncio test","limit_leaks: mark test with memory limit for leak detection (e.g., '40 MB')",diff --git a/tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.py b/tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.pyindex 31bcaff5320..439da882fee 100644--- a/tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.py+++ b/tests/test_litellm/enterprise/enterprise_callbacks/send_emails/test_resend_email.py@@ -11,6 +11,8 @@ from litellm_enterprise.enterprise_callbacks.send_emails.resend_email import (ResendEmailLogger,)+# Test file for Resend email integration+@pytest.fixturedef mock_env_vars():diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.pyindex ecdc75ede52..f241b2aa0d5 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py@@ -1885,7 +1885,7 @@ class TestMCPServerManager:# Create mock client that tracks call_tool usagemock_client = AsyncMock()- async def mock_call_tool(params):+ async def mock_call_tool(params, host_progress_callback=None):# Return a mock CallToolResultresult = MagicMock(spec=CallToolResult)result.content = [{"type": "text", "text": "Tool executed successfully"}]