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]

Merge branch 'main' into litellm_staging_01_06_2026

Sameer Kankutee7efd51bd7bc
diff --cc litellm/types/integrations/prometheus.py
index 6a98c8a6105,fb439a9541b..88dee19ae5a
--- a/litellm/types/integrations/prometheus.py
+++ b/litellm/types/integrations/prometheus.py
@@@ -185,10 -185,10 +185,14 @@@ DEFINED_PROMETHEUS_METRICS = Literal
"litellm_redis_daily_spend_update_queue_size",
"litellm_in_memory_spend_update_queue_size",
"litellm_redis_spend_update_queue_size",
+ "litellm_request_queue_time_seconds",
+ "litellm_guardrail_latency_seconds",
+ "litellm_guardrail_errors_total",
+ "litellm_guardrail_requests_total",
+ # Cache metrics
+ "litellm_cache_hits_metric",
+ "litellm_cache_misses_metric",
+ "litellm_cached_tokens_metric",
]
diff --cc tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
index a4188f03cde,4914ec0bfb7..42a2b5d0971
--- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
+++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py
@@@ -1139,167 -1139,92 +1139,256 @@@ def test_bedrock_create_bedrock_block_d
assert block["document"]["format"] == format_type
+def test_convert_to_anthropic_tool_result_image_with_cache_control():
+ """
+ Test that cache_control is properly applied to image content in tool results.
+ This tests the functionality added in the uncommitted changes where
+ add_cache_control_to_content is called for image_url content types.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_anthropic_tool_result,
+ )
+
+ # Test with base64 image data URI
+ message = {
+ "role": "tool",
+ "tool_call_id": "call_test_123",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the image you requested:",
+ },
+ {
+ "type": "image_url",
+ "image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQ",
+ "cache_control": {"type": "ephemeral"},
+ },
+ ],
+ }
+
+ result = convert_to_anthropic_tool_result(message)
+
+ # Verify the result structure
+ assert result["type"] == "tool_result"
+ assert result["tool_use_id"] == "call_test_123"
+ assert isinstance(result["content"], list)
+ assert len(result["content"]) == 2
+
+ # Verify text content
+ assert result["content"][0]["type"] == "text"
+ assert result["content"][0]["text"] == "Here is the image you requested:"
+
+ # Verify image content with cache_control
+ assert result["content"][1]["type"] == "image"
+ assert result["content"][1]["source"]["type"] == "base64"
+ assert result["content"][1]["source"]["media_type"] == "image/jpeg"
+ assert "cache_control" in result["content"][1]
+ assert result["content"][1]["cache_control"]["type"] == "ephemeral"
+
+
+def test_convert_to_anthropic_tool_result_image_without_cache_control():
+ """
+ Test that images without cache_control in tool results work correctly.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_anthropic_tool_result,
+ )
+
+ message = {
+ "role": "tool",
+ "tool_call_id": "call_test_456",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA",
+ },
+ ],
+ }
+
+ result = convert_to_anthropic_tool_result(message)
+
+ # Verify the result structure
+ assert result["type"] == "tool_result"
+ assert result["tool_use_id"] == "call_test_456"
+ assert isinstance(result["content"], list)
+ assert len(result["content"]) == 1
+
+ # Verify image content without cache_control (cache_control will be None if not set)
+ assert result["content"][0]["type"] == "image"
+ assert result["content"][0]["source"]["type"] == "base64"
+ assert result["content"][0]["source"]["media_type"] == "image/png"
+ assert result["content"][0].get("cache_control") is None
+
+
+def test_convert_to_anthropic_tool_result_mixed_content_with_cache_control():
+ """
+ Test tool results with mixed content types (text and image) where only some have cache_control.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_anthropic_tool_result,
+ )
+
+ message = {
+ "role": "tool",
+ "tool_call_id": "call_test_789",
+ "content": [
+ {
+ "type": "text",
+ "text": "First image:",
+ "cache_control": {"type": "ephemeral"},
+ },
+ {
+ "type": "image_url",
+ "image_url": "data:image/jpeg;base64,/9j/4AAQSkZJRg",
+ "cache_control": {"type": "ephemeral"},
+ },
+ {
+ "type": "text",
+ "text": "Second image (no cache):",
+ },
+ {
+ "type": "image_url",
+ "image_url": "data:image/png;base64,iVBORw0KGgo",
+ },
+ ],
+ }
+
+ result = convert_to_anthropic_tool_result(message)
+
+ assert result["type"] == "tool_result"
+ assert isinstance(result["content"], list)
+ assert len(result["content"]) == 4
+
+ # First text with cache_control
+ assert result["content"][0]["type"] == "text"
+ assert result["content"][0]["cache_control"]["type"] == "ephemeral"
+
+ # First image with cache_control
+ assert result["content"][1]["type"] == "image"
+ assert result["content"][1]["cache_control"]["type"] == "ephemeral"
+
+ # Second text without cache_control (cache_control will be None if not set)
+ assert result["content"][2]["type"] == "text"
+ assert result["content"][2].get("cache_control") is None
+
+ # Second image without cache_control (cache_control will be None if not set)
+ assert result["content"][3]["type"] == "image"
+ assert result["content"][3].get("cache_control") is None
+
+
+def test_convert_to_anthropic_tool_result_image_url_as_http():
+ """
+ Test that HTTP/HTTPS URLs with cache_control are handled correctly.
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import (
+ convert_to_anthropic_tool_result,
+ )
+
+ message = {
+ "role": "tool",
+ "tool_call_id": "call_http_001",
+ "content": [
+ {
+ "type": "image_url",
+ "image_url": "https://example.com/image.jpg",
+ "cache_control": {"type": "ephemeral"},
+ },
+ ],
+ }
+
+ result = convert_to_anthropic_tool_result(message)
+
+ # Verify image is passed as URL reference with cache_control
+ assert result["content"][0]["type"] == "image"
+ assert result["content"][0]["source"]["type"] == "url"
+ assert result["content"][0]["source"]["url"] == "https://example.com/image.jpg"
+ assert result["content"][0]["cache_control"]["type"] == "ephemeral"
+ def test_anthropic_messages_pt_server_tool_use_passthrough():
+ """
+ Test that anthropic_messages_pt passes through server_tool_use and
+ tool_search_tool_result blocks in assistant message content.
+
+ These are Anthropic-native content types used for tool search functionality
+ that need to be preserved when reconstructing multi-turn conversations.
+
+ Fixes: https://github.com/BerriAI/litellm/issues/XXXXX
+ """
+ from litellm.litellm_core_utils.prompt_templates.factory import anthropic_messages_pt
+
+ messages = [
+ {
+ "role": "user",
+ "content": "I need help with time information."
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "server_tool_use",
+ "id": "srvtoolu_01ABC123",
+ "name": "tool_search_tool_regex",
+ "input": {"query": ".*time.*"}
+ },
+ {
+ "type": "tool_search_tool_result",
+ "tool_use_id": "srvtoolu_01ABC123",
+ "content": {
+ "type": "tool_search_tool_search_result",
+ "tool_references": [
+ {"type": "tool_reference", "tool_name": "get_time"}
+ ]
+ }
+ },
+ {
+ "type": "text",
+ "text": "I found the time tool. How can I help you?"
+ }
+ ],
+ },
+ {
+ "role": "user",
+ "content": "What's the time in New York?"
+ },
+ ]
+
+ result = anthropic_messages_pt(
+ messages=messages,
+ model="claude-sonnet-4-5-20250929",
+ llm_provider="anthropic",
+ )
+
+ # Verify we have 3 messages (user, assistant, user)
+ assert len(result) == 3
+
+ # Verify the assistant message content
+ assistant_msg = result[1]
+ assert assistant_msg["role"] == "assistant"
+ assert isinstance(assistant_msg["content"], list)
+
+ # Find the different content block types
+ content_types = [block.get("type") for block in assistant_msg["content"]]
+
+ # Verify server_tool_use block is preserved
+ assert "server_tool_use" in content_types
+ server_tool_use_block = next(
+ b for b in assistant_msg["content"] if b.get("type") == "server_tool_use"
+ )
+ assert server_tool_use_block["id"] == "srvtoolu_01ABC123"
+ assert server_tool_use_block["name"] == "tool_search_tool_regex"
+ assert server_tool_use_block["input"] == {"query": ".*time.*"}
+
+ # Verify tool_search_tool_result block is preserved
+ assert "tool_search_tool_result" in content_types
+ tool_result_block = next(
+ b for b in assistant_msg["content"] if b.get("type") == "tool_search_tool_result"
+ )
+ assert tool_result_block["tool_use_id"] == "srvtoolu_01ABC123"
+ assert tool_result_block["content"]["type"] == "tool_search_tool_search_result"
+ assert tool_result_block["content"]["tool_references"][0]["tool_name"] == "get_time"
+
+ # Verify text block is also preserved
+ assert "text" in content_types
+ text_block = next(
+ b for b in assistant_msg["content"] if b.get("type") == "text"
+ )
+ assert text_block["text"] == "I found the time tool. How can I help you?"