Skip to content

Commit 8a70416

Browse files
committed
refactor(router): derive the async index from the sync one
Address review on #784: - _get_async_index reads the URL, connection kwargs, lib_name and client ownership from self._index instead of keeping copies on the router, which also makes the from_existing patch unnecessary. - aroute/aroute_many take everything after vector keyword-only, so a positional distance_threshold can't be read as an aggregation method. - Docstrings note that SSL, unix-socket, Sentinel and Cluster clients should pass redis_url for async routing, that vectorizers without a native aembed still block on embedding, and that adisconnect() must be called before switching event loops. - Replace the six async tests with one that blocks the sync embed and sync aggregate paths, so it fails if aroute takes either.
1 parent 54b6f0a commit 8a70416

2 files changed

Lines changed: 44 additions & 87 deletions

File tree

‎redisvl/extensions/router/semantic.py‎

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ class SemanticRouter(BaseModel):
5353

5454
_index: SearchIndex = PrivateAttr()
5555
_aindex: AsyncSearchIndex | None = PrivateAttr(default=None)
56-
# Kept so the async index can reach the same Redis as the sync one.
57-
_redis_client_provided: bool = PrivateAttr(default=False)
58-
_redis_url: str = PrivateAttr(default="redis://localhost:6379")
59-
_connection_kwargs: dict[str, Any] | None = PrivateAttr(default=None)
60-
_lib_name: str | None = PrivateAttr(default=None)
6156
# A private attribute, never a field: it describes this instance's
6257
# relationship to the index, and must not reach the stored route config.
6358
_create_index: bool = PrivateAttr(default=True)
@@ -145,10 +140,6 @@ def __init__(
145140
)
146141

147142
self._create_index = create_index
148-
self._redis_client_provided = redis_client is not None
149-
self._redis_url = redis_url
150-
self._connection_kwargs = connection_kwargs or None
151-
self._lib_name = (index_kwargs or {}).get("lib_name")
152143
self._initialize_index(
153144
redis_client,
154145
redis_url,
@@ -228,7 +219,7 @@ def from_existing(
228219
)
229220
resolved_redis_url = redis_url if created_redis_client else None
230221
try:
231-
router = cls.from_dict(
222+
return cls.from_dict(
232223
router_dict,
233224
redis_url=resolved_redis_url,
234225
redis_client=redis_client,
@@ -241,11 +232,6 @@ def from_existing(
241232
if created_redis_client and redis_client is not None:
242233
redis_client.close()
243234
raise
244-
if created_redis_client:
245-
# The caller gave a URL, not a client: the async path should connect
246-
# from that URL too, rather than convert the client built here.
247-
router._redis_client_provided = False
248-
return router
249235

250236
@deprecated_argument("dtype")
251237
def _initialize_index(
@@ -567,22 +553,21 @@ async def _aget_route_matches(
567553
def _get_async_index(self) -> AsyncSearchIndex:
568554
"""Lazily construct the async search index over the same Redis index."""
569555
if self._aindex is None:
570-
if self._redis_client_provided:
571-
# Reuse the caller's connection settings rather than the URL default.
572-
async_client = RedisConnectionFactory.sync_to_async_redis(
573-
self._index._redis_client # type: ignore[arg-type]
574-
)
556+
index = self._index
557+
if index._owns_redis_client and index._redis_url:
575558
self._aindex = AsyncSearchIndex(
576-
schema=self._index.schema,
577-
redis_client=async_client,
578-
owns_client=True,
559+
schema=index.schema,
560+
redis_url=index._redis_url,
561+
connection_kwargs=index._connection_kwargs or None,
562+
lib_name=index._lib_name,
579563
)
580564
else:
581565
self._aindex = AsyncSearchIndex(
582-
schema=self._index.schema,
583-
redis_url=self._redis_url,
584-
connection_kwargs=self._connection_kwargs,
585-
lib_name=self._lib_name,
566+
schema=index.schema,
567+
redis_client=RedisConnectionFactory.sync_to_async_redis(
568+
index._redis_client # type: ignore[arg-type]
569+
),
570+
owns_client=True,
586571
)
587572
return self._aindex
588573

@@ -729,6 +714,7 @@ async def aroute(
729714
self,
730715
statement: str | None = None,
731716
vector: list[float] | None = None,
717+
*,
732718
aggregation_method: DistanceAggregationMethod | None = None,
733719
) -> RouteMatch:
734720
"""Async version of calling the router: match a statement or vector to
@@ -738,6 +724,14 @@ async def aroute(
738724
:class:`~redisvl.index.AsyncSearchIndex` over the same Redis index, so
739725
it returns the same match as ``router(statement)``.
740726
727+
Pass ``redis_url`` rather than a client to the router if you need
728+
async routing over SSL, a unix socket, Sentinel or Redis Cluster: those
729+
clients cannot be converted to their async form. With a vectorizer that
730+
has no native ``aembed``, such as the default ``HFTextVectorizer``,
731+
embedding still blocks the loop; only the Redis call is non-blocking.
732+
The async client is bound to the event loop that made the first call,
733+
so call :meth:`adisconnect` before switching loops.
734+
741735
Args:
742736
statement (Optional[str]): The input statement to be queried.
743737
vector (Optional[List[float]]): The input vector to be queried.
@@ -768,11 +762,13 @@ async def aroute_many(
768762
self,
769763
statement: str | None = None,
770764
vector: list[float] | None = None,
765+
*,
771766
max_k: int | None = None,
772767
aggregation_method: DistanceAggregationMethod | None = None,
773768
) -> list[RouteMatch]:
774769
"""Async version of :meth:`route_many`: return up to ``max_k`` matching
775-
routes without blocking the event loop.
770+
routes without blocking the event loop. See :meth:`aroute` for the
771+
connection and vectorizer caveats.
776772
777773
Args:
778774
statement (Optional[str]): The input statement to be queried.

‎tests/integration/test_semantic_router.py‎

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -819,71 +819,32 @@ def test_delete_route_references(semantic_router):
819819
assert [r["name"] for r in router_dict["routes"]] == ["farewell"]
820820

821821

822-
async def test_aroute_matches_sync(semantic_router):
822+
async def test_async_routing_matches_sync(semantic_router, monkeypatch):
823823
skip_if_redis_version_below(semantic_router._index.client, "7.0.0")
824-
825-
for statement in ["hello", "goodbye", "unknown_phrase"]:
826-
assert await semantic_router.aroute(statement) == semantic_router(statement)
827-
await semantic_router.adisconnect()
828-
829-
830-
async def test_aroute_vector(semantic_router):
831-
skip_if_redis_version_below(semantic_router._index.client, "7.0.0")
832-
824+
# Loose enough that both routes match, so the max_k default is observable.
825+
semantic_router.update_route_thresholds({"greeting": 2.0, "farewell": 2.0})
833826
vector = semantic_router.vectorizer.embed("goodbye")
834-
match = await semantic_router.aroute(vector=vector)
835-
assert match.name == "farewell"
836-
await semantic_router.adisconnect()
837-
838-
839-
async def test_aroute_many_matches_sync(semantic_router):
840-
skip_if_redis_version_below(semantic_router._index.client, "7.0.0")
841-
842-
matches = await semantic_router.aroute_many("hello", max_k=2)
843-
assert matches == semantic_router.route_many("hello", max_k=2)
844-
assert matches[0].name == "greeting"
845-
await semantic_router.adisconnect()
846-
847-
848-
async def test_aroute_requires_input(semantic_router):
849-
with pytest.raises(ValueError, match="Must provide a vector or statement"):
850-
await semantic_router.aroute()
851-
with pytest.raises(ValueError, match="Must provide a vector or statement"):
852-
await semantic_router.aroute_many()
853-
854-
855-
async def test_aroute_sees_routes_added_after_first_call(semantic_router):
856-
skip_if_redis_version_below(semantic_router._index.client, "7.0.0")
857-
858-
assert (await semantic_router.aroute("how is the weather today")).name is None
859-
semantic_router.add_route(
860-
Route(
861-
name="weather",
862-
references=["how is the weather today", "will it rain"],
863-
distance_threshold=0.3,
864-
)
865-
)
866-
assert (await semantic_router.aroute("how is the weather today")).name == "weather"
867-
await semantic_router.adisconnect()
868-
assert semantic_router.aindex is None
827+
inputs = [{"statement": s} for s in ("hello", "unknown_phrase")]
828+
inputs.append({"vector": vector})
829+
expected = [(semantic_router(**i), semantic_router.route_many(**i)) for i in inputs]
869830

831+
def blocking(*args, **kwargs):
832+
raise AssertionError("the async path made a blocking call")
870833

871-
async def test_aroute_with_redis_url(redis_url, routes, hf_vectorizer, redis_test_name):
872-
router = SemanticRouter(
873-
name=redis_test_name("test_router_async_url"),
874-
routes=routes,
875-
redis_url=redis_url,
876-
overwrite=True,
877-
vectorizer=hf_vectorizer,
878-
)
834+
# The async path must embed with aembed and query the async index.
835+
monkeypatch.setattr(type(semantic_router.vectorizer), "embed", blocking)
836+
monkeypatch.setattr(semantic_router._index, "aggregate", blocking)
879837
try:
880-
skip_if_no_redis_search(router._index.client)
881-
skip_if_redis_version_below(router._index.client, "7.0.0")
882-
assert (await router.aroute("hello")).name == "greeting"
883-
assert router.aindex is not None
838+
actual = [
839+
(await semantic_router.aroute(**i), await semantic_router.aroute_many(**i))
840+
for i in inputs
841+
]
842+
with pytest.raises(ValueError, match="Must provide a vector or statement"):
843+
await semantic_router.aroute()
884844
finally:
885-
await router.adisconnect()
886-
router.delete()
845+
await semantic_router.adisconnect()
846+
assert actual == expected
847+
assert semantic_router.aindex is None
887848

888849

889850
async def test_aroute_from_existing_url_connects_from_url(

0 commit comments

Comments
 (0)