@@ -472,6 +472,50 @@ public void create_withRecursiveParam_avoidsInfiniteRecursion() {
472472 assertThat (tool .declaration ().get ().parameters ()).hasValue (expectedParameters );
473473 }
474474
475+ @ Test
476+ public void create_withOptionalParameter_excludesFromRequired () {
477+ FunctionTool tool = FunctionTool .create (Functions .class , "functionWithOptionalParam" );
478+
479+ assertThat (tool ).isNotNull ();
480+ assertThat (tool .declaration ().get ().parameters ())
481+ .hasValue (
482+ Schema .builder ()
483+ .type ("OBJECT" )
484+ .properties (
485+ ImmutableMap .of (
486+ "requiredParam" ,
487+ Schema .builder ().type ("STRING" ).description ("A required parameter" ).build (),
488+ "optionalParam" ,
489+ Schema .builder ()
490+ .type ("INTEGER" )
491+ .description ("An optional parameter" )
492+ .build ()))
493+ .required (ImmutableList .of ("requiredParam" ))
494+ .build ());
495+ }
496+
497+ @ Test
498+ public void call_withOptionalParameter_missingValue () throws Exception {
499+ FunctionTool tool = FunctionTool .create (Functions .class , "functionWithOptionalParam" );
500+
501+ Map <String , Object > result =
502+ tool .runAsync (ImmutableMap .of ("requiredParam" , "test" ), null ).blockingGet ();
503+
504+ assertThat (result )
505+ .containsExactly (
506+ "requiredParam" , "test" , "optionalParam" , "null_value" , "wasOptionalProvided" , false );
507+ }
508+
509+ @ Test
510+ public void call_withOptionalParameter_missingRequired_returnsError () {
511+ FunctionTool tool = FunctionTool .create (Functions .class , "functionWithOptionalParam" );
512+
513+ Map <String , Object > result =
514+ tool .runAsync (ImmutableMap .of ("optionalParam" , "test" ), null ).blockingGet ();
515+
516+ assertThat (result ).containsExactly ("status" , "error" , "message" , "An internal error occurred." );
517+ }
518+
475519 @ Test
476520 public void create_withMaybeMapReturnType () {
477521 FunctionTool tool = FunctionTool .create (Functions .class , "returnsMaybeMap" );
@@ -719,6 +763,25 @@ public static ImmutableMap<String, Object> recursiveParam(Node param) {
719763 return ImmutableMap .of ("param" , param );
720764 }
721765
766+ public static ImmutableMap <String , Object > functionWithOptionalParam (
767+ @ Annotations .Schema (name = "requiredParam" , description = "A required parameter" )
768+ String requiredParam ,
769+ @ Annotations .Schema (
770+ name = "optionalParam" ,
771+ description = "An optional parameter" ,
772+ optional = true )
773+ Integer optionalParam ) {
774+ ImmutableMap .Builder <String , Object > builder = ImmutableMap .builder ();
775+ builder .put ("requiredParam" , requiredParam );
776+ if (optionalParam != null ) {
777+ builder .put ("optionalParam" , optionalParam );
778+ } else {
779+ builder .put ("optionalParam" , "null_value" );
780+ }
781+ builder .put ("wasOptionalProvided" , optionalParam != null );
782+ return builder .buildOrThrow ();
783+ }
784+
722785 public ImmutableMap <String , Object > nonStaticReturnAllSupportedParametersAsMap (
723786 String stringParam ,
724787 boolean primitiveBoolParam ,
0 commit comments