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]

test(anthropic): let monkeypatch own litellm.callbacks in the cache control tests (#37847)

yuneng-jiang89187cd030c7
diff --git a/test-quality-budget.json b/test-quality-budget.json
index 38fd31d7275..cbfc8f4ffee 100644
--- a/test-quality-budget.json
+++ b/test-quality-budget.json
@@ -12,7 +12,7 @@
"limit": 469
},
"TQ005": {
- "limit": 2436
+ "limit": 2421
},
"TQ006": {
"limit": 34
diff --git a/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py b/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py
index 7bf15f59eb9..8fb83a17296 100644
--- a/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py
+++ b/tests/test_litellm/integrations/test_anthropic_cache_control_hook.py
@@ -37,7 +37,7 @@ def _rendered_log_message(call):
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_system_message():
+async def test_anthropic_cache_control_hook_system_message(monkeypatch: pytest.MonkeyPatch):
# Use patch.dict to mock environment variables instead of setting them directly
with patch.dict(
os.environ,
@@ -48,7 +48,7 @@ async def test_anthropic_cache_control_hook_system_message():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -116,7 +116,7 @@ async def test_anthropic_cache_control_hook_system_message():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_user_message():
+async def test_anthropic_cache_control_hook_user_message(monkeypatch: pytest.MonkeyPatch):
# Use patch.dict to mock environment variables instead of setting them directly
with patch.dict(
os.environ,
@@ -127,7 +127,7 @@ async def test_anthropic_cache_control_hook_user_message():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -188,7 +188,7 @@ async def test_anthropic_cache_control_hook_user_message():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_negative_indices():
+async def test_anthropic_cache_control_hook_negative_indices(monkeypatch: pytest.MonkeyPatch):
"""
Test the bug fix for handling negative indices in cache control injection points.
This test verifies that negative indices (-1, -2) are properly converted to positive indices
@@ -204,7 +204,7 @@ async def test_anthropic_cache_control_hook_negative_indices():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -302,7 +302,7 @@ async def test_anthropic_cache_control_hook_negative_indices():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_out_of_bounds_logging():
+async def test_anthropic_cache_control_hook_out_of_bounds_logging(monkeypatch: pytest.MonkeyPatch):
"""
Test that warning logs are generated when out-of-bounds indices are used.
This verifies that the verbose_logger.warning is called with the correct message.
@@ -316,7 +316,7 @@ async def test_anthropic_cache_control_hook_out_of_bounds_logging():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -365,7 +365,7 @@ async def test_anthropic_cache_control_hook_out_of_bounds_logging():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_negative_out_of_bounds_logging():
+async def test_anthropic_cache_control_hook_negative_out_of_bounds_logging(monkeypatch: pytest.MonkeyPatch):
"""
Test that warning logs are generated for negative indices that are out of bounds.
"""
@@ -378,7 +378,7 @@ async def test_anthropic_cache_control_hook_negative_out_of_bounds_logging():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -431,7 +431,7 @@ async def test_anthropic_cache_control_hook_negative_out_of_bounds_logging():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_multiple_user_messages():
+async def test_anthropic_cache_control_hook_multiple_user_messages(monkeypatch: pytest.MonkeyPatch):
"""
Test cache control injection on multiple user messages specifically.
Note: Bedrock API combines consecutive user messages into a single message with multiple content blocks.
@@ -445,7 +445,7 @@ async def test_anthropic_cache_control_hook_multiple_user_messages():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -523,7 +523,7 @@ async def test_anthropic_cache_control_hook_multiple_user_messages():
@pytest.mark.asyncio
@pytest.mark.parametrize("bad_index", [10, -10])
-async def test_anthropic_cache_control_hook_out_of_bounds(bad_index):
+async def test_anthropic_cache_control_hook_out_of_bounds(bad_index, monkeypatch: pytest.MonkeyPatch):
"""
Verify the hook does not raise an error and makes no changes
when an out-of-bounds index is provided.
@@ -537,7 +537,7 @@ async def test_anthropic_cache_control_hook_out_of_bounds(bad_index):
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -586,7 +586,7 @@ async def test_anthropic_cache_control_hook_out_of_bounds(bad_index):
"message_list",
[[{"role": "user", "content": "Single message"}]], # Single message only - empty list will fail at API level
)
-async def test_anthropic_cache_control_hook_single_message(message_list):
+async def test_anthropic_cache_control_hook_single_message(message_list, monkeypatch: pytest.MonkeyPatch):
"""
Verify the hook runs without error on very short message lists.
"""
@@ -599,7 +599,7 @@ async def test_anthropic_cache_control_hook_single_message(message_list):
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -637,7 +637,7 @@ async def test_anthropic_cache_control_hook_single_message(message_list):
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_empty_message_list():
+async def test_anthropic_cache_control_hook_empty_message_list(monkeypatch: pytest.MonkeyPatch):
"""
Verify that empty message lists are handled appropriately (should fail at API level, not hook level).
"""
@@ -650,7 +650,7 @@ async def test_anthropic_cache_control_hook_empty_message_list():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
client = AsyncHTTPHandler()
with patch.object(client, "post", return_value=MagicMock()) as mock_post:
@@ -668,7 +668,7 @@ async def test_anthropic_cache_control_hook_empty_message_list():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_no_op():
+async def test_anthropic_cache_control_hook_no_op(monkeypatch: pytest.MonkeyPatch):
"""
Verify that if no injection points are specified, messages remain unmodified.
"""
@@ -681,7 +681,7 @@ async def test_anthropic_cache_control_hook_no_op():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
# Mock response data
mock_response = MagicMock()
@@ -726,7 +726,7 @@ async def test_anthropic_cache_control_hook_no_op():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_multiple_content_items_last_only():
+async def test_anthropic_cache_control_hook_multiple_content_items_last_only(monkeypatch: pytest.MonkeyPatch):
"""
Test that cache_control is only applied to the last content item in a list, not all items.
This verifies the fix for https://github.com/BerriAI/litellm/issues/15696
@@ -740,7 +740,7 @@ async def test_anthropic_cache_control_hook_multiple_content_items_last_only():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
mock_response = MagicMock()
mock_response.json.return_value = {
@@ -797,7 +797,7 @@ async def test_anthropic_cache_control_hook_multiple_content_items_last_only():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_document_analysis_multiple_pages():
+async def test_anthropic_cache_control_hook_document_analysis_multiple_pages(monkeypatch: pytest.MonkeyPatch):
"""
Test cache_control with multiple document pages to ensure only the last page gets cached.
This simulates document analysis with 6 content blocks, verifying the fix for issue 15696.
@@ -811,7 +811,7 @@ async def test_anthropic_cache_control_hook_document_analysis_multiple_pages():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
mock_response = MagicMock()
mock_response.json.return_value = {
@@ -969,7 +969,7 @@ def test_gemini_cache_control_injection_list_content_detected():
@pytest.mark.asyncio
-async def test_anthropic_cache_control_hook_string_negative_index():
+async def test_anthropic_cache_control_hook_string_negative_index(monkeypatch: pytest.MonkeyPatch):
"""
Test that string negative indices like "-1" are handled correctly.
@@ -986,7 +986,7 @@ async def test_anthropic_cache_control_hook_string_negative_index():
},
):
anthropic_cache_control_hook = AnthropicCacheControlHook()
- litellm.callbacks = [anthropic_cache_control_hook]
+ monkeypatch.setattr(litellm, "callbacks", [anthropic_cache_control_hook])
mock_response = MagicMock()
mock_response.json.return_value = {
@@ -1185,7 +1185,7 @@ def test_cache_control_hook_does_not_overwrite_existing_cache_control():
@pytest.mark.asyncio
-async def test_cache_control_hook_bedrock_payload_caps_cachepoints_at_four():
+async def test_cache_control_hook_bedrock_payload_caps_cachepoints_at_four(monkeypatch: pytest.MonkeyPatch):
"""End-to-end: outgoing Bedrock payload must not exceed 4 cachePoint blocks.
Reproduces the customer report where 4 client cache_control system blocks
@@ -1199,7 +1199,7 @@ async def test_cache_control_hook_bedrock_payload_caps_cachepoints_at_four():
"AWS_REGION_NAME": "us-east-1",
},
):
- litellm.callbacks = [AnthropicCacheControlHook()]
+ monkeypatch.setattr(litellm, "callbacks", [AnthropicCacheControlHook()])
mock_response = MagicMock()
mock_response.json.return_value = {
@@ -1289,7 +1289,7 @@ def test_cache_control_hook_reserves_slot_for_tool_config_point():
@pytest.mark.asyncio
-async def test_cache_control_hook_bedrock_payload_caps_with_tool_config_point():
+async def test_cache_control_hook_bedrock_payload_caps_with_tool_config_point(monkeypatch: pytest.MonkeyPatch):
"""End-to-end: message + tool_config injection must not exceed 4 cachePoints."""
with patch.dict(
os.environ,
@@ -1299,7 +1299,7 @@ async def test_cache_control_hook_bedrock_payload_caps_with_tool_config_point():
"AWS_REGION_NAME": "us-east-1",
},
):
- litellm.callbacks = [AnthropicCacheControlHook()]
+ monkeypatch.setattr(litellm, "callbacks", [AnthropicCacheControlHook()])
mock_response = MagicMock()
mock_response.json.return_value = {