From 5bc62d32dca2c44efdf4c9e2a907c89f07b41118 Mon Sep 17 00:00:00 2001 From: "shuwen.wu" Date: Wed, 2 Sep 2026 17:58:02 +0800 Subject: [PATCH 1/5] fix: ValkeySemanticCache import compatibility with redis-py >= 5 Fixes #39180 - ValkeySemanticCache crashes at import with redis-py >= 5 due to stale camelCase import of redis.commands.search.indexDefinition. redis-py >= 5 uses snake_case module name (index_definition), while redis-py 4.x uses camelCase (indexDefinition). Add try/except to support both versions. --- litellm/caching/valkey_semantic_cache.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/caching/valkey_semantic_cache.py b/litellm/caching/valkey_semantic_cache.py index 58b76d98d6d..4f6e1b9564f 100644 --- a/litellm/caching/valkey_semantic_cache.py +++ b/litellm/caching/valkey_semantic_cache.py @@ -24,7 +24,10 @@ from typing import Any, Final from redis import Redis from redis.asyncio import Redis as AsyncRedis from redis.commands.search.field import TagField, VectorField -from redis.commands.search.indexDefinition import IndexDefinition, IndexType +try: # redis-py >= 5 (module renamed to snake_case) + from redis.commands.search.index_definition import IndexDefinition, IndexType +except ModuleNotFoundError: # redis-py 4.x (last version with camelCase name) + from redis.commands.search.indexDefinition import IndexDefinition, IndexType from redis.commands.search.query import Query from litellm._logging import print_verbose From 1ecf44dc85d027888e422aeb3c18f77cc1a2b7e1 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 4 Sep 2026 14:12:50 +0800 Subject: [PATCH 2/5] test: cover legacy redis search imports --- .../caching/test_valkey_semantic_cache.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_litellm/caching/test_valkey_semantic_cache.py b/tests/test_litellm/caching/test_valkey_semantic_cache.py index 749658784ac..f128795039d 100644 --- a/tests/test_litellm/caching/test_valkey_semantic_cache.py +++ b/tests/test_litellm/caching/test_valkey_semantic_cache.py @@ -534,3 +534,45 @@ def test_importing_caching_does_not_require_redis(): ) assert result.returncode == 0, result.stderr assert "ok" in result.stdout + + +def test_valkey_cache_supports_redis_py_4_index_module(): + code = textwrap.dedent( + """ + import builtins + import sys + import types + + from redis.commands.search.index_definition import IndexDefinition, IndexType + + legacy = types.ModuleType("redis.commands.search.indexDefinition") + legacy.IndexDefinition = IndexDefinition + legacy.IndexType = IndexType + sys.modules[legacy.__name__] = legacy + original_import = builtins.__import__ + + def compatibility_import(name, *args, **kwargs): + if name == "redis.commands.search.index_definition": + raise ModuleNotFoundError(name=name) + return original_import(name, *args, **kwargs) + + builtins.__import__ = compatibility_import + from litellm.caching.valkey_semantic_cache import ValkeySemanticCache + + cache = ValkeySemanticCache( + similarity_threshold=0.8, + sync_client=object(), + async_client=object(), + ) + assert cache._index_definition().__class__ is IndexDefinition + print("ok") + """ + ) + result = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + env={**os.environ, "PYTHONPATH": _REPO_ROOT}, + ) + assert result.returncode == 0, result.stderr + assert "ok" in result.stdout From 5eb2f653802a0e59cb351043b5f2ef9f7e2bc517 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 4 Sep 2026 14:36:08 +0800 Subject: [PATCH 3/5] style: format valkey semantic cache --- litellm/caching/valkey_semantic_cache.py | 1 + 1 file changed, 1 insertion(+) diff --git a/litellm/caching/valkey_semantic_cache.py b/litellm/caching/valkey_semantic_cache.py index 4f6e1b9564f..0feb1d13634 100644 --- a/litellm/caching/valkey_semantic_cache.py +++ b/litellm/caching/valkey_semantic_cache.py @@ -24,6 +24,7 @@ from typing import Any, Final from redis import Redis from redis.asyncio import Redis as AsyncRedis from redis.commands.search.field import TagField, VectorField + try: # redis-py >= 5 (module renamed to snake_case) from redis.commands.search.index_definition import IndexDefinition, IndexType except ModuleNotFoundError: # redis-py 4.x (last version with camelCase name) From 3c35c46f96fc03f4395d8cb69397865fd45d4932 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 4 Sep 2026 14:47:14 +0800 Subject: [PATCH 4/5] test: isolate redis py4 import fallback --- tests/test_litellm/caching/test_valkey_semantic_cache.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/test_litellm/caching/test_valkey_semantic_cache.py b/tests/test_litellm/caching/test_valkey_semantic_cache.py index f128795039d..45564199467 100644 --- a/tests/test_litellm/caching/test_valkey_semantic_cache.py +++ b/tests/test_litellm/caching/test_valkey_semantic_cache.py @@ -543,12 +543,18 @@ def test_valkey_cache_supports_redis_py_4_index_module(): import sys import types - from redis.commands.search.index_definition import IndexDefinition, IndexType + class IndexDefinition: + def __init__(self, *args, **kwargs): + pass + + class IndexType: + pass legacy = types.ModuleType("redis.commands.search.indexDefinition") legacy.IndexDefinition = IndexDefinition legacy.IndexType = IndexType sys.modules[legacy.__name__] = legacy + original_import = builtins.__import__ def compatibility_import(name, *args, **kwargs): From 4bf186fcad8068f56045ea54a2f6893208e59aa1 Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Fri, 4 Sep 2026 14:47:37 +0800 Subject: [PATCH 5/5] test: model redis py4 index type --- tests/test_litellm/caching/test_valkey_semantic_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_litellm/caching/test_valkey_semantic_cache.py b/tests/test_litellm/caching/test_valkey_semantic_cache.py index 45564199467..6327ebfa294 100644 --- a/tests/test_litellm/caching/test_valkey_semantic_cache.py +++ b/tests/test_litellm/caching/test_valkey_semantic_cache.py @@ -548,7 +548,7 @@ def test_valkey_cache_supports_redis_py_4_index_module(): pass class IndexType: - pass + HASH = "HASH" legacy = types.ModuleType("redis.commands.search.indexDefinition") legacy.IndexDefinition = IndexDefinition