Skip to content

Commit a56f9a1

Browse files
committed
perf: remove proactive module validation to reduce network calls (#370)
Remove MODULE LIST calls during connection initialization and index creation. Operations now fail naturally when Redis modules are missing, providing clear error messages at the point of failure. Changes: - Remove validate_modules() function and DEFAULT_REQUIRED_MODULES constant - Remove required_modules parameter from all connection methods - Remove proactive validation from SearchIndex and AsyncSearchIndex - Remove validation from SemanticRouter.from_existing() - Fix SemanticCache to use lazy client initialization - Remove skip_if_module_version_error test helper - Remove unused RedisModuleVersionError exception - Fix mocking strategy to patch instance methods instead of class methods
1 parent 42aa83b commit a56f9a1

File tree

13 files changed

+582
-258
lines changed

13 files changed

+582
-258
lines changed

redisvl/exceptions.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ class RedisVLError(Exception):
1111
pass
1212

1313

14-
class RedisModuleVersionError(RedisVLError):
15-
"""Error raised when required Redis modules are missing or have incompatible versions."""
16-
17-
pass
18-
19-
2014
class RedisSearchError(RedisVLError):
2115
"""Error raised for Redis Search specific operations."""
2216

redisvl/extensions/cache/llm/semantic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__(
162162
)
163163

164164
existing_index = SearchIndex.from_existing(
165-
name, redis_client=self._index.client
165+
name, redis_client=self._index._redis_client
166166
)
167167
if existing_index.schema.to_dict() != self._index.schema.to_dict():
168168
raise ValueError(

redisvl/extensions/router/semantic.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from redis.commands.search.aggregation import AggregateRequest, AggregateResult, Reducer
88
from redis.exceptions import ResponseError
99

10-
from redisvl.exceptions import RedisModuleVersionError
1110
from redisvl.extensions.constants import ROUTE_VECTOR_FIELD_NAME
1211
from redisvl.extensions.router.schema import (
1312
DistanceAggregationMethod,
@@ -118,18 +117,14 @@ def from_existing(
118117
**kwargs,
119118
) -> "SemanticRouter":
120119
"""Return SemanticRouter instance from existing index."""
121-
try:
122-
if redis_url:
123-
redis_client = RedisConnectionFactory.get_redis_connection(
124-
redis_url=redis_url,
125-
**kwargs,
126-
)
127-
elif redis_client:
128-
RedisConnectionFactory.validate_sync_redis(redis_client)
129-
except RedisModuleVersionError as e:
130-
raise RedisModuleVersionError(
131-
f"Loading from existing index failed. {str(e)}"
120+
if redis_url:
121+
redis_client = RedisConnectionFactory.get_redis_connection(
122+
redis_url=redis_url,
123+
**kwargs,
132124
)
125+
elif redis_client:
126+
# Just validate client type and set lib name
127+
RedisConnectionFactory.validate_sync_redis(redis_client)
133128
if redis_client is None:
134129
raise ValueError(
135130
"Creating Redis client failed. Please check the redis_url and connection_kwargs."

redisvl/index/index.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767

6868
from redisvl.exceptions import (
6969
QueryValidationError,
70-
RedisModuleVersionError,
7170
RedisSearchError,
7271
RedisVLError,
7372
SchemaValidationError,
@@ -439,23 +438,15 @@ def from_existing(
439438
440439
Raises:
441440
ValueError: If redis_url or redis_client is not provided.
442-
RedisModuleVersionError: If required Redis modules are not installed.
443441
"""
444-
try:
445-
if redis_url:
446-
redis_client = RedisConnectionFactory.get_redis_connection(
447-
redis_url=redis_url,
448-
required_modules=REQUIRED_MODULES_FOR_INTROSPECTION,
449-
**kwargs,
450-
)
451-
elif redis_client:
452-
RedisConnectionFactory.validate_sync_redis(
453-
redis_client, required_modules=REQUIRED_MODULES_FOR_INTROSPECTION
454-
)
455-
except RedisModuleVersionError as e:
456-
raise RedisModuleVersionError(
457-
f"Loading from existing index failed. {str(e)}"
442+
if redis_url:
443+
redis_client = RedisConnectionFactory.get_redis_connection(
444+
redis_url=redis_url,
445+
**kwargs,
458446
)
447+
elif redis_client:
448+
# Validate client type and set lib name
449+
RedisConnectionFactory.validate_sync_redis(redis_client)
459450

460451
if not redis_client:
461452
raise ValueError("Must provide either a redis_url or redis_client")
@@ -481,11 +472,16 @@ def _redis_client(self) -> SyncRedisClient:
481472
if self.__redis_client is None:
482473
with self._lock:
483474
if self.__redis_client is None:
475+
# Pass lib_name to connection factory
476+
kwargs = {**self._connection_kwargs}
477+
if self._lib_name:
478+
kwargs["lib_name"] = self._lib_name
484479
self.__redis_client = RedisConnectionFactory.get_redis_connection(
485480
redis_url=self._redis_url,
486-
**self._connection_kwargs,
481+
**kwargs,
487482
)
488-
if not self._validated_client:
483+
if not self._validated_client and self._lib_name:
484+
# Only set lib name for user-provided clients
489485
RedisConnectionFactory.validate_sync_redis(
490486
self.__redis_client,
491487
self._lib_name,
@@ -1193,21 +1189,14 @@ async def from_existing(
11931189
"Must provide either a redis_url or redis_client to fetch Redis index info."
11941190
)
11951191

1196-
try:
1197-
if redis_url:
1198-
redis_client = await RedisConnectionFactory._get_aredis_connection(
1199-
url=redis_url,
1200-
required_modules=REQUIRED_MODULES_FOR_INTROSPECTION,
1201-
**kwargs,
1202-
)
1203-
elif redis_client:
1204-
await RedisConnectionFactory.validate_async_redis(
1205-
redis_client, required_modules=REQUIRED_MODULES_FOR_INTROSPECTION
1206-
)
1207-
except RedisModuleVersionError as e:
1208-
raise RedisModuleVersionError(
1209-
f"Loading from existing index failed. {str(e)}"
1210-
) from e
1192+
if redis_url:
1193+
redis_client = await RedisConnectionFactory._get_aredis_connection(
1194+
url=redis_url,
1195+
**kwargs,
1196+
)
1197+
elif redis_client:
1198+
# Validate client type and set lib name
1199+
await RedisConnectionFactory.validate_async_redis(redis_client)
12111200

12121201
if redis_client is None:
12131202
raise ValueError(
@@ -1256,13 +1245,17 @@ async def _get_client(self) -> AsyncRedisClient:
12561245
async with self._lock:
12571246
# Double-check to protect against concurrent access
12581247
if self._redis_client is None:
1259-
kwargs = self._connection_kwargs
1248+
# Pass lib_name to connection factory
1249+
kwargs = {**self._connection_kwargs}
12601250
if self._redis_url:
12611251
kwargs["url"] = self._redis_url
1252+
if self._lib_name:
1253+
kwargs["lib_name"] = self._lib_name
12621254
self._redis_client = (
12631255
await RedisConnectionFactory._get_aredis_connection(**kwargs)
12641256
)
1265-
if not self._validated_client:
1257+
if not self._validated_client and self._lib_name:
1258+
# Set lib name for user-provided clients
12661259
await RedisConnectionFactory.validate_async_redis(
12671260
self._redis_client,
12681261
self._lib_name,

0 commit comments

Comments
 (0)