Skip to content

Commit e1cd469

Browse files
committed
fix(llm-router): add threshold validation and empty-tiers guard
- Validate threshold range (0, 2] in update_tier_threshold before assignment, matching the ModelTier Pydantic schema constraint. - Guard _get_tier_matches against empty tiers list to prevent ValueError from max() on empty sequence. Applied to both sync and async implementations.
1 parent e5facb6 commit e1cd469

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

redisvl/extensions/llm_router/router.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ def update_tier_threshold(self, tier_name: str, threshold: float):
347347
tier = self.get_tier(tier_name)
348348
if tier is None:
349349
raise ValueError(f"Tier {tier_name} not found")
350+
if not (0 < threshold <= 2):
351+
raise ValueError("Threshold must be in range (0, 2]")
350352
tier.distance_threshold = threshold
351353
self._update_router_config()
352354

@@ -390,6 +392,8 @@ def _get_tier_matches(
390392
max_k: int = 1,
391393
) -> List[LLMRouteMatch]:
392394
"""Get matching tiers for a vector."""
395+
if not self.tiers:
396+
return []
393397
distance_threshold = max(t.distance_threshold for t in self.tiers)
394398

395399
query = VectorRangeQuery(
@@ -1086,6 +1090,8 @@ async def update_tier_threshold(self, tier_name: str, threshold: float):
10861090
tier = self.get_tier(tier_name)
10871091
if tier is None:
10881092
raise ValueError(f"Tier {tier_name} not found")
1093+
if not (0 < threshold <= 2):
1094+
raise ValueError("Threshold must be in range (0, 2]")
10891095
tier.distance_threshold = threshold
10901096
await self._update_router_config()
10911097

@@ -1129,6 +1135,8 @@ async def _get_tier_matches(
11291135
max_k: int = 1,
11301136
) -> List[LLMRouteMatch]:
11311137
"""Get matching tiers for a vector (async)."""
1138+
if not self.tiers:
1139+
return []
11321140
distance_threshold = max(t.distance_threshold for t in self.tiers)
11331141

11341142
query = VectorRangeQuery(

0 commit comments

Comments
 (0)