@@ -512,6 +512,8 @@ public interface StreamEvent {
512512 * return tool_use content blocks that represent the model's use of those tools. You
513513 * can then run those tools using the tool input generated by the model and then
514514 * optionally return results back to the model using tool_result content blocks.
515+ * @param toolChoice How the model should use the provided tools. The model can use a
516+ * specific tool, any available tool, decide by itself, or not use tools at all.
515517 * @param thinking Configuration for the model's thinking mode. When enabled, the
516518 * model can perform more in-depth reasoning before responding to a query.
517519 */
@@ -529,17 +531,19 @@ public record ChatCompletionRequest(
529531 @ JsonProperty ("top_p" ) Double topP ,
530532 @ JsonProperty ("top_k" ) Integer topK ,
531533 @ JsonProperty ("tools" ) List <Tool > tools ,
534+ @ JsonProperty ("tool_choice" ) ToolChoice toolChoice ,
532535 @ JsonProperty ("thinking" ) ThinkingConfig thinking ) {
533536 // @formatter:on
534537
535538 public ChatCompletionRequest (String model , List <AnthropicMessage > messages , Object system , Integer maxTokens ,
536539 Double temperature , Boolean stream ) {
537- this (model , messages , system , maxTokens , null , null , stream , temperature , null , null , null , null );
540+ this (model , messages , system , maxTokens , null , null , stream , temperature , null , null , null , null , null );
538541 }
539542
540543 public ChatCompletionRequest (String model , List <AnthropicMessage > messages , Object system , Integer maxTokens ,
541544 List <String > stopSequences , Double temperature , Boolean stream ) {
542- this (model , messages , system , maxTokens , null , stopSequences , stream , temperature , null , null , null , null );
545+ this (model , messages , system , maxTokens , null , stopSequences , stream , temperature , null , null , null , null ,
546+ null );
543547 }
544548
545549 public static ChatCompletionRequestBuilder builder () {
@@ -613,6 +617,8 @@ public static final class ChatCompletionRequestBuilder {
613617
614618 private List <Tool > tools ;
615619
620+ private ToolChoice toolChoice ;
621+
616622 private ChatCompletionRequest .ThinkingConfig thinking ;
617623
618624 private ChatCompletionRequestBuilder () {
@@ -630,6 +636,7 @@ private ChatCompletionRequestBuilder(ChatCompletionRequest request) {
630636 this .topP = request .topP ;
631637 this .topK = request .topK ;
632638 this .tools = request .tools ;
639+ this .toolChoice = request .toolChoice ;
633640 this .thinking = request .thinking ;
634641 }
635642
@@ -693,6 +700,11 @@ public ChatCompletionRequestBuilder tools(List<Tool> tools) {
693700 return this ;
694701 }
695702
703+ public ChatCompletionRequestBuilder toolChoice (ToolChoice toolChoice ) {
704+ this .toolChoice = toolChoice ;
705+ return this ;
706+ }
707+
696708 public ChatCompletionRequestBuilder thinking (ChatCompletionRequest .ThinkingConfig thinking ) {
697709 this .thinking = thinking ;
698710 return this ;
@@ -705,7 +717,8 @@ public ChatCompletionRequestBuilder thinking(ThinkingType type, Integer budgetTo
705717
706718 public ChatCompletionRequest build () {
707719 return new ChatCompletionRequest (this .model , this .messages , this .system , this .maxTokens , this .metadata ,
708- this .stopSequences , this .stream , this .temperature , this .topP , this .topK , this .tools , this .thinking );
720+ this .stopSequences , this .stream , this .temperature , this .topP , this .topK , this .tools ,
721+ this .toolChoice , this .thinking );
709722 }
710723
711724 }
@@ -1135,6 +1148,126 @@ public Tool(String name, String description, Map<String, Object> inputSchema) {
11351148
11361149 }
11371150
1151+ /**
1152+ * Base interface for tool choice options.
1153+ */
1154+ @ JsonTypeInfo (use = JsonTypeInfo .Id .NAME , include = JsonTypeInfo .As .EXISTING_PROPERTY , property = "type" ,
1155+ visible = true )
1156+ @ JsonSubTypes ({ @ JsonSubTypes .Type (value = ToolChoiceAuto .class , name = "auto" ),
1157+ @ JsonSubTypes .Type (value = ToolChoiceAny .class , name = "any" ),
1158+ @ JsonSubTypes .Type (value = ToolChoiceTool .class , name = "tool" ),
1159+ @ JsonSubTypes .Type (value = ToolChoiceNone .class , name = "none" ) })
1160+ public interface ToolChoice {
1161+
1162+ @ JsonProperty ("type" )
1163+ String type ();
1164+
1165+ }
1166+
1167+ /**
1168+ * Auto tool choice - the model will automatically decide whether to use tools.
1169+ *
1170+ * @param type The type of tool choice, always "auto".
1171+ * @param disableParallelToolUse Whether to disable parallel tool use. Defaults to
1172+ * false. If set to true, the model will output at most one tool use.
1173+ */
1174+ @ JsonInclude (Include .NON_NULL )
1175+ public record ToolChoiceAuto (@ JsonProperty ("type" ) String type ,
1176+ @ JsonProperty ("disable_parallel_tool_use" ) Boolean disableParallelToolUse ) implements ToolChoice {
1177+
1178+ /**
1179+ * Create an auto tool choice with default settings.
1180+ */
1181+ public ToolChoiceAuto () {
1182+ this ("auto" , null );
1183+ }
1184+
1185+ /**
1186+ * Create an auto tool choice with specific parallel tool use setting.
1187+ * @param disableParallelToolUse Whether to disable parallel tool use.
1188+ */
1189+ public ToolChoiceAuto (Boolean disableParallelToolUse ) {
1190+ this ("auto" , disableParallelToolUse );
1191+ }
1192+
1193+ }
1194+
1195+ /**
1196+ * Any tool choice - the model will use any available tools.
1197+ *
1198+ * @param type The type of tool choice, always "any".
1199+ * @param disableParallelToolUse Whether to disable parallel tool use. Defaults to
1200+ * false. If set to true, the model will output exactly one tool use.
1201+ */
1202+ @ JsonInclude (Include .NON_NULL )
1203+ public record ToolChoiceAny (@ JsonProperty ("type" ) String type ,
1204+ @ JsonProperty ("disable_parallel_tool_use" ) Boolean disableParallelToolUse ) implements ToolChoice {
1205+
1206+ /**
1207+ * Create an any tool choice with default settings.
1208+ */
1209+ public ToolChoiceAny () {
1210+ this ("any" , null );
1211+ }
1212+
1213+ /**
1214+ * Create an any tool choice with specific parallel tool use setting.
1215+ * @param disableParallelToolUse Whether to disable parallel tool use.
1216+ */
1217+ public ToolChoiceAny (Boolean disableParallelToolUse ) {
1218+ this ("any" , disableParallelToolUse );
1219+ }
1220+
1221+ }
1222+
1223+ /**
1224+ * Tool choice - the model will use the specified tool.
1225+ *
1226+ * @param type The type of tool choice, always "tool".
1227+ * @param name The name of the tool to use.
1228+ * @param disableParallelToolUse Whether to disable parallel tool use. Defaults to
1229+ * false. If set to true, the model will output exactly one tool use.
1230+ */
1231+ @ JsonInclude (Include .NON_NULL )
1232+ public record ToolChoiceTool (@ JsonProperty ("type" ) String type , @ JsonProperty ("name" ) String name ,
1233+ @ JsonProperty ("disable_parallel_tool_use" ) Boolean disableParallelToolUse ) implements ToolChoice {
1234+
1235+ /**
1236+ * Create a tool choice for a specific tool.
1237+ * @param name The name of the tool to use.
1238+ */
1239+ public ToolChoiceTool (String name ) {
1240+ this ("tool" , name , null );
1241+ }
1242+
1243+ /**
1244+ * Create a tool choice for a specific tool with parallel tool use setting.
1245+ * @param name The name of the tool to use.
1246+ * @param disableParallelToolUse Whether to disable parallel tool use.
1247+ */
1248+ public ToolChoiceTool (String name , Boolean disableParallelToolUse ) {
1249+ this ("tool" , name , disableParallelToolUse );
1250+ }
1251+
1252+ }
1253+
1254+ /**
1255+ * None tool choice - the model will not be allowed to use tools.
1256+ *
1257+ * @param type The type of tool choice, always "none".
1258+ */
1259+ @ JsonInclude (Include .NON_NULL )
1260+ public record ToolChoiceNone (@ JsonProperty ("type" ) String type ) implements ToolChoice {
1261+
1262+ /**
1263+ * Create a none tool choice.
1264+ */
1265+ public ToolChoiceNone () {
1266+ this ("none" );
1267+ }
1268+
1269+ }
1270+
11381271 // CB START EVENT
11391272
11401273 /**
0 commit comments