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
test test_redis_caching_multiple_namespaces (#12552)
Ishaan Jafff0e87d4eb03d
diff --git a/tests/local_testing/test_caching.py b/tests/local_testing/test_caching.pyindex 74108038708..8bbe63c5468 100644--- a/tests/local_testing/test_caching.py+++ b/tests/local_testing/test_caching.py@@ -2602,39 +2602,124 @@ def test_redis_caching_multiple_namespaces():The same request with different namespaces should not be cached under the same key"""import uuid+ from unittest.mock import patch, MagicMock+ import litellm+ from litellm.caching import Cache+ from litellm import completion++ # Use a fixed uuid to ensure consistent cache keys+ test_uuid = "12345678-1234-1234-1234-123456789abc"+ messages = [{"role": "user", "content": f"what is litellm? {test_uuid}"}]++ # Mock the Redis client creation from the _redis module+ with patch('litellm._redis.get_redis_client') as mock_get_redis_client, \+ patch('litellm._redis.get_redis_connection_pool') as mock_get_redis_connection_pool:+ # Create a mock Redis client that simulates real Redis behavior+ mock_redis_client = MagicMock()+ mock_get_redis_client.return_value = mock_redis_client++ # Mock the connection pool+ mock_connection_pool = MagicMock()+ mock_get_redis_connection_pool.return_value = mock_connection_pool++ # Dictionary to simulate Redis storage with namespace support+ redis_storage = {}++ def mock_redis_get(key):+ print(f"Redis GET: {key}")+ value = redis_storage.get(key, None)+ # Convert to bytes to match real Redis behavior+ if value is not None:+ import json+ return json.dumps(value).encode('utf-8')+ return None++ def mock_redis_set(name, value, ex=None, **kwargs):+ print(f"Redis SET: {name} = {value}")+ redis_storage[name] = value+ return True++ def mock_redis_ping():+ return True++ def mock_redis_info():+ return {"redis_version": "7.0.0"}++ mock_redis_client.get = mock_redis_get+ mock_redis_client.set = mock_redis_set+ mock_redis_client.ping = mock_redis_ping+ mock_redis_client.info = mock_redis_info++ # Initialize the cache+ litellm.cache = Cache(type="redis")++ namespace_1 = "org-id1"+ namespace_2 = "org-id2"++ # Use mock_response to ensure deterministic responses without external API calls+ response_1 = completion(+ model="gpt-3.5-turbo",+ messages=messages,+ cache={"namespace": namespace_1},+ mock_response="Response for namespace 1"+ )- messages = [{"role": "user", "content": f"what is litellm? {uuid.uuid4()}"}]- litellm.cache = Cache(type="redis")- namespace_1 = "org-id1"- namespace_2 = "org-id2"-- response_1 = completion(- model="gpt-3.5-turbo", messages=messages, cache={"namespace": namespace_1}- )-- response_2 = completion(- model="gpt-3.5-turbo", messages=messages, cache={"namespace": namespace_2}- )-- response_3 = completion(- model="gpt-3.5-turbo", messages=messages, cache={"namespace": namespace_1}- )-- response_4 = completion(model="gpt-3.5-turbo", messages=messages)-- print("response 1: ", response_1.model_dump_json(indent=4))- print("response 2: ", response_2.model_dump_json(indent=4))- print("response 3: ", response_3.model_dump_json(indent=4))- print("response 4: ", response_4.model_dump_json(indent=4))+ response_2 = completion(+ model="gpt-3.5-turbo",+ messages=messages,+ cache={"namespace": namespace_2},+ mock_response="Response for namespace 2"+ )- # request 1 & 3 used under the same namespace- assert response_1.id == response_3.id+ response_3 = completion(+ model="gpt-3.5-turbo",+ messages=messages,+ cache={"namespace": namespace_1},+ mock_response="This should be cached"+ )- # request 2 used under a different namespace- assert response_2.id != response_1.id+ response_4 = completion(+ model="gpt-3.5-turbo",+ messages=messages,+ mock_response="Response without namespace"+ )- # request 4 without a namespace should not be cached under the same key as request 3- assert response_4.id != response_3.id+ print(f"Response 1 type: {type(response_1)} - ID: {getattr(response_1, 'id', 'N/A')}")+ print(f"Response 2 type: {type(response_2)} - ID: {getattr(response_2, 'id', 'N/A')}")+ print(f"Response 3 type: {type(response_3)} - Cache hit: {isinstance(response_3, str)}")+ print(f"Response 4 type: {type(response_4)} - ID: {getattr(response_4, 'id', 'N/A')}")++ print(f"Redis storage keys: {list(redis_storage.keys())}")++ # Verify that different namespaces created different cache keys+ cache_keys = list(redis_storage.keys())+ namespace_1_keys = [k for k in cache_keys if k.startswith(f"{namespace_1}:")]+ namespace_2_keys = [k for k in cache_keys if k.startswith(f"{namespace_2}:")]+ no_namespace_keys = [k for k in cache_keys if not k.startswith(f"{namespace_1}:") and not k.startswith(f"{namespace_2}:")]++ print(f"Namespace 1 keys: {namespace_1_keys}")+ print(f"Namespace 2 keys: {namespace_2_keys}")+ print(f"No namespace keys: {no_namespace_keys}")++ # Should have at least one key for each namespace+ assert len(namespace_1_keys) > 0, "Should have cache keys for namespace 1"+ assert len(namespace_2_keys) > 0, "Should have cache keys for namespace 2"+ assert len(no_namespace_keys) > 0, "Should have cache keys for no namespace"++ # The main test: response 3 should be a cache hit (string) because it uses same namespace as response 1+ assert isinstance(response_3, str), "Response 3 should be a cache hit (string) for same namespace"++ # response 1 & 2 should be ModelResponse objects (cache misses)+ assert hasattr(response_1, 'id'), "Response 1 should be a ModelResponse object"+ assert hasattr(response_2, 'id'), "Response 2 should be a ModelResponse object"+ assert hasattr(response_4, 'id'), "Response 4 should be a ModelResponse object"++ # response 1 & 2 should have different IDs (different namespaces)+ assert response_1.id != response_2.id, f"Expected different response ID for different namespace. Got {response_1.id} and {response_2.id}"++ # response 1 & 4 should have different IDs (different namespaces)+ assert response_1.id != response_4.id, f"Expected different response ID for no namespace vs namespaced. Got {response_1.id} and {response_4.id}"+def test_caching_with_reasoning_content():