Skip to main content

openai_types/responses/
create.rs

1// MANUAL — hand-maintained. py2rust sync will not overwrite.
2// Request types for the Responses API.
3
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7use super::common::ReasoningEffort;
8use super::input::ResponseInput;
9use super::tools::{ResponseTool, ResponseToolChoice};
10
11/// Summary mode for reasoning output.
12pub use super::common::ReasoningSummary;
13
14/// Reasoning configuration for o-series models.
15#[derive(Debug, Clone, Default, Serialize, Deserialize)]
16#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
17pub struct Reasoning {
18    /// Effort level for reasoning.
19    #[serde(default)]
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub effort: Option<ReasoningEffort>,
22    /// Summary mode for reasoning output.
23    #[serde(default)]
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub summary: Option<ReasoningSummary>,
26}
27
28impl Reasoning {
29    /// Builder-style: set effort.
30    pub fn effort(&mut self, effort: ReasoningEffort) -> &mut Self {
31        self.effort = Some(effort);
32        self
33    }
34    /// Builder-style: set summary.
35    pub fn summary(&mut self, summary: ReasoningSummary) -> &mut Self {
36        self.summary = Some(summary);
37        self
38    }
39    /// Compat with async-openai's derive_builder pattern.
40    pub fn build(&self) -> Result<Self, String> {
41        Ok(self.clone())
42    }
43}
44
45/// Compat alias for async-openai builder pattern.
46pub type ReasoningArgs = Reasoning;
47
48/// Text output configuration.
49#[derive(Debug, Clone, Serialize, Deserialize)]
50#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
51pub struct ResponseTextConfig {
52    /// Format configuration (text, json_object, or json_schema).
53    #[serde(default)]
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub format: Option<ResponseTextFormat>,
56    /// Verbosity level for the response.
57    #[serde(default)]
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub verbosity: Option<String>,
60}
61
62/// Text output format for the Responses API.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
65#[serde(tag = "type")]
66#[non_exhaustive]
67pub enum ResponseTextFormat {
68    /// Plain text output.
69    #[serde(rename = "text")]
70    Text,
71    /// JSON object output.
72    #[serde(rename = "json_object")]
73    JsonObject,
74    /// JSON schema output with structured schema.
75    #[serde(rename = "json_schema")]
76    JsonSchema {
77        /// Schema name.
78        name: String,
79        /// Schema description.
80        #[serde(default)]
81        #[serde(skip_serializing_if = "Option::is_none")]
82        description: Option<String>,
83        /// JSON Schema definition.
84        #[serde(default)]
85        #[serde(skip_serializing_if = "Option::is_none")]
86        schema: Option<serde_json::Value>,
87        /// Whether to enforce strict schema validation.
88        #[serde(default)]
89        #[serde(skip_serializing_if = "Option::is_none")]
90        strict: Option<bool>,
91    },
92}
93
94/// Request body for `POST /responses`.
95#[derive(Debug, Clone, Default, Serialize)]
96#[cfg_attr(feature = "structured", derive(schemars::JsonSchema))]
97pub struct ResponseCreateRequest {
98    /// Model to use.
99    #[serde(default)]
100    pub model: String,
101
102    /// Input text or messages.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub input: Option<ResponseInput>,
105
106    /// System instructions.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    pub instructions: Option<String>,
109
110    /// Tools available to the model.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub tools: Option<Vec<ResponseTool>>,
113
114    /// How the model selects tools.
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub tool_choice: Option<ResponseToolChoice>,
117
118    /// Whether to enable parallel tool calls.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub parallel_tool_calls: Option<bool>,
121
122    /// Previous response ID for multi-turn.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub previous_response_id: Option<String>,
125
126    /// Temperature (0-2).
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub temperature: Option<f64>,
129
130    /// Nucleus sampling parameter.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub top_p: Option<f64>,
133
134    /// Max output tokens.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub max_output_tokens: Option<i64>,
137
138    /// Truncation strategy: "auto" or "disabled".
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub truncation: Option<String>,
141
142    /// Reasoning configuration for o-series models.
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub reasoning: Option<Reasoning>,
145
146    /// Store for evals/distillation.
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub store: Option<bool>,
149
150    /// Metadata key-value pairs.
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub metadata: Option<HashMap<String, String>>,
153
154    /// Additional data to include in response.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub include: Option<Vec<String>>,
157
158    /// Whether to stream.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub stream: Option<bool>,
161
162    /// Service tier.
163    #[serde(skip_serializing_if = "Option::is_none")]
164    pub service_tier: Option<String>,
165
166    /// End user identifier.
167    #[serde(skip_serializing_if = "Option::is_none")]
168    pub user: Option<String>,
169
170    /// Text output configuration.
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub text: Option<ResponseTextConfig>,
173
174    /// Prompt cache key — caches system prompt prefix server-side for faster repeat calls.
175    #[serde(skip_serializing_if = "Option::is_none")]
176    pub prompt_cache_key: Option<String>,
177
178    /// Prompt cache retention: "in-memory" or "24h" for extended caching.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    pub prompt_cache_retention: Option<String>,
181
182    /// Whether to run in background mode.
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub background: Option<bool>,
185}
186
187impl ResponseCreateRequest {
188    /// Create a new request with the given model.
189    pub fn new(model: impl Into<String>) -> Self {
190        Self {
191            model: model.into(),
192            ..Default::default()
193        }
194    }
195
196    /// Set the input text or messages.
197    pub fn input(mut self, input: impl Into<ResponseInput>) -> Self {
198        self.input = Some(input.into());
199        self
200    }
201
202    /// Set system instructions.
203    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
204        self.instructions = Some(instructions.into());
205        self
206    }
207
208    /// Set the tools.
209    pub fn tools(mut self, tools: Vec<ResponseTool>) -> Self {
210        self.tools = Some(tools);
211        self
212    }
213
214    /// Set how the model selects tools.
215    pub fn tool_choice(mut self, choice: ResponseToolChoice) -> Self {
216        self.tool_choice = Some(choice);
217        self
218    }
219
220    /// Set previous response ID for multi-turn.
221    pub fn previous_response_id(mut self, id: impl Into<String>) -> Self {
222        self.previous_response_id = Some(id.into());
223        self
224    }
225
226    /// Set the temperature (0-2).
227    pub fn temperature(mut self, temperature: f64) -> Self {
228        self.temperature = Some(temperature);
229        self
230    }
231
232    /// Set max output tokens.
233    pub fn max_output_tokens(mut self, max: i64) -> Self {
234        self.max_output_tokens = Some(max);
235        self
236    }
237
238    /// Set reasoning configuration.
239    pub fn reasoning(mut self, reasoning: Reasoning) -> Self {
240        self.reasoning = Some(reasoning);
241        self
242    }
243
244    /// Set truncation strategy.
245    pub fn truncation(mut self, truncation: impl Into<String>) -> Self {
246        self.truncation = Some(truncation.into());
247        self
248    }
249
250    /// Enable storage for evals/distillation.
251    pub fn store(mut self, store: bool) -> Self {
252        self.store = Some(store);
253        self
254    }
255
256    /// Set model.
257    pub fn model(mut self, model: impl Into<String>) -> Self {
258        self.model = model.into();
259        self
260    }
261
262    /// Set text output configuration (format + verbosity).
263    pub fn text(mut self, text: ResponseTextConfig) -> Self {
264        self.text = Some(text);
265        self
266    }
267
268    /// Set top_p (nucleus sampling).
269    pub fn top_p(mut self, top_p: f64) -> Self {
270        self.top_p = Some(top_p);
271        self
272    }
273
274    /// Enable or disable parallel tool calls.
275    pub fn parallel_tool_calls(mut self, parallel: bool) -> Self {
276        self.parallel_tool_calls = Some(parallel);
277        self
278    }
279
280    /// Set metadata key-value pairs.
281    pub fn metadata(mut self, metadata: HashMap<String, String>) -> Self {
282        self.metadata = Some(metadata);
283        self
284    }
285
286    /// Set include fields for additional response data.
287    pub fn include(mut self, include: Vec<String>) -> Self {
288        self.include = Some(include);
289        self
290    }
291
292    /// Set service tier ("auto", "default", "flex", "scale", "priority").
293    pub fn service_tier(mut self, tier: impl Into<String>) -> Self {
294        self.service_tier = Some(tier.into());
295        self
296    }
297
298    /// Set end user identifier.
299    pub fn user(mut self, user: impl Into<String>) -> Self {
300        self.user = Some(user.into());
301        self
302    }
303
304    /// Set prompt cache key for server-side system prompt caching.
305    pub fn prompt_cache_key(mut self, key: impl Into<String>) -> Self {
306        self.prompt_cache_key = Some(key.into());
307        self
308    }
309
310    /// Set prompt cache retention: "in-memory" or "24h".
311    pub fn prompt_cache_retention(mut self, retention: impl Into<String>) -> Self {
312        self.prompt_cache_retention = Some(retention.into());
313        self
314    }
315
316    /// Run response in background mode.
317    pub fn background(mut self, background: bool) -> Self {
318        self.background = Some(background);
319        self
320    }
321}
322
323// Compat aliases for async-openai migration (their names differ from OpenAPI spec).
324// Users switching from async-openai can use these to minimize code changes.
325
326/// Alias for [`ResponseCreateRequest`].
327pub type CreateResponse = ResponseCreateRequest;
328/// Alias for [`ResponseCreateRequest`].
329pub type CreateResponseArgs = ResponseCreateRequest;