From baa5e3feb0b56a442b918688b0ab46da6a3eb395 Mon Sep 17 00:00:00 2001
From: Siraj R Aizlewood <siraj@aurelio.ai>
Date: Thu, 25 Jan 2024 14:16:18 +0400
Subject: [PATCH] Fixed PyTest Fail

The test

TestRouteLayer::test_query_with_no_index

was failing because when it ran

assert route_layer("Anything").name is None

There were no Routes defined yet, and this meant that

route = [route for route in self.routes if route.name == top_class][0] was raising an error.

If no routes are defined then we can just defualt to RouteChoice().
---
 semantic_router/layer.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/semantic_router/layer.py b/semantic_router/layer.py
index 297f6a3f..5e99d386 100644
--- a/semantic_router/layer.py
+++ b/semantic_router/layer.py
@@ -186,7 +186,11 @@ class RouteLayer:
         results = self._query(text)
         top_class, top_class_scores = self._semantic_classify(results)
         # get chosen route object
-        route = [route for route in self.routes if route.name == top_class][0]
+        matching_routes = [route for route in self.routes if route.name == top_class]
+        if not matching_routes:
+            logger.error(f"No route found with name {top_class}. Check to see if any Routes have been defined.")
+            return RouteChoice()
+        route = matching_routes[0]
         threshold = (
             route.score_threshold
             if route.score_threshold is not None
-- 
GitLab