Skip to main content

openai_types/responses/
tools.rs

1// MANUAL — hand-maintained. py2rust sync will not overwrite.
2// Tool definitions for the Responses API.
3
4use serde::{Deserialize, Serialize};
5
6use crate::chat::WebSearchUserLocation;
7
8/// A function tool definition for the Responses API.
9///
10/// Maps to Python SDK `FunctionTool`. Standalone struct for typed tool definitions.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
13pub struct FunctionTool {
14    /// The name of the function.
15    pub name: String,
16    /// JSON Schema object describing the parameters.
17    #[serde(default)]
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub parameters: Option<serde_json::Value>,
20    /// Whether to enforce strict parameter validation.
21    #[serde(default)]
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub strict: Option<bool>,
24    /// A description of the function.
25    #[serde(default)]
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub description: Option<String>,
28}
29
30/// Ranking options for file search in the Responses API.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
33pub struct ResponseRankingOptions {
34    /// Score threshold (0.0-1.0).
35    #[serde(default)]
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub score_threshold: Option<f64>,
38    /// Ranker to use: "auto" or "default-2024-11-15".
39    #[serde(default)]
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub ranker: Option<String>,
42}
43
44/// Tool types for the Responses API.
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
47#[serde(tag = "type")]
48#[non_exhaustive]
49pub enum ResponseTool {
50    /// Function tool.
51    #[serde(rename = "function")]
52    Function {
53        /// Function name.
54        name: String,
55        /// A description of the function.
56        #[serde(default)]
57        #[serde(skip_serializing_if = "Option::is_none")]
58        description: Option<String>,
59        /// JSON Schema object describing the parameters.
60        #[serde(default)]
61        #[serde(skip_serializing_if = "Option::is_none")]
62        parameters: Option<serde_json::Value>,
63        /// Whether to enforce strict parameter validation.
64        #[serde(default)]
65        #[serde(skip_serializing_if = "Option::is_none")]
66        strict: Option<bool>,
67    },
68    /// Web search tool.
69    #[serde(rename = "web_search")]
70    WebSearch {
71        /// Search context size.
72        #[serde(default)]
73        #[serde(skip_serializing_if = "Option::is_none")]
74        search_context_size: Option<String>,
75        /// User location.
76        #[serde(default)]
77        #[serde(skip_serializing_if = "Option::is_none")]
78        user_location: Option<WebSearchUserLocation>,
79    },
80    /// File search tool.
81    #[serde(rename = "file_search")]
82    FileSearch {
83        /// Vector store IDs.
84        vector_store_ids: Vec<String>,
85        /// Max results.
86        #[serde(default)]
87        #[serde(skip_serializing_if = "Option::is_none")]
88        max_num_results: Option<i64>,
89        /// Ranking options.
90        #[serde(default)]
91        #[serde(skip_serializing_if = "Option::is_none")]
92        ranking_options: Option<ResponseRankingOptions>,
93    },
94    /// Code interpreter tool.
95    #[serde(rename = "code_interpreter")]
96    CodeInterpreter {
97        /// Container ID.
98        #[serde(default)]
99        #[serde(skip_serializing_if = "Option::is_none")]
100        container: Option<String>,
101    },
102    /// Computer use tool.
103    #[serde(rename = "computer")]
104    ComputerUse {},
105    /// MCP tool.
106    #[serde(rename = "mcp")]
107    Mcp {
108        /// Server label.
109        server_label: String,
110        /// Server URL.
111        #[serde(default)]
112        #[serde(skip_serializing_if = "Option::is_none")]
113        server_url: Option<String>,
114        /// Allowed tools.
115        #[serde(default)]
116        #[serde(skip_serializing_if = "Option::is_none")]
117        allowed_tools: Option<Vec<String>>,
118        /// Approval config — polymorphic ("never" | filter object), kept as Value.
119        #[serde(default)]
120        #[serde(skip_serializing_if = "Option::is_none")]
121        require_approval: Option<serde_json::Value>,
122    },
123    /// Image generation tool.
124    #[serde(rename = "image_generation")]
125    ImageGeneration {
126        /// Model to use for generation.
127        #[serde(default)]
128        #[serde(skip_serializing_if = "Option::is_none")]
129        model: Option<String>,
130        /// Image quality.
131        #[serde(default)]
132        #[serde(skip_serializing_if = "Option::is_none")]
133        quality: Option<String>,
134        /// Image size.
135        #[serde(default)]
136        #[serde(skip_serializing_if = "Option::is_none")]
137        size: Option<String>,
138    },
139}
140
141/// How the model selects tools in the Responses API.
142#[derive(Debug, Clone, Serialize, Deserialize)]
143#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
144#[serde(untagged)]
145#[non_exhaustive]
146pub enum ResponseToolChoice {
147    /// "none", "auto", or "required".
148    Mode(String),
149    /// Force a specific function by name.
150    Named {
151        /// Type — usually "function".
152        #[serde(rename = "type")]
153        type_: String,
154        /// The function to call.
155        function: ResponseToolChoiceFunction,
156    },
157}
158
159/// Specifies which function to call in tool choice.
160#[derive(Debug, Clone, Serialize, Deserialize)]
161#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
162pub struct ResponseToolChoiceFunction {
163    /// The function name.
164    pub name: String,
165}
166
167/// How the model selects tools — typed version.
168///
169/// Maps to Python SDK `ToolChoiceOptions` + `ToolChoiceFunction`.
170#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
171#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
172#[serde(untagged)]
173#[non_exhaustive]
174pub enum ToolChoiceParam {
175    /// Predefined mode: none, auto, or required.
176    Mode(ToolChoiceOptions),
177    /// Force a specific function by name.
178    Function(ToolChoiceFunction),
179}
180
181/// Predefined tool choice modes.
182#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
183#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
184#[serde(rename_all = "snake_case")]
185#[non_exhaustive]
186pub enum ToolChoiceOptions {
187    /// Do not use any tools.
188    None,
189    /// Let the model decide.
190    Auto,
191    /// Force tool use.
192    Required,
193}
194
195/// Force a specific function tool by name.
196#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
197#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
198pub struct ToolChoiceFunction {
199    /// The name of the function to call.
200    pub name: String,
201    /// The type — always "function".
202    #[serde(rename = "type")]
203    #[serde(default = "default_function_type")]
204    pub type_: String,
205}
206
207fn default_function_type() -> String {
208    "function".to_string()
209}