1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use serde::{Deserialize, Serialize};
/// Configuration for Claude's tool choice behavior.
///
/// This can be one of the following:
/// - "auto": Let the model decide if and when to use tools
/// - "any": Allow the model to use any available tool
/// - "tool": Force the model to use a specific named tool
/// - "none": Do not use any tools
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum ToolChoice {
/// Automatic tool choice
Auto {
/// Whether to disable parallel tool use.
///
/// Defaults to `false`. If set to `true`, the model will output at most one tool use.
#[serde(skip_serializing_if = "Option::is_none")]
disable_parallel_tool_use: Option<bool>,
},
/// Any tool choice
Any {
/// Whether to disable parallel tool use.
///
/// Defaults to `false`. If set to `true`, the model will output exactly one tool use.
#[serde(skip_serializing_if = "Option::is_none")]
disable_parallel_tool_use: Option<bool>,
},
/// Specific tool choice
Tool {
/// The name of the tool to use.
name: String,
/// Whether to disable parallel tool use.
///
/// Defaults to `false`. If set to `true`, the model will output exactly one tool use.
#[serde(skip_serializing_if = "Option::is_none")]
disable_parallel_tool_use: Option<bool>,
},
/// No tools
None,
}
impl ToolChoice {
/// Create a new `ToolChoice` with auto mode.
pub fn auto() -> Self {
Self::Auto { disable_parallel_tool_use: None }
}
/// Create a new `ToolChoice` with auto mode, specifying whether to disable parallel tool use.
pub fn auto_with_disable_parallel(disable: bool) -> Self {
Self::Auto { disable_parallel_tool_use: Some(disable) }
}
/// Create a new `ToolChoice` allowing any tool.
pub fn any() -> Self {
Self::Any { disable_parallel_tool_use: None }
}
/// Create a new `ToolChoice` allowing any tool, specifying whether to disable parallel tool use.
pub fn any_with_disable_parallel(disable: bool) -> Self {
Self::Any { disable_parallel_tool_use: Some(disable) }
}
/// Create a new `ToolChoice` with a specific named tool.
pub fn tool(name: impl Into<String>) -> Self {
Self::Tool { name: name.into(), disable_parallel_tool_use: None }
}
/// Create a new `ToolChoice` with a specific named tool, specifying whether to disable parallel tool use.
pub fn tool_with_disable_parallel(name: impl Into<String>, disable: bool) -> Self {
Self::Tool { name: name.into(), disable_parallel_tool_use: Some(disable) }
}
/// Create a new `ToolChoice` with no tools.
pub fn none() -> Self {
Self::None
}
}
impl Default for ToolChoice {
fn default() -> Self {
Self::auto()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, to_value};
#[test]
fn tool_choice_auto() {
let param = ToolChoice::auto();
let json = to_value(¶m).unwrap();
assert_eq!(
json,
json!({
"type": "auto"
})
);
}
#[test]
fn tool_choice_any() {
let param = ToolChoice::any();
let json = to_value(¶m).unwrap();
assert_eq!(
json,
json!({
"type": "any"
})
);
}
#[test]
fn tool_choice_tool() {
let param = ToolChoice::tool("my_tool");
let json = to_value(¶m).unwrap();
assert_eq!(
json,
json!({
"name": "my_tool",
"type": "tool"
})
);
}
#[test]
fn tool_choice_none() {
let param = ToolChoice::none();
let json = to_value(¶m).unwrap();
assert_eq!(
json,
json!({
"type": "none"
})
);
}
#[test]
fn tool_choice_auto_with_disable_parallel() {
let param = ToolChoice::auto_with_disable_parallel(true);
let json = to_value(¶m).unwrap();
assert_eq!(
json,
json!({
"type": "auto",
"disable_parallel_tool_use": true
})
);
}
#[test]
fn tool_choice_deserialization_auto() {
let json = json!({
"type": "auto",
"disable_parallel_tool_use": true
});
let param: ToolChoice = serde_json::from_value(json).unwrap();
match param {
ToolChoice::Auto { disable_parallel_tool_use } => {
assert_eq!(disable_parallel_tool_use, Some(true));
}
_ => panic!("Expected Auto variant"),
}
}
#[test]
fn tool_choice_deserialization_tool() {
let json = json!({
"name": "my_tool",
"type": "tool",
"disable_parallel_tool_use": true
});
let param: ToolChoice = serde_json::from_value(json).unwrap();
match param {
ToolChoice::Tool { name, disable_parallel_tool_use } => {
assert_eq!(name, "my_tool");
assert_eq!(disable_parallel_tool_use, Some(true));
}
_ => panic!("Expected Tool variant"),
}
}
}