1+ import warnings
12from enum import Enum
2- from typing import Dict , List , Optional
3+ from typing import Any , Dict , List , Optional
34
4- from pydantic import BaseModel , Field , field_validator
5+ from pydantic import BaseModel , ConfigDict , Field , field_validator , model_validator
6+ from typing_extensions import Annotated
57
68from redisvl .extensions .constants import ROUTE_VECTOR_FIELD_NAME
79from redisvl .schema import IndexSchema
@@ -16,7 +18,7 @@ class Route(BaseModel):
1618 """List of reference phrases for the route."""
1719 metadata : Dict [str , str ] = Field (default = {})
1820 """Metadata associated with the route."""
19- distance_threshold : float = Field (default = 0.5 )
21+ distance_threshold : Annotated [ float , Field (strict = True , default = 0.5 , gt = 0 , le = 1 )]
2022 """Distance threshold for matching the route."""
2123
2224 @field_validator ("name" )
@@ -35,13 +37,6 @@ def references_must_not_be_empty(cls, v):
3537 raise ValueError ("All references must be non-empty strings" )
3638 return v
3739
38- @field_validator ("distance_threshold" )
39- @classmethod
40- def distance_threshold_must_be_positive (cls , v ):
41- if v is not None and v <= 0 :
42- raise ValueError ("Route distance threshold must be greater than zero" )
43- return v
44-
4540
4641class RouteMatch (BaseModel ):
4742 """Model representing a matched route with distance information."""
@@ -66,28 +61,26 @@ class DistanceAggregationMethod(Enum):
6661class RoutingConfig (BaseModel ):
6762 """Configuration for routing behavior."""
6863
69- # distance_threshold: float = Field(default=0.5)
70- """The threshold for semantic distance."""
71- max_k : int = Field (default = 1 )
72-
64+ """The maximum number of top matches to return."""
65+ max_k : Annotated [int , Field (strict = True , default = 1 , gt = 0 )] = 1
7366 """Aggregation method to use to classify queries."""
7467 aggregation_method : DistanceAggregationMethod = Field (
7568 default = DistanceAggregationMethod .avg
7669 )
7770
78- """The maximum number of top matches to return."""
79- distance_threshold : float = Field (
80- default = 0.5 ,
81- deprecated = True ,
82- description = "Global distance threshold is deprecated all distance_thresholds now apply at route level." ,
83- )
71+ model_config = ConfigDict (extra = "ignore" )
8472
85- @field_validator ( "max_k " )
73+ @model_validator ( mode = "before " )
8674 @classmethod
87- def max_k_must_be_positive (cls , v ):
88- if v <= 0 :
89- raise ValueError ("max_k must be a positive integer" )
90- return v
75+ def remove_distance_threshold (cls , values : Dict [str , Any ]) -> Dict [str , Any ]:
76+ if "distance_threshold" in values :
77+ warnings .warn (
78+ "The 'distance_threshold' field is deprecated and will be ignored. Set distance_threshold per Route." ,
79+ DeprecationWarning ,
80+ stacklevel = 2 ,
81+ )
82+ values .pop ("distance_threshold" )
83+ return values
9184
9285
9386class SemanticRouterIndexSchema (IndexSchema ):
0 commit comments