Skip to content

Commit

Permalink
Revert "fix(_redis.py): allow all supported arguments for redis clust…
Browse files Browse the repository at this point in the history
…er (#5554)" (#5583)

This reverts commit f2191ef.
  • Loading branch information
krrishdholakia authored Sep 7, 2024
1 parent f2191ef commit 8811884
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions litellm/_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# s/o [@Frank Colson](https://www.linkedin.com/in/frank-colson-422b9b183/) for this redis implementation
import os
from typing import List
from typing import List, Optional

import redis # type: ignore
import redis.asyncio as async_redis # type: ignore
Expand Down Expand Up @@ -55,6 +55,19 @@ def _get_redis_url_kwargs(client=None):
return available_args


def _get_redis_cluster_kwargs(client=None):
if client is None:
client = redis.Redis.from_url
arg_spec = inspect.getfullargspec(redis.RedisCluster)

# Only allow primitive arguments
exclude_args = {"self", "connection_pool", "retry", "host", "port", "startup_nodes"}

available_args = [x for x in arg_spec.args if x not in exclude_args]

return available_args


def _get_redis_env_kwarg_mapping():
PREFIX = "REDIS_"

Expand Down Expand Up @@ -130,10 +143,13 @@ def get_redis_client(**env_overrides):
return redis.Redis.from_url(**url_kwargs)

if "startup_nodes" in redis_kwargs:
from redis.cluster import ClusterNode, cleanup_kwargs
from redis.cluster import ClusterNode

# Only allow primitive arguments
cluster_kwargs = cleanup_kwargs(**redis_kwargs)
args = _get_redis_cluster_kwargs()
cluster_kwargs = {}
for arg in redis_kwargs:
if arg in args:
cluster_kwargs[arg] = redis_kwargs[arg]

new_startup_nodes: List[ClusterNode] = []

Expand Down Expand Up @@ -161,10 +177,14 @@ def get_redis_async_client(**env_overrides):
return async_redis.Redis.from_url(**url_kwargs)

if "startup_nodes" in redis_kwargs:
from redis.cluster import ClusterNode, cleanup_kwargs
from redis.cluster import ClusterNode

args = _get_redis_cluster_kwargs()
cluster_kwargs = {}
for arg in redis_kwargs:
if arg in args:
cluster_kwargs[arg] = redis_kwargs[arg]

# Only allow primitive arguments
cluster_kwargs = cleanup_kwargs(**redis_kwargs)
new_startup_nodes: List[ClusterNode] = []

for item in redis_kwargs["startup_nodes"]:
Expand Down
2 changes: 1 addition & 1 deletion litellm/tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ async def test_redis_cache_cluster_init_unit_test():
assert isinstance(resp.redis_client, RedisCluster)
assert isinstance(resp.init_async_client(), AsyncRedisCluster)

resp = litellm.Cache(type="redis", redis_startup_nodes=startup_nodes, password="my-cluster-password")
resp = litellm.Cache(type="redis", redis_startup_nodes=startup_nodes)

assert isinstance(resp.cache, RedisCache)
assert isinstance(resp.cache.redis_client, RedisCluster)
Expand Down

0 comments on commit 8811884

Please sign in to comment.