@@ -147,8 +147,15 @@ fn validate_no_unsupported_fields_with_ignore(
147147 anyhow:: bail!( "`cache_salt` must be a string" ) ;
148148 }
149149 if let Some ( value) = unsupported_fields. get ( "stop_token_ids" ) {
150- serde_json :: from_value :: < Vec < crate :: types:: TokenIdType > > ( value. clone ( ) )
150+ let token_ids : Vec < crate :: types:: TokenIdType > = serde_json :: from_value ( value. clone ( ) )
151151 . map_err ( |_| anyhow:: anyhow!( "`stop_token_ids` must be an array of token IDs" ) ) ?;
152+ if token_ids. len ( ) > MAX_STOP_SEQUENCES {
153+ return Err ( crate :: protocols:: common:: invalid_argument_error ( format ! (
154+ "Maximum of {} stop token IDs allowed, got {}" ,
155+ MAX_STOP_SEQUENCES ,
156+ token_ids. len( )
157+ ) ) ) ;
158+ }
152159 }
153160 if let Some ( value) = unsupported_fields. get ( "detokenize" )
154161 && !value. is_boolean ( )
@@ -445,11 +452,11 @@ pub fn validate_stop(stop: &Option<dynamo_protocols::types::Stop>) -> Result<(),
445452 anyhow:: bail!( "Stop sequences array cannot be empty" ) ;
446453 }
447454 if sequences. len ( ) > MAX_STOP_SEQUENCES {
448- anyhow :: bail !(
455+ return Err ( crate :: protocols :: common :: invalid_argument_error ( format ! (
449456 "Maximum of {} stop sequences allowed, got {}" ,
450457 MAX_STOP_SEQUENCES ,
451458 sequences. len( )
452- ) ;
459+ ) ) ) ;
453460 }
454461 for ( i, sequence) in sequences. iter ( ) . enumerate ( ) {
455462 if sequence. is_empty ( ) {
@@ -462,11 +469,11 @@ pub fn validate_stop(stop: &Option<dynamo_protocols::types::Stop>) -> Result<(),
462469 anyhow:: bail!( "Stop token IDs array cannot be empty" ) ;
463470 }
464471 if token_ids. len ( ) > MAX_STOP_SEQUENCES {
465- anyhow :: bail !(
472+ return Err ( crate :: protocols :: common :: invalid_argument_error ( format ! (
466473 "Maximum of {} stop token IDs allowed, got {}" ,
467474 MAX_STOP_SEQUENCES ,
468475 token_ids. len( )
469- ) ;
476+ ) ) ) ;
470477 }
471478 }
472479 }
@@ -1039,4 +1046,66 @@ mod tests {
10391046 . unwrap ( ) ;
10401047 validate_response_format ( & Some ( fmt) ) . unwrap ( ) ;
10411048 }
1049+
1050+ #[ test]
1051+ fn validate_stop_accepts_up_to_max_sequences ( ) {
1052+ let max_strings = Some ( dynamo_protocols:: types:: Stop :: StringArray (
1053+ ( 0 ..MAX_STOP_SEQUENCES ) . map ( |i| i. to_string ( ) ) . collect ( ) ,
1054+ ) ) ;
1055+ validate_stop ( & max_strings) . unwrap ( ) ;
1056+
1057+ let max_token_ids = Some ( dynamo_protocols:: types:: Stop :: TokenIdArray (
1058+ ( 0 ..MAX_STOP_SEQUENCES as u32 ) . collect ( ) ,
1059+ ) ) ;
1060+ validate_stop ( & max_token_ids) . unwrap ( ) ;
1061+ }
1062+
1063+ #[ test]
1064+ fn validate_stop_rejects_over_max_sequences ( ) {
1065+ let over_max_strings = Some ( dynamo_protocols:: types:: Stop :: StringArray (
1066+ ( 0 ..=MAX_STOP_SEQUENCES ) . map ( |i| i. to_string ( ) ) . collect ( ) ,
1067+ ) ) ;
1068+ let err = validate_stop ( & over_max_strings) . unwrap_err ( ) ;
1069+ assert_eq ! (
1070+ err. to_string( ) ,
1071+ format!(
1072+ "InvalidArgument: Maximum of {} stop sequences allowed, got {}" ,
1073+ MAX_STOP_SEQUENCES ,
1074+ MAX_STOP_SEQUENCES + 1
1075+ )
1076+ ) ;
1077+
1078+ let over_max_token_ids = Some ( dynamo_protocols:: types:: Stop :: TokenIdArray (
1079+ ( 0 ..=MAX_STOP_SEQUENCES as u32 ) . collect ( ) ,
1080+ ) ) ;
1081+ let err = validate_stop ( & over_max_token_ids) . unwrap_err ( ) ;
1082+ assert_eq ! (
1083+ err. to_string( ) ,
1084+ format!(
1085+ "InvalidArgument: Maximum of {} stop token IDs allowed, got {}" ,
1086+ MAX_STOP_SEQUENCES ,
1087+ MAX_STOP_SEQUENCES + 1
1088+ )
1089+ ) ;
1090+ }
1091+
1092+ #[ test]
1093+ fn validate_no_unsupported_fields_rejects_over_max_stop_token_ids ( ) {
1094+ let over_max: Vec < u32 > = ( 0 ..=MAX_STOP_SEQUENCES as u32 ) . collect ( ) ;
1095+ let unsupported_fields = HashMap :: from ( [ ( "stop_token_ids" . to_string ( ) , json ! ( over_max) ) ] ) ;
1096+
1097+ let err = validate_no_unsupported_fields ( & unsupported_fields) . unwrap_err ( ) ;
1098+ assert_eq ! (
1099+ err. to_string( ) ,
1100+ format!(
1101+ "InvalidArgument: Maximum of {} stop token IDs allowed, got {}" ,
1102+ MAX_STOP_SEQUENCES ,
1103+ MAX_STOP_SEQUENCES + 1
1104+ )
1105+ ) ;
1106+
1107+ let at_max: Vec < u32 > = ( 0 ..MAX_STOP_SEQUENCES as u32 ) . collect ( ) ;
1108+ let ok_fields = HashMap :: from ( [ ( "stop_token_ids" . to_string ( ) , json ! ( at_max) ) ] ) ;
1109+ validate_no_unsupported_fields ( & ok_fields) . unwrap ( ) ;
1110+ }
10421111}
0 commit comments