40
40
BaseModel ,
41
41
Field ,
42
42
model_validator ,
43
- root_validator ,
44
- validator ,
45
43
ConfigDict ,
46
- field_serializer
44
+ field_serializer ,
45
+ field_validator ,
46
+ ValidationInfo
47
47
)
48
48
49
49
logger = logging .getLogger (__name__ )
@@ -284,7 +284,7 @@ class ToolCreate(BaseModelWithConfigDict):
284
284
auth : Optional [AuthenticationValues ] = Field (None , description = "Authentication credentials (Basic or Bearer Token or custom headers) if required" )
285
285
gateway_id : Optional [str ] = Field (None , description = "id of gateway for the tool" )
286
286
287
- @root_validator ( pre = True )
287
+ @model_validator ( mode = "before" )
288
288
def assemble_auth (cls , values : Dict [str , Any ]) -> Dict [str , Any ]:
289
289
"""
290
290
Assemble authentication information from separate keys if provided.
@@ -343,7 +343,7 @@ class ToolUpdate(BaseModelWithConfigDict):
343
343
auth : Optional [AuthenticationValues ] = Field (None , description = "Authentication credentials (Basic or Bearer Token or custom headers) if required" )
344
344
gateway_id : Optional [str ] = Field (None , description = "id of gateway for the tool" )
345
345
346
- @root_validator ( pre = True )
346
+ @model_validator ( mode = "before" )
347
347
def assemble_auth (cls , values : Dict [str , Any ]) -> Dict [str , Any ]:
348
348
"""
349
349
Assemble authentication information from separate keys if provided.
@@ -546,9 +546,21 @@ class PromptArgument(BaseModelWithConfigDict):
546
546
description : Optional [str ] = Field (None , description = "Argument description" )
547
547
required : bool = Field (default = False , description = "Whether argument is required" )
548
548
549
+
549
550
model_config : ConfigDict = ConfigDict (
550
- ** BaseModelWithConfigDict .model_config , # carry over the base settings
551
- schema_extra = {"example" : {"name" : "language" , "description" : "Programming language" , "required" : True }}
551
+ ** {
552
+ # start with every key from the base
553
+ ** BaseModelWithConfigDict .model_config ,
554
+ # override only json_schema_extra by merging the two dicts:
555
+ "json_schema_extra" : {
556
+ ** BaseModelWithConfigDict .model_config .get ("json_schema_extra" , {}),
557
+ "example" : {
558
+ "name" : "language" ,
559
+ "description" : "Programming language" ,
560
+ "required" : True ,
561
+ },
562
+ },
563
+ }
552
564
)
553
565
554
566
@@ -641,9 +653,9 @@ class GatewayCreate(BaseModelWithConfigDict):
641
653
auth_header_value : Optional [str ] = Field (None , description = "Value for custom headers authentication" )
642
654
643
655
# Adding `auth_value` as an alias for better access post-validation
644
- auth_value : Optional [str ] = None
656
+ auth_value : Optional [str ] = Field ( None , validate_default = True )
645
657
646
- @validator ("url" , pre = True )
658
+ @field_validator ("url" , mode = "before" )
647
659
def ensure_url_scheme (cls , v : str ) -> str :
648
660
"""
649
661
Ensure URL has an http/https scheme.
@@ -659,31 +671,32 @@ def ensure_url_scheme(cls, v: str) -> str:
659
671
return f"http://{ v } "
660
672
return v
661
673
662
- @validator ("auth_value" , pre = True , always = True )
663
- def create_auth_value (cls , v , values ):
674
+ @field_validator ("auth_value" , mode = "before" )
675
+ def create_auth_value (cls , v , info ):
664
676
"""
665
- This validator will run before the model is fully instantiated (pre=True )
677
+ This validator will run before the model is fully instantiated (mode="before" )
666
678
It will process the auth fields based on auth_type and generate auth_value.
667
679
668
680
Args:
669
681
v: Input url
670
- values: Dict containing auth_type
682
+ info: ValidationInfo containing auth_type
671
683
672
684
Returns:
673
685
str: Auth value
674
686
"""
675
- auth_type = values .get ("auth_type" )
687
+ data = info .data
688
+ auth_type = data .get ("auth_type" )
676
689
677
690
if (auth_type is None ) or (auth_type == "" ):
678
691
return v # If no auth_type is provided, no need to create auth_value
679
692
680
693
# Process the auth fields and generate auth_value based on auth_type
681
- auth_value = cls ._process_auth_fields (values )
694
+ auth_value = cls ._process_auth_fields (info )
682
695
683
696
return auth_value
684
697
685
698
@staticmethod
686
- def _process_auth_fields (values : Dict [ str , Any ] ) -> Optional [Dict [str , Any ]]:
699
+ def _process_auth_fields (info : ValidationInfo ) -> Optional [Dict [str , Any ]]:
687
700
"""
688
701
Processes the input authentication fields and returns the correct auth_value.
689
702
This method is called based on the selected auth_type.
@@ -697,12 +710,13 @@ def _process_auth_fields(values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
697
710
Raises:
698
711
ValueError: If auth_type is invalid
699
712
"""
700
- auth_type = values .get ("auth_type" )
713
+ data = info .data
714
+ auth_type = data .get ("auth_type" )
701
715
702
716
if auth_type == "basic" :
703
717
# For basic authentication, both username and password must be present
704
- username = values .get ("auth_username" )
705
- password = values .get ("auth_password" )
718
+ username = data .get ("auth_username" )
719
+ password = data .get ("auth_password" )
706
720
707
721
if not username or not password :
708
722
raise ValueError ("For 'basic' auth, both 'auth_username' and 'auth_password' must be provided." )
@@ -712,7 +726,7 @@ def _process_auth_fields(values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
712
726
713
727
if auth_type == "bearer" :
714
728
# For bearer authentication, only token is required
715
- token = values .get ("auth_token" )
729
+ token = data .get ("auth_token" )
716
730
717
731
if not token :
718
732
raise ValueError ("For 'bearer' auth, 'auth_token' must be provided." )
@@ -721,8 +735,8 @@ def _process_auth_fields(values: Dict[str, Any]) -> Optional[Dict[str, Any]]:
721
735
722
736
if auth_type == "authheaders" :
723
737
# For headers authentication, both key and value must be present
724
- header_key = values .get ("auth_header_key" )
725
- header_value = values .get ("auth_header_value" )
738
+ header_key = data .get ("auth_header_key" )
739
+ header_value = data .get ("auth_header_value" )
726
740
727
741
if not header_key or not header_value :
728
742
raise ValueError ("For 'headers' auth, both 'auth_header_key' and 'auth_header_value' must be provided." )
@@ -753,9 +767,9 @@ class GatewayUpdate(BaseModelWithConfigDict):
753
767
auth_header_value : Optional [str ] = Field (None , description = "vallue for custom headers authentication" )
754
768
755
769
# Adding `auth_value` as an alias for better access post-validation
756
- auth_value : Optional [str ] = None
770
+ auth_value : Optional [str ] = Field ( None , validate_default = True )
757
771
758
- @validator ("url" , pre = True )
772
+ @field_validator ("url" , mode = "before" )
759
773
def ensure_url_scheme (cls , v : Optional [str ]) -> Optional [str ]:
760
774
"""
761
775
Ensure URL has an http/https scheme.
@@ -770,10 +784,10 @@ def ensure_url_scheme(cls, v: Optional[str]) -> Optional[str]:
770
784
return f"http://{ v } "
771
785
return v
772
786
773
- @validator ("auth_value" , pre = True , always = True )
774
- def create_auth_value (cls , v , values ):
787
+ @field_validator ("auth_value" , mode = "before" )
788
+ def create_auth_value (cls , v , info ):
775
789
"""
776
- This validator will run before the model is fully instantiated (pre=True )
790
+ This validator will run before the model is fully instantiated (mode="before" )
777
791
It will process the auth fields based on auth_type and generate auth_value.
778
792
779
793
Args:
@@ -783,13 +797,14 @@ def create_auth_value(cls, v, values):
783
797
Returns:
784
798
str: Auth value or URL
785
799
"""
786
- auth_type = values .get ("auth_type" )
800
+ data = info .data
801
+ auth_type = data .get ("auth_type" )
787
802
788
803
if (auth_type is None ) or (auth_type == "" ):
789
804
return v # If no auth_type is provided, no need to create auth_value
790
805
791
806
# Process the auth fields and generate auth_value based on auth_type
792
- auth_value = cls ._process_auth_fields (values )
807
+ auth_value = cls ._process_auth_fields (info )
793
808
794
809
return auth_value
795
810
@@ -1029,7 +1044,7 @@ class AdminToolCreate(BaseModelWithConfigDict):
1029
1044
headers : Optional [str ] = None # JSON string
1030
1045
input_schema : Optional [str ] = None # JSON string
1031
1046
1032
- @validator ("headers" , "input_schema" )
1047
+ @field_validator ("headers" , "input_schema" )
1033
1048
def validate_json (cls , v : Optional [str ]) -> Optional [Dict [str , Any ]]:
1034
1049
"""
1035
1050
Validate and parse JSON string inputs.
@@ -1114,7 +1129,7 @@ class ServerCreate(BaseModelWithConfigDict):
1114
1129
associated_resources : Optional [List [str ]] = Field (None , description = "Comma-separated resource IDs" )
1115
1130
associated_prompts : Optional [List [str ]] = Field (None , description = "Comma-separated prompt IDs" )
1116
1131
1117
- @validator ("associated_tools" , "associated_resources" , "associated_prompts" , pre = True )
1132
+ @field_validator ("associated_tools" , "associated_resources" , "associated_prompts" , mode = "before" )
1118
1133
def split_comma_separated (cls , v ):
1119
1134
"""
1120
1135
Splits a comma-separated string into a list of strings if needed.
@@ -1143,7 +1158,7 @@ class ServerUpdate(BaseModelWithConfigDict):
1143
1158
associated_resources : Optional [List [str ]] = Field (None , description = "Comma-separated resource IDs" )
1144
1159
associated_prompts : Optional [List [str ]] = Field (None , description = "Comma-separated prompt IDs" )
1145
1160
1146
- @validator ("associated_tools" , "associated_resources" , "associated_prompts" , pre = True )
1161
+ @field_validator ("associated_tools" , "associated_resources" , "associated_prompts" , mode = "before" )
1147
1162
def split_comma_separated (cls , v ):
1148
1163
"""
1149
1164
Splits a comma-separated string into a list of strings if needed.
@@ -1182,7 +1197,7 @@ class ServerRead(BaseModelWithConfigDict):
1182
1197
associated_prompts : List [int ] = []
1183
1198
metrics : ServerMetrics
1184
1199
1185
- @root_validator ( pre = True )
1200
+ @model_validator ( mode = "before" )
1186
1201
def populate_associated_ids (cls , values ):
1187
1202
"""
1188
1203
Pre-validation method that converts associated objects to their 'id'.
0 commit comments