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
fix(caching): keep generic add_cache failures at ERROR unless the backend is Redis
yassinbea22df6c27b
diff --git a/litellm/caching/caching.py b/litellm/caching/caching.pyindex d6dd2a073af..be82f5def1f 100644--- a/litellm/caching/caching.py+++ b/litellm/caching/caching.py@@ -679,7 +679,14 @@ class Cache:cache_key, cached_data, kwargs = self._add_cache_logic(result=result, **kwargs)self.cache.set_cache(cache_key, cached_data, **kwargs)except Exception as e:- log_redis_failure(verbose_logger, logging.ERROR, "LiteLLM Cache: exception in add_cache", e)+ self._log_add_cache_failure(e)++ def _log_add_cache_failure(self, exc: Exception) -> None:+ message: Final = "LiteLLM Cache: exception in add_cache"+ if isinstance(self.cache, RedisCache):+ log_redis_failure(verbose_logger, logging.ERROR, message, exc)+ return+ verbose_logger.error("%s: %s", message, exc)async def async_add_cache(self, result, dynamic_cache_object: BaseCache | None = None, **kwargs):"""@@ -698,7 +705,7 @@ class Cache:else:await self.cache.async_set_cache(cache_key, cached_data, **kwargs)except Exception as e:- log_redis_failure(verbose_logger, logging.ERROR, "LiteLLM Cache: exception in add_cache", e)+ self._log_add_cache_failure(e)def _convert_to_cached_embedding(self,@@ -877,7 +884,7 @@ class Cache:else:await self.cache.async_set_cache_pipeline(cache_list=cache_list, **kwargs)except Exception as e:- log_redis_failure(verbose_logger, logging.ERROR, "LiteLLM Cache: exception in add_cache", e)+ self._log_add_cache_failure(e)def should_use_cache(self, **kwargs):"""diff --git a/tests/test_litellm/caching/test_caching.py b/tests/test_litellm/caching/test_caching.pyindex 4d0ec0fb677..c7ec8abc31e 100644--- a/tests/test_litellm/caching/test_caching.py+++ b/tests/test_litellm/caching/test_caching.py@@ -1,9 +1,12 @@import loggingimport re+from unittest.mock import MagicMockimport pytest+import litellm.caching.redis_cache as redis_cache_modulefrom litellm.caching.caching import Cache+from litellm.caching.redis_cache import RedisCache, _RedisTimeoutLogThrottlefrom litellm.types.caching import LiteLLMCacheType, SemanticCacheScopefrom litellm.types.utils import Embedding, EmbeddingResponse, Usage@@ -53,6 +56,29 @@ def test_cache_key_debug_log_does_not_include_prompt_material(caplog):assert any(cache_key in message for message in created_cache_key_logs)+ ("backend", "expected_level"),+ [+ pytest.param(MagicMock(spec=RedisCache), logging.DEBUG, id="redis_backend_is_throttled"),+ pytest.param(MagicMock(), logging.ERROR, id="other_backend_logs_every_timeout"),+ ],+)+def test_add_cache_timeout_only_joins_redis_throttle_for_redis_backends(backend, expected_level, caplog, monkeypatch):+ throttle = _RedisTimeoutLogThrottle(interval=5.0, clock=MagicMock(return_value=1_000.0))+ assert throttle.admit() == 0+ monkeypatch.setattr(redis_cache_module, "_redis_timeout_log_throttle", throttle)++ cache = Cache(type=LiteLLMCacheType.LOCAL)+ backend.set_cache.side_effect = TimeoutError("lit7520 backend timed out")+ cache.cache = backend++ with caplog.at_level(logging.DEBUG, logger="LiteLLM"):+ cache.add_cache("result", model="gpt-4.1-mini", messages=[{"role": "user", "content": "hi"}])++ records = [r for r in caplog.records if "lit7520 backend timed out" in r.getMessage()]+ assert [r.levelno for r in records] == [expected_level]++def _embedding_response(prompt_tokens, num_items):return EmbeddingResponse(model="amazon.titan-embed-image-v1",