From bcfbeadc3027f5411c3cf71c037f7d86973e79f4 Mon Sep 17 00:00:00 2001 From: sid sri Date: Sat, 12 Sep 2026 22:08:04 +0530 Subject: [PATCH] fix(caching): valkey-semantic index_definition import compat with redis-py >= 6 The module-level import of redis.commands.search.indexDefinition (camelCase) raises ModuleNotFoundError on redis-py >= 6.0, which renamed the module to index_definition. Any deployment enabling valkey-semantic caching crashes at startup on the official images (redis-py 8.x). Try the modern snake_case path first and fall back to the legacy camelCase module for redis-py 5.x, which only ships the old name. Add a subprocess regression test simulating the redis-py 5.x layout to guard the fallback branch. --- litellm/caching/valkey_semantic_cache.py | 8 +++- .../caching/test_valkey_semantic_cache.py | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/litellm/caching/valkey_semantic_cache.py b/litellm/caching/valkey_semantic_cache.py index 58b76d98d6d..1fd32744856 100644 --- a/litellm/caching/valkey_semantic_cache.py +++ b/litellm/caching/valkey_semantic_cache.py @@ -24,7 +24,13 @@ 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 >= 6.0 renamed the indexDefinition module to snake_case. + from redis.commands.search.index_definition import IndexDefinition, IndexType +except ModuleNotFoundError: + from redis.commands.search.indexDefinition import IndexDefinition, IndexType + from redis.commands.search.query import Query from litellm._logging import print_verbose diff --git a/tests/test_litellm/caching/test_valkey_semantic_cache.py b/tests/test_litellm/caching/test_valkey_semantic_cache.py index 749658784ac..f528d1f0271 100644 --- a/tests/test_litellm/caching/test_valkey_semantic_cache.py +++ b/tests/test_litellm/caching/test_valkey_semantic_cache.py @@ -534,3 +534,44 @@ def test_importing_caching_does_not_require_redis(): ) assert result.returncode == 0, result.stderr assert "ok" in result.stdout + + +def test_index_definition_import_falls_back_to_legacy_camelcase_module(): + # redis-py >= 6.0 renamed redis/commands/search/indexDefinition.py to + # snake_case index_definition.py. valkey_semantic_cache.py imports it at + # module level. On modern redis-py the snake_case path must resolve; the + # fix also keeps a fallback for redis-py 5.x, which shipped only the + # camelCase module. That fallback cannot be exercised by swapping packages + # (redis.commands.search itself hard-imports index_definition), so this + # subprocess fully imports the module first, then simulates the redis-py + # 5.x layout (snake_case blocked + legacy camelCase injected) and forces a + # fresh module import to prove the fallback branch resolves it. + code = textwrap.dedent(""" + import importlib + import sys + import types + + import redis.commands.search + import litellm.caching.valkey_semantic_cache # primary snake_case path + + sys.modules["redis.commands.search.index_definition"] = None + legacy = types.ModuleType("redis.commands.search.indexDefinition") + legacy.IndexDefinition = object + legacy.IndexType = object + sys.modules["redis.commands.search.indexDefinition"] = legacy + sys.modules.pop("litellm.caching.valkey_semantic_cache", None) + + legacy_cache = importlib.import_module("litellm.caching.valkey_semantic_cache") + assert legacy_cache.IndexDefinition is legacy.IndexDefinition + assert legacy_cache.IndexType is legacy.IndexType + assert isinstance(legacy_cache.ValkeySemanticCache, type) + 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