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: Create RedisClusterCache when startup nodes provided in cache args of router (#9010)
Ogun Oz85d1427710e9
diff --git a/litellm/router.py b/litellm/router.pyindex 9ddd5960067..d1c410e7865 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -44,7 +44,12 @@ import litellm.litellm_core_utilsimport litellm.litellm_core_utils.exception_mapping_utilsfrom litellm import get_secret_strfrom litellm._logging import verbose_router_logger-from litellm.caching.caching import DualCache, InMemoryCache, RedisCache+from litellm.caching.caching import (+ DualCache,+ InMemoryCache,+ RedisCache,+ RedisClusterCache,+)from litellm.integrations.custom_logger import CustomLoggerfrom litellm.litellm_core_utils.asyncify import run_async_functionfrom litellm.litellm_core_utils.core_helpers import _get_parent_otel_span_from_kwargs@@ -351,7 +356,7 @@ class Router:# Add additional key-value pairs from cache_kwargscache_config.update(cache_kwargs)- redis_cache = RedisCache(**cache_config)+ redis_cache = self._create_redis_cache(cache_config)if cache_responses:if litellm.cache is None:@@ -611,6 +616,15 @@ class Router:litellm.callbacks, self)+ @staticmethod+ def _create_redis_cache(+ cache_config: dict[str, Any]+ ) -> RedisCache | RedisClusterCache:+ if cache_config.get("startup_nodes"):+ return RedisClusterCache(**cache_config)+ else:+ return RedisCache(**cache_config)+def _update_redis_cache(self, cache: RedisCache):"""Update the redis cache for the router, if none set.diff --git a/tests/local_testing/test_router_caching.py b/tests/local_testing/test_router_caching.pyindex 88e9111bfdb..53a79b94341 100644--- a/tests/local_testing/test_router_caching.py+++ b/tests/local_testing/test_router_caching.py@@ -5,6 +5,7 @@ import osimport sysimport timeimport traceback+from unittest.mock import patchimport pytest@@ -13,6 +14,8 @@ sys.path.insert() # Adds the parent directory to the system pathimport litellmfrom litellm import Router+from litellm.caching import RedisCache, RedisClusterCache+## Scenarios## 1. 2 models - openai + azure - 1 model group "gpt-3.5-turbo",@@ -322,3 +325,36 @@ async def test_acompletion_caching_on_router_caching_groups():except Exception as e:traceback.print_exc()pytest.fail(f"Error occurred: {e}")+++ "startup_nodes, expected_cache_type",+ [+ pytest.param(+ [dict(host="node1.localhost", port=6379)],+ RedisClusterCache,+ id="Expects a RedisClusterCache instance when startup_nodes provided",+ ),+ pytest.param(+ None,+ RedisCache,+ id="Expects a RedisCache instance when there is no startup nodes",+ ),+ ],+)+def test_create_correct_redis_cache_instance(+ startup_nodes: list[dict] | None,+ expected_cache_type: type[RedisClusterCache | RedisCache],+):+ cache_config = dict(+ host="mockhost",+ port=6379,+ password="mock-password",+ startup_nodes=startup_nodes,+ )++ def _mock_redis_cache_init(*args, **kwargs): ...++ with patch.object(RedisCache, "__init__", _mock_redis_cache_init):+ redis_cache = Router._create_redis_cache(cache_config)+ assert isinstance(redis_cache, expected_cache_type)