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
chore(caching): align redis semantic miss metadata
userd8c11f962296
diff --git a/litellm/caching/redis_semantic_cache.py b/litellm/caching/redis_semantic_cache.pyindex 06e9b4d0fa9..85d3a4f1b4b 100644--- a/litellm/caching/redis_semantic_cache.py+++ b/litellm/caching/redis_semantic_cache.py@@ -290,6 +290,7 @@ class RedisSemanticCache(BaseCache):messages = kwargs.get("messages", [])if not messages:print_verbose("No messages provided for semantic cache lookup")+ kwargs.setdefault("metadata", {})["semantic-similarity"] = 0.0return Noneprompt = get_str_from_messages(messages)@@ -302,12 +303,14 @@ class RedisSemanticCache(BaseCache):# Return None if no similar prompts foundif not results:+ kwargs.setdefault("metadata", {})["semantic-similarity"] = 0.0return None# Process the best matching resultcache_hit = results[0]if not self._cache_hit_matches_key(cache_hit=cache_hit, key=key):print_verbose("Redis semantic-cache hit did not match cache key scope")+ kwargs.setdefault("metadata", {})["semantic-similarity"] = 0.0return Nonevector_distance = float(cache_hit["vector_distance"])@@ -319,6 +322,9 @@ class RedisSemanticCache(BaseCache):cached_prompt = cache_hit["prompt"]cached_response = cache_hit["response"]+ # update kwargs["metadata"] with similarity, don't rewrite the original metadata+ kwargs.setdefault("metadata", {})["semantic-similarity"] = similarity+print_verbose(f"Cache hit: similarity threshold: {self.similarity_threshold}, "f"actual similarity: {similarity}, "@@ -329,6 +335,7 @@ class RedisSemanticCache(BaseCache):return self._get_cache_logic(cached_response=cached_response)except Exception as e:print_verbose(f"Error retrieving from Redis semantic cache: {str(e)}")+ kwargs.setdefault("metadata", {})["semantic-similarity"] = 0.0async def _get_async_embedding(self, prompt: str, **kwargs) -> List[float]:"""diff --git a/tests/test_litellm/caching/test_redis_semantic_cache.py b/tests/test_litellm/caching/test_redis_semantic_cache.pyindex b702de8d5f0..f5c84c9d3ef 100644--- a/tests/test_litellm/caching/test_redis_semantic_cache.py+++ b/tests/test_litellm/caching/test_redis_semantic_cache.py@@ -90,12 +90,16 @@ def test_redis_semantic_cache_get_cache(monkeypatch):),):# Test get_cache with a message+ metadata = {}result = redis_semantic_cache.get_cache(- key="test_key", messages=[{"content": "What is the capital of France?"}]+ key="test_key",+ messages=[{"content": "What is the capital of France?"}],+ metadata=metadata,)# Verify result is properly parsedassert result == {"content": "Paris is the capital of France."}+ assert metadata["semantic-similarity"] == pytest.approx(0.9)# Verify llmcache.check was calledredis_semantic_cache.llmcache.check.assert_called_once_with(@@ -139,12 +143,15 @@ def test_redis_semantic_cache_rejects_unscoped_cache_hit(monkeypatch):"_get_cache_key_filter_expression",return_value="cache-key-filter",):+ metadata = {}result = redis_semantic_cache.get_cache(key="test_key",messages=[{"content": "What is the capital of France?"}],+ metadata=metadata,)assert result is None+ assert metadata["semantic-similarity"] == 0.0def test_redis_semantic_cache_set_cache_stores_cache_key_filter(monkeypatch):