diff --git a/semantic_router/routers/base.py b/semantic_router/routers/base.py index 556d0f51278b2ad518faa454ec6a41bcfc13e05c..5bff5f5d07e4c577d07f81f5a3996fddd5966db9 100644 --- a/semantic_router/routers/base.py +++ b/semantic_router/routers/base.py @@ -421,6 +421,8 @@ class BaseRouter(BaseModel): simulate_static: bool = False, route_filter: Optional[List[str]] = None, ) -> RouteChoice: + if self.index.index is None or self.routes is None: + raise ValueError("Index or routes are not populated.") # if no vector provided, encode text to get vector if vector is None: if text is None: @@ -477,6 +479,8 @@ class BaseRouter(BaseModel): simulate_static: bool = False, route_filter: Optional[List[str]] = None, ) -> RouteChoice: + if self.index.index is None or self.routes is None: + raise ValueError("Index or routes are not populated.") # if no vector provided, encode text to get vector if vector is None: if text is None: diff --git a/semantic_router/routers/hybrid.py b/semantic_router/routers/hybrid.py index e2f0a23ebd3bd7ddcbb7d17d38a2b437a5c072ff..3d810576855ffa791e8dcf1056053aebc489c7ed 100644 --- a/semantic_router/routers/hybrid.py +++ b/semantic_router/routers/hybrid.py @@ -218,6 +218,8 @@ class HybridRouter(BaseRouter): route_filter: Optional[List[str]] = None, sparse_vector: dict[int, float] | SparseEmbedding | None = None, ) -> RouteChoice: + if self.index.index is None or self.routes is None: + raise ValueError("Index or routes are not populated.") potential_sparse_vector: List[SparseEmbedding] | None = None # if no vector provided, encode text to get vector if vector is None: diff --git a/tests/unit/test_router.py b/tests/unit/test_router.py index cfb467dc0e8c37e47e7ea9f6c7768a0bbff7aea0..4d1378c5487733baf57cb07545678c3d8c6f283f 100644 --- a/tests/unit/test_router.py +++ b/tests/unit/test_router.py @@ -815,7 +815,8 @@ class TestSemanticRouter: encoder = encoder_cls() route_layer = router_cls(encoder=encoder) # TODO: probably should avoid running this with multiple encoders or find a way to set dims - assert route_layer(text="Anything").name is None + with pytest.raises(ValueError): + assert route_layer(text="Anything").name is None def test_query_with_vector(self, routes, index_cls, encoder_cls, router_cls): encoder = encoder_cls()