diff --git a/litellm/_redis.py b/litellm/_redis.py index 69ff6f3f2cb..e2688bf418c 100644 --- a/litellm/_redis.py +++ b/litellm/_redis.py @@ -32,6 +32,25 @@ def _get_redis_kwargs(): return available_args +def _get_redis_url_kwargs(client=None): + if client is None: + client = redis.Redis.from_url + arg_spec = inspect.getfullargspec(redis.Redis.from_url) + + # Only allow primitive arguments + exclude_args = { + "self", + "connection_pool", + "retry", + } + + include_args = ["url"] + + available_args = [x for x in arg_spec.args if x not in exclude_args] + include_args + + return available_args + + def _get_redis_env_kwarg_mapping(): PREFIX = "REDIS_" @@ -98,20 +117,31 @@ def _get_redis_client_logic(**env_overrides): def get_redis_client(**env_overrides): redis_kwargs = _get_redis_client_logic(**env_overrides) if "url" in redis_kwargs and redis_kwargs["url"] is not None: - redis_kwargs.pop( - "connection_pool", None - ) # redis.from_url doesn't support setting your own connection pool - return redis.Redis.from_url(**redis_kwargs) + args = _get_redis_url_kwargs() + url_kwargs = {} + for arg in redis_kwargs: + if arg in args: + url_kwargs[arg] = redis_kwargs[arg] + + return redis.Redis.from_url(**url_kwargs) return redis.Redis(**redis_kwargs) def get_redis_async_client(**env_overrides): redis_kwargs = _get_redis_client_logic(**env_overrides) if "url" in redis_kwargs and redis_kwargs["url"] is not None: - redis_kwargs.pop( - "connection_pool", None - ) # redis.from_url doesn't support setting your own connection pool - return async_redis.Redis.from_url(**redis_kwargs) + args = _get_redis_url_kwargs(client=async_redis.Redis.from_url) + url_kwargs = {} + for arg in redis_kwargs: + if arg in args: + url_kwargs[arg] = redis_kwargs[arg] + else: + litellm.print_verbose( + "REDIS: ignoring argument: {}. Not an allowed async_redis.Redis.from_url arg.".format( + arg + ) + ) + return async_redis.Redis.from_url(**url_kwargs) return async_redis.Redis( socket_timeout=5, **redis_kwargs, diff --git a/litellm/caching.py b/litellm/caching.py index 87d99587f82..22e53d6f935 100644 --- a/litellm/caching.py +++ b/litellm/caching.py @@ -174,6 +174,9 @@ class RedisCache(BaseCache): except Exception: pass + ### SYNC HEALTH PING ### + self.redis_client.ping() + def init_async_client(self): from ._redis import get_redis_async_client