1use std::collections::{HashMap, HashSet};
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use validator::{Validate, ValidationError};
9
10use super::{
11 common::{
12 default_model, default_true, validate_stop, ChatLogProbs, Function, GenerationRequest,
13 PromptTokenUsageInfo, StringOrArray, ToolChoice, ToolChoiceValue, ToolReference, UsageInfo,
14 },
15 sampling_params::{validate_top_k_value, validate_top_p_value},
16};
17use crate::{builders::ResponsesResponseBuilder, validated::Normalizable};
18
19#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
24#[serde(tag = "type")]
25#[serde(rename_all = "snake_case")]
26pub enum ResponseTool {
27 #[serde(rename = "function")]
29 Function(FunctionTool),
30
31 #[serde(rename = "web_search_preview")]
33 WebSearchPreview(WebSearchPreviewTool),
34
35 #[serde(rename = "code_interpreter")]
37 CodeInterpreter(CodeInterpreterTool),
38
39 #[serde(rename = "mcp")]
41 Mcp(McpTool),
42}
43
44#[serde_with::skip_serializing_none]
45#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
46#[serde(deny_unknown_fields)]
47pub struct FunctionTool {
48 #[serde(flatten)]
50 pub function: Function,
51}
52
53#[serde_with::skip_serializing_none]
54#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
55#[serde(deny_unknown_fields)]
56pub struct McpTool {
57 pub server_url: Option<String>,
58 pub authorization: Option<String>,
59 pub headers: Option<HashMap<String, String>>,
61 pub server_label: String,
62 pub server_description: Option<String>,
63 pub require_approval: Option<RequireApproval>,
65 pub allowed_tools: Option<Vec<String>>,
66}
67
68#[serde_with::skip_serializing_none]
69#[derive(Debug, Clone, Deserialize, Serialize, Default, schemars::JsonSchema)]
70#[serde(deny_unknown_fields)]
71pub struct WebSearchPreviewTool {
72 pub search_context_size: Option<String>,
73 pub user_location: Option<Value>,
74}
75
76#[serde_with::skip_serializing_none]
77#[derive(Debug, Clone, Deserialize, Serialize, Default, schemars::JsonSchema)]
78#[serde(deny_unknown_fields)]
79pub struct CodeInterpreterTool {
80 pub container: Option<Value>,
81}
82
83#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, schemars::JsonSchema)]
85#[serde(rename_all = "snake_case")]
86pub enum RequireApproval {
87 Always,
88 Never,
89}
90
91#[serde_with::skip_serializing_none]
96#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
97pub struct ResponseReasoningParam {
98 #[serde(default = "default_reasoning_effort")]
99 pub effort: Option<ReasoningEffort>,
100 pub summary: Option<ReasoningSummary>,
101}
102
103#[expect(
104 clippy::unnecessary_wraps,
105 reason = "serde default function must match field type Option<T>"
106)]
107fn default_reasoning_effort() -> Option<ReasoningEffort> {
108 Some(ReasoningEffort::Medium)
109}
110
111#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
112#[serde(rename_all = "snake_case")]
113pub enum ReasoningEffort {
114 Minimal,
115 Low,
116 Medium,
117 High,
118}
119
120#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
121#[serde(rename_all = "snake_case")]
122pub enum ReasoningSummary {
123 Auto,
124 Concise,
125 Detailed,
126}
127
128#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
134#[serde(untagged)]
135pub enum StringOrContentParts {
136 String(String),
137 Array(Vec<ResponseContentPart>),
138}
139
140#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
141#[serde(tag = "type")]
142#[serde(rename_all = "snake_case")]
143pub enum ResponseInputOutputItem {
144 #[serde(rename = "message")]
145 Message {
146 id: String,
147 role: String,
148 content: Vec<ResponseContentPart>,
149 #[serde(skip_serializing_if = "Option::is_none")]
150 status: Option<String>,
151 },
152 #[serde(rename = "reasoning")]
153 Reasoning {
154 id: String,
155 summary: Vec<String>,
156 #[serde(skip_serializing_if = "Vec::is_empty")]
157 #[serde(default)]
158 content: Vec<ResponseReasoningContent>,
159 #[serde(skip_serializing_if = "Option::is_none")]
160 status: Option<String>,
161 },
162 #[serde(rename = "function_call")]
163 FunctionToolCall {
164 id: String,
165 call_id: String,
166 name: String,
167 arguments: String,
168 #[serde(skip_serializing_if = "Option::is_none")]
169 output: Option<String>,
170 #[serde(skip_serializing_if = "Option::is_none")]
171 status: Option<String>,
172 },
173 #[serde(rename = "function_call_output")]
174 FunctionCallOutput {
175 id: Option<String>,
176 call_id: String,
177 output: String,
178 #[serde(skip_serializing_if = "Option::is_none")]
179 status: Option<String>,
180 },
181 #[serde(rename = "mcp_approval_request")]
182 McpApprovalRequest {
183 id: String,
184 server_label: String,
185 name: String,
186 arguments: String,
187 },
188 #[serde(rename = "mcp_approval_response")]
189 McpApprovalResponse {
190 #[serde(skip_serializing_if = "Option::is_none")]
191 id: Option<String>,
192 approval_request_id: String,
193 approve: bool,
194 #[serde(skip_serializing_if = "Option::is_none")]
195 reason: Option<String>,
196 },
197 #[serde(untagged)]
198 SimpleInputMessage {
199 content: StringOrContentParts,
200 role: String,
201 #[serde(skip_serializing_if = "Option::is_none")]
202 #[serde(rename = "type")]
203 r#type: Option<String>,
204 },
205}
206
207#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
208#[serde(tag = "type")]
209#[serde(rename_all = "snake_case")]
210pub enum ResponseContentPart {
211 #[serde(rename = "output_text")]
212 OutputText {
213 text: String,
214 #[serde(default)]
215 #[serde(skip_serializing_if = "Vec::is_empty")]
216 annotations: Vec<String>,
217 #[serde(skip_serializing_if = "Option::is_none")]
218 logprobs: Option<ChatLogProbs>,
219 },
220 #[serde(rename = "input_text")]
221 InputText { text: String },
222 #[serde(other)]
223 Unknown,
224}
225
226#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
227#[serde(tag = "type")]
228#[serde(rename_all = "snake_case")]
229pub enum ResponseReasoningContent {
230 #[serde(rename = "reasoning_text")]
231 ReasoningText { text: String },
232}
233
234#[serde_with::skip_serializing_none]
236#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
237pub struct McpToolInfo {
238 pub name: String,
239 pub description: Option<String>,
240 pub input_schema: Value,
241 pub annotations: Option<Value>,
242}
243
244#[serde_with::skip_serializing_none]
245#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
246#[serde(tag = "type")]
247#[serde(rename_all = "snake_case")]
248pub enum ResponseOutputItem {
249 #[serde(rename = "message")]
250 Message {
251 id: String,
252 role: String,
253 content: Vec<ResponseContentPart>,
254 status: String,
255 },
256 #[serde(rename = "reasoning")]
257 Reasoning {
258 id: String,
259 summary: Vec<String>,
260 content: Vec<ResponseReasoningContent>,
261 status: Option<String>,
262 },
263 #[serde(rename = "function_call")]
264 FunctionToolCall {
265 id: String,
266 call_id: String,
267 name: String,
268 arguments: String,
269 output: Option<String>,
270 status: String,
271 },
272 #[serde(rename = "mcp_list_tools")]
273 McpListTools {
274 id: String,
275 server_label: String,
276 tools: Vec<McpToolInfo>,
277 },
278 #[serde(rename = "mcp_call")]
279 McpCall {
280 id: String,
281 status: String,
282 approval_request_id: Option<String>,
283 arguments: String,
284 error: Option<String>,
285 name: String,
286 output: String,
287 server_label: String,
288 },
289 #[serde(rename = "mcp_approval_request")]
290 McpApprovalRequest {
291 id: String,
292 server_label: String,
293 name: String,
294 arguments: String,
295 },
296 #[serde(rename = "web_search_call")]
297 WebSearchCall {
298 id: String,
299 status: WebSearchCallStatus,
300 action: WebSearchAction,
301 },
302 #[serde(rename = "code_interpreter_call")]
303 CodeInterpreterCall {
304 id: String,
305 status: CodeInterpreterCallStatus,
306 container_id: String,
307 code: Option<String>,
308 outputs: Option<Vec<CodeInterpreterOutput>>,
309 },
310 #[serde(rename = "file_search_call")]
311 FileSearchCall {
312 id: String,
313 status: FileSearchCallStatus,
314 queries: Vec<String>,
315 results: Option<Vec<FileSearchResult>>,
316 },
317}
318
319#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, schemars::JsonSchema)]
325#[serde(rename_all = "snake_case")]
326pub enum WebSearchCallStatus {
327 InProgress,
328 Searching,
329 Completed,
330 Failed,
331}
332
333#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
335#[serde(tag = "type", rename_all = "snake_case")]
336pub enum WebSearchAction {
337 Search {
338 #[serde(skip_serializing_if = "Option::is_none")]
339 query: Option<String>,
340 #[serde(default, skip_serializing_if = "Vec::is_empty")]
341 queries: Vec<String>,
342 #[serde(default, skip_serializing_if = "Vec::is_empty")]
343 sources: Vec<WebSearchSource>,
344 },
345 OpenPage {
346 url: String,
347 },
348 Find {
349 url: String,
350 pattern: String,
351 },
352}
353
354#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
356pub struct WebSearchSource {
357 #[serde(rename = "type")]
358 pub source_type: String,
359 pub url: String,
360}
361
362#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, schemars::JsonSchema)]
364#[serde(rename_all = "snake_case")]
365pub enum CodeInterpreterCallStatus {
366 InProgress,
367 Completed,
368 Incomplete,
369 Interpreting,
370 Failed,
371}
372
373#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
375#[serde(tag = "type", rename_all = "snake_case")]
376pub enum CodeInterpreterOutput {
377 Logs { logs: String },
378 Image { url: String },
379}
380
381#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, schemars::JsonSchema)]
383#[serde(rename_all = "snake_case")]
384pub enum FileSearchCallStatus {
385 InProgress,
386 Searching,
387 Completed,
388 Incomplete,
389 Failed,
390}
391
392#[serde_with::skip_serializing_none]
394#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
395pub struct FileSearchResult {
396 pub file_id: String,
397 pub filename: String,
398 pub text: Option<String>,
399 pub score: Option<f32>,
400 pub attributes: Option<Value>,
401}
402
403#[derive(Debug, Clone, Deserialize, Serialize, Default, schemars::JsonSchema)]
408#[serde(rename_all = "snake_case")]
409#[schemars(rename = "ResponsesServiceTier")]
410pub enum ServiceTier {
411 #[default]
412 Auto,
413 Default,
414 Flex,
415 Scale,
416 Priority,
417}
418
419#[derive(Debug, Clone, Deserialize, Serialize, Default, schemars::JsonSchema)]
420#[serde(rename_all = "snake_case")]
421pub enum Truncation {
422 Auto,
423 #[default]
424 Disabled,
425}
426
427#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, schemars::JsonSchema)]
428#[serde(rename_all = "snake_case")]
429pub enum ResponseStatus {
430 Queued,
431 InProgress,
432 Completed,
433 Failed,
434 Cancelled,
435}
436
437#[serde_with::skip_serializing_none]
438#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
439pub struct ReasoningInfo {
440 pub effort: Option<String>,
441 pub summary: Option<String>,
442}
443
444#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
450pub struct TextConfig {
451 #[serde(skip_serializing_if = "Option::is_none")]
452 pub format: Option<TextFormat>,
453}
454
455#[serde_with::skip_serializing_none]
457#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
458#[serde(tag = "type")]
459pub enum TextFormat {
460 #[serde(rename = "text")]
461 Text,
462
463 #[serde(rename = "json_object")]
464 JsonObject,
465
466 #[serde(rename = "json_schema")]
467 JsonSchema {
468 name: String,
469 schema: Value,
470 description: Option<String>,
471 strict: Option<bool>,
472 },
473}
474
475#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, schemars::JsonSchema)]
476#[serde(rename_all = "snake_case")]
477pub enum IncludeField {
478 #[serde(rename = "code_interpreter_call.outputs")]
479 CodeInterpreterCallOutputs,
480 #[serde(rename = "computer_call_output.output.image_url")]
481 ComputerCallOutputImageUrl,
482 #[serde(rename = "file_search_call.results")]
483 FileSearchCallResults,
484 #[serde(rename = "message.input_image.image_url")]
485 MessageInputImageUrl,
486 #[serde(rename = "message.output_text.logprobs")]
487 MessageOutputTextLogprobs,
488 #[serde(rename = "reasoning.encrypted_content")]
489 ReasoningEncryptedContent,
490}
491
492#[serde_with::skip_serializing_none]
498#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
499pub struct ResponseUsage {
500 pub input_tokens: u32,
501 pub output_tokens: u32,
502 pub total_tokens: u32,
503 pub input_tokens_details: Option<InputTokensDetails>,
504 pub output_tokens_details: Option<OutputTokensDetails>,
505}
506
507#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
508#[serde(untagged)]
509pub enum ResponsesUsage {
510 Classic(UsageInfo),
511 Modern(ResponseUsage),
512}
513
514#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
515pub struct InputTokensDetails {
516 pub cached_tokens: u32,
517}
518
519impl From<&PromptTokenUsageInfo> for InputTokensDetails {
520 fn from(d: &PromptTokenUsageInfo) -> Self {
521 Self {
522 cached_tokens: d.cached_tokens,
523 }
524 }
525}
526
527#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
528pub struct OutputTokensDetails {
529 pub reasoning_tokens: u32,
530}
531
532impl UsageInfo {
533 pub fn to_response_usage(&self) -> ResponseUsage {
535 ResponseUsage {
536 input_tokens: self.prompt_tokens,
537 output_tokens: self.completion_tokens,
538 total_tokens: self.total_tokens,
539 input_tokens_details: self
540 .prompt_tokens_details
541 .as_ref()
542 .map(InputTokensDetails::from),
543 output_tokens_details: self.reasoning_tokens.map(|tokens| OutputTokensDetails {
544 reasoning_tokens: tokens,
545 }),
546 }
547 }
548}
549
550impl From<UsageInfo> for ResponseUsage {
551 fn from(usage: UsageInfo) -> Self {
552 usage.to_response_usage()
553 }
554}
555
556impl ResponseUsage {
557 pub fn to_usage_info(&self) -> UsageInfo {
559 UsageInfo {
560 prompt_tokens: self.input_tokens,
561 completion_tokens: self.output_tokens,
562 total_tokens: self.total_tokens,
563 reasoning_tokens: self
564 .output_tokens_details
565 .as_ref()
566 .map(|details| details.reasoning_tokens),
567 prompt_tokens_details: self.input_tokens_details.as_ref().map(|details| {
568 PromptTokenUsageInfo {
569 cached_tokens: details.cached_tokens,
570 }
571 }),
572 }
573 }
574}
575
576#[derive(Debug, Clone, Default, Deserialize, Serialize, schemars::JsonSchema)]
577pub struct ResponsesGetParams {
578 #[serde(default)]
579 pub include: Vec<String>,
580 #[serde(default)]
581 pub include_obfuscation: Option<bool>,
582 #[serde(default)]
583 pub starting_after: Option<i64>,
584 #[serde(default)]
585 pub stream: Option<bool>,
586}
587
588impl ResponsesUsage {
589 pub fn to_response_usage(&self) -> ResponseUsage {
590 match self {
591 ResponsesUsage::Classic(usage) => usage.to_response_usage(),
592 ResponsesUsage::Modern(usage) => usage.clone(),
593 }
594 }
595
596 pub fn to_usage_info(&self) -> UsageInfo {
597 match self {
598 ResponsesUsage::Classic(usage) => usage.clone(),
599 ResponsesUsage::Modern(usage) => usage.to_usage_info(),
600 }
601 }
602}
603
604fn default_top_k() -> i32 {
609 -1
610}
611
612fn default_repetition_penalty() -> f32 {
613 1.0
614}
615
616#[expect(
617 clippy::unnecessary_wraps,
618 reason = "serde default function must match field type Option<T>"
619)]
620fn default_temperature() -> Option<f32> {
621 Some(1.0)
622}
623
624#[expect(
625 clippy::unnecessary_wraps,
626 reason = "serde default function must match field type Option<T>"
627)]
628fn default_top_p() -> Option<f32> {
629 Some(1.0)
630}
631
632#[derive(Debug, Clone, Deserialize, Serialize, Validate, schemars::JsonSchema)]
637#[validate(schema(function = "validate_responses_cross_parameters"))]
638pub struct ResponsesRequest {
639 #[serde(skip_serializing_if = "Option::is_none")]
641 pub background: Option<bool>,
642
643 #[serde(skip_serializing_if = "Option::is_none")]
645 pub include: Option<Vec<IncludeField>>,
646
647 #[validate(custom(function = "validate_response_input"))]
649 pub input: ResponseInput,
650
651 #[serde(skip_serializing_if = "Option::is_none")]
653 pub instructions: Option<String>,
654
655 #[serde(skip_serializing_if = "Option::is_none")]
657 #[validate(range(min = 1))]
658 pub max_output_tokens: Option<u32>,
659
660 #[serde(skip_serializing_if = "Option::is_none")]
662 #[validate(range(min = 1))]
663 pub max_tool_calls: Option<u32>,
664
665 #[serde(skip_serializing_if = "Option::is_none")]
667 pub metadata: Option<HashMap<String, Value>>,
668
669 #[serde(default = "default_model")]
671 pub model: String,
672
673 #[serde(skip_serializing_if = "Option::is_none")]
675 #[validate(custom(function = "validate_conversation_id"))]
676 pub conversation: Option<String>,
677
678 #[serde(skip_serializing_if = "Option::is_none")]
680 pub parallel_tool_calls: Option<bool>,
681
682 #[serde(skip_serializing_if = "Option::is_none")]
684 pub previous_response_id: Option<String>,
685
686 #[serde(skip_serializing_if = "Option::is_none")]
688 pub reasoning: Option<ResponseReasoningParam>,
689
690 #[serde(skip_serializing_if = "Option::is_none")]
692 pub service_tier: Option<ServiceTier>,
693
694 #[serde(skip_serializing_if = "Option::is_none")]
696 pub store: Option<bool>,
697
698 #[serde(default)]
700 pub stream: Option<bool>,
701
702 #[serde(
704 default = "default_temperature",
705 skip_serializing_if = "Option::is_none"
706 )]
707 #[validate(range(min = 0.0, max = 2.0))]
708 pub temperature: Option<f32>,
709
710 #[serde(skip_serializing_if = "Option::is_none")]
712 pub tool_choice: Option<ToolChoice>,
713
714 #[serde(skip_serializing_if = "Option::is_none")]
716 #[validate(custom(function = "validate_response_tools"))]
717 pub tools: Option<Vec<ResponseTool>>,
718
719 #[serde(skip_serializing_if = "Option::is_none")]
721 #[validate(range(min = 0, max = 20))]
722 pub top_logprobs: Option<u32>,
723
724 #[serde(default = "default_top_p", skip_serializing_if = "Option::is_none")]
726 #[validate(custom(function = "validate_top_p_value"))]
727 pub top_p: Option<f32>,
728
729 #[serde(skip_serializing_if = "Option::is_none")]
731 pub truncation: Option<Truncation>,
732
733 #[serde(skip_serializing_if = "Option::is_none")]
735 #[validate(custom(function = "validate_text_format"))]
736 pub text: Option<TextConfig>,
737
738 #[serde(skip_serializing_if = "Option::is_none")]
740 pub user: Option<String>,
741
742 #[serde(skip_serializing_if = "Option::is_none")]
744 pub request_id: Option<String>,
745
746 #[serde(default)]
748 pub priority: i32,
749
750 #[serde(skip_serializing_if = "Option::is_none")]
752 #[validate(range(min = -2.0, max = 2.0))]
753 pub frequency_penalty: Option<f32>,
754
755 #[serde(skip_serializing_if = "Option::is_none")]
757 #[validate(range(min = -2.0, max = 2.0))]
758 pub presence_penalty: Option<f32>,
759
760 #[serde(skip_serializing_if = "Option::is_none")]
762 #[validate(custom(function = "validate_stop"))]
763 pub stop: Option<StringOrArray>,
764
765 #[serde(default = "default_top_k")]
767 #[validate(custom(function = "validate_top_k_value"))]
768 pub top_k: i32,
769
770 #[serde(default)]
772 #[validate(range(min = 0.0, max = 1.0))]
773 pub min_p: f32,
774
775 #[serde(default = "default_repetition_penalty")]
777 #[validate(range(min = 0.0, max = 2.0))]
778 pub repetition_penalty: f32,
779}
780
781#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
782#[serde(untagged)]
783pub enum ResponseInput {
784 Items(Vec<ResponseInputOutputItem>),
785 Text(String),
786}
787
788impl Default for ResponsesRequest {
789 fn default() -> Self {
790 Self {
791 background: None,
792 include: None,
793 input: ResponseInput::Text(String::new()),
794 instructions: None,
795 max_output_tokens: None,
796 max_tool_calls: None,
797 metadata: None,
798 model: default_model(),
799 conversation: None,
800 parallel_tool_calls: None,
801 previous_response_id: None,
802 reasoning: None,
803 service_tier: None,
804 store: None,
805 stream: None,
806 temperature: None,
807 tool_choice: None,
808 tools: None,
809 top_logprobs: None,
810 top_p: None,
811 truncation: None,
812 text: None,
813 user: None,
814 request_id: None,
815 priority: 0,
816 frequency_penalty: None,
817 presence_penalty: None,
818 stop: None,
819 top_k: default_top_k(),
820 min_p: 0.0,
821 repetition_penalty: default_repetition_penalty(),
822 }
823 }
824}
825
826impl Normalizable for ResponsesRequest {
827 fn normalize(&mut self) {
832 if self.tool_choice.is_none() {
834 if let Some(tools) = &self.tools {
835 let choice_value = if tools.is_empty() {
836 ToolChoiceValue::None
837 } else {
838 ToolChoiceValue::Auto
839 };
840 self.tool_choice = Some(ToolChoice::Value(choice_value));
841 }
842 }
844
845 if self.parallel_tool_calls.is_none() && self.tools.is_some() {
847 self.parallel_tool_calls = Some(true);
848 }
849
850 if self.store.is_none() {
852 self.store = Some(true);
853 }
854 }
855}
856
857impl GenerationRequest for ResponsesRequest {
858 fn is_stream(&self) -> bool {
859 self.stream.unwrap_or(false)
860 }
861
862 fn get_model(&self) -> Option<&str> {
863 Some(self.model.as_str())
864 }
865
866 fn extract_text_for_routing(&self) -> String {
867 match &self.input {
868 ResponseInput::Text(text) => text.clone(),
869 ResponseInput::Items(items) => items
870 .iter()
871 .filter_map(|item| match item {
872 ResponseInputOutputItem::Message { content, .. } => {
873 let texts: Vec<String> = content
874 .iter()
875 .filter_map(|part| match part {
876 ResponseContentPart::OutputText { text, .. } => Some(text.clone()),
877 ResponseContentPart::InputText { text } => Some(text.clone()),
878 ResponseContentPart::Unknown => None,
879 })
880 .collect();
881 if texts.is_empty() {
882 None
883 } else {
884 Some(texts.join(" "))
885 }
886 }
887 ResponseInputOutputItem::SimpleInputMessage { content, .. } => {
888 match content {
889 StringOrContentParts::String(s) => Some(s.clone()),
890 StringOrContentParts::Array(parts) => {
891 let texts: Vec<String> = parts
893 .iter()
894 .filter_map(|part| match part {
895 ResponseContentPart::InputText { text } => {
896 Some(text.clone())
897 }
898 _ => None,
899 })
900 .collect();
901 if texts.is_empty() {
902 None
903 } else {
904 Some(texts.join(" "))
905 }
906 }
907 }
908 }
909 ResponseInputOutputItem::Reasoning { content, .. } => {
910 let texts: Vec<String> = content
911 .iter()
912 .map(|part| match part {
913 ResponseReasoningContent::ReasoningText { text } => text.clone(),
914 })
915 .collect();
916 if texts.is_empty() {
917 None
918 } else {
919 Some(texts.join(" "))
920 }
921 }
922 ResponseInputOutputItem::FunctionToolCall { arguments, .. } => {
923 Some(arguments.clone())
924 }
925 ResponseInputOutputItem::FunctionCallOutput { output, .. } => {
926 Some(output.clone())
927 }
928 ResponseInputOutputItem::McpApprovalRequest { .. } => None,
929 ResponseInputOutputItem::McpApprovalResponse { .. } => None,
930 })
931 .collect::<Vec<String>>()
932 .join(" "),
933 }
934 }
935}
936
937pub fn validate_conversation_id(conv_id: &str) -> Result<(), ValidationError> {
939 if !conv_id.starts_with("conv_") {
940 let mut error = ValidationError::new("invalid_conversation_id");
941 error.message = Some(std::borrow::Cow::Owned(format!(
942 "Invalid 'conversation': '{conv_id}'. Expected an ID that begins with 'conv_'."
943 )));
944 return Err(error);
945 }
946
947 let is_valid = conv_id
949 .chars()
950 .all(|c| c.is_alphanumeric() || c == '_' || c == '-');
951
952 if !is_valid {
953 let mut error = ValidationError::new("invalid_conversation_id");
954 error.message = Some(std::borrow::Cow::Owned(format!(
955 "Invalid 'conversation': '{conv_id}'. Expected an ID that contains letters, numbers, underscores, or dashes, but this value contained additional characters."
956 )));
957 return Err(error);
958 }
959 Ok(())
960}
961
962fn validate_tool_choice_with_tools(request: &ResponsesRequest) -> Result<(), ValidationError> {
964 let Some(tool_choice) = &request.tool_choice else {
965 return Ok(());
966 };
967
968 let has_tools = request.tools.as_ref().is_some_and(|t| !t.is_empty());
969 let is_some_choice = !matches!(tool_choice, ToolChoice::Value(ToolChoiceValue::None));
970
971 if is_some_choice && !has_tools {
973 let mut e = ValidationError::new("tool_choice_requires_tools");
974 e.message = Some("Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.".into());
975 return Err(e);
976 }
977
978 if !has_tools {
980 return Ok(());
981 }
982
983 let Some(tools) = request.tools.as_ref() else {
986 return Ok(());
987 };
988 let function_tool_names: Vec<&str> = tools
989 .iter()
990 .filter_map(|t| match t {
991 ResponseTool::Function(ft) => Some(ft.function.name.as_str()),
992 _ => None,
993 })
994 .collect();
995
996 match tool_choice {
998 ToolChoice::Function { function, .. } => {
999 if !function_tool_names.contains(&function.name.as_str()) {
1000 let mut e = ValidationError::new("tool_choice_function_not_found");
1001 e.message = Some(
1002 format!(
1003 "Invalid value for 'tool_choice': function '{}' not found in 'tools'.",
1004 function.name
1005 )
1006 .into(),
1007 );
1008 return Err(e);
1009 }
1010 }
1011 ToolChoice::AllowedTools {
1012 mode,
1013 tools: allowed_tools,
1014 ..
1015 } => {
1016 if mode != "auto" && mode != "required" {
1018 let mut e = ValidationError::new("tool_choice_invalid_mode");
1019 e.message = Some(
1020 format!(
1021 "Invalid value for 'tool_choice.mode': must be 'auto' or 'required', got '{mode}'."
1022 )
1023 .into(),
1024 );
1025 return Err(e);
1026 }
1027
1028 for tool_ref in allowed_tools {
1030 if let ToolReference::Function { name } = tool_ref {
1031 if !function_tool_names.contains(&name.as_str()) {
1032 let mut e = ValidationError::new("tool_choice_tool_not_found");
1033 e.message = Some(
1034 format!(
1035 "Invalid value for 'tool_choice.tools': tool '{name}' not found in 'tools'."
1036 )
1037 .into(),
1038 );
1039 return Err(e);
1040 }
1041 }
1042 }
1045 }
1046 ToolChoice::Value(_) => {}
1047 }
1048
1049 Ok(())
1050}
1051
1052fn validate_responses_cross_parameters(request: &ResponsesRequest) -> Result<(), ValidationError> {
1054 validate_tool_choice_with_tools(request)?;
1056
1057 if request.top_logprobs.is_some() {
1059 let has_logprobs_include = request
1060 .include
1061 .as_ref()
1062 .is_some_and(|inc| inc.contains(&IncludeField::MessageOutputTextLogprobs));
1063
1064 if !has_logprobs_include {
1065 let mut e = ValidationError::new("top_logprobs_requires_include");
1066 e.message = Some(
1067 "top_logprobs requires include field with 'message.output_text.logprobs'".into(),
1068 );
1069 return Err(e);
1070 }
1071 }
1072
1073 if request.background == Some(true) && request.stream == Some(true) {
1075 let mut e = ValidationError::new("background_conflicts_with_stream");
1076 e.message = Some("Cannot use background mode with streaming".into());
1077 return Err(e);
1078 }
1079
1080 if request.conversation.is_some() && request.previous_response_id.is_some() {
1082 let mut e = ValidationError::new("mutually_exclusive_parameters");
1083 e.message = Some("Mutually exclusive parameters. Ensure you are only providing one of: 'previous_response_id' or 'conversation'.".into());
1084 return Err(e);
1085 }
1086
1087 if let ResponseInput::Items(items) = &request.input {
1089 let has_valid_input = items.iter().any(|item| {
1091 matches!(
1092 item,
1093 ResponseInputOutputItem::Message { .. }
1094 | ResponseInputOutputItem::SimpleInputMessage { .. }
1095 )
1096 });
1097
1098 if !has_valid_input {
1099 let mut e = ValidationError::new("input_missing_user_message");
1100 e.message = Some("Input items must contain at least one message".into());
1101 return Err(e);
1102 }
1103 }
1104
1105 Ok(())
1110}
1111
1112fn validate_response_input(input: &ResponseInput) -> Result<(), ValidationError> {
1118 match input {
1119 ResponseInput::Text(text) => {
1120 if text.is_empty() {
1121 let mut e = ValidationError::new("input_text_empty");
1122 e.message = Some("Input text cannot be empty".into());
1123 return Err(e);
1124 }
1125 }
1126 ResponseInput::Items(items) => {
1127 if items.is_empty() {
1128 let mut e = ValidationError::new("input_items_empty");
1129 e.message = Some("Input items cannot be empty".into());
1130 return Err(e);
1131 }
1132 for item in items {
1134 validate_input_item(item)?;
1135 }
1136 }
1137 }
1138 Ok(())
1139}
1140
1141fn validate_input_item(item: &ResponseInputOutputItem) -> Result<(), ValidationError> {
1143 match item {
1144 ResponseInputOutputItem::Message { content, .. } => {
1145 if content.is_empty() {
1146 let mut e = ValidationError::new("message_content_empty");
1147 e.message = Some("Message content cannot be empty".into());
1148 return Err(e);
1149 }
1150 }
1151 ResponseInputOutputItem::SimpleInputMessage { content, .. } => match content {
1152 StringOrContentParts::String(s) if s.is_empty() => {
1153 let mut e = ValidationError::new("message_content_empty");
1154 e.message = Some("Message content cannot be empty".into());
1155 return Err(e);
1156 }
1157 StringOrContentParts::Array(parts) if parts.is_empty() => {
1158 let mut e = ValidationError::new("message_content_empty");
1159 e.message = Some("Message content parts cannot be empty".into());
1160 return Err(e);
1161 }
1162 _ => {}
1163 },
1164 ResponseInputOutputItem::Reasoning { .. } => {
1165 }
1167 ResponseInputOutputItem::FunctionCallOutput { output, .. } => {
1168 if output.is_empty() {
1169 let mut e = ValidationError::new("function_output_empty");
1170 e.message = Some("Function call output cannot be empty".into());
1171 return Err(e);
1172 }
1173 }
1174 ResponseInputOutputItem::FunctionToolCall { .. } => {}
1175 ResponseInputOutputItem::McpApprovalRequest { .. } => {}
1176 ResponseInputOutputItem::McpApprovalResponse { .. } => {}
1177 }
1178 Ok(())
1179}
1180
1181fn validate_response_tools(tools: &[ResponseTool]) -> Result<(), ValidationError> {
1183 let mut seen_mcp_labels: HashSet<String> = HashSet::new();
1185
1186 for (idx, tool) in tools.iter().enumerate() {
1187 if let ResponseTool::Mcp(mcp) = tool {
1188 let raw_label = mcp.server_label.as_str();
1189 if raw_label.is_empty() {
1190 let mut e = ValidationError::new("missing_required_parameter");
1191 e.message = Some(
1192 format!("Missing required parameter: 'tools[{idx}].server_label'.").into(),
1193 );
1194 return Err(e);
1195 }
1196
1197 let valid = raw_label.starts_with(|c: char| c.is_ascii_alphabetic())
1200 && raw_label
1201 .chars()
1202 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
1203 if !valid {
1204 let mut e = ValidationError::new("invalid_server_label");
1205 e.message = Some(
1206 format!(
1207 "Invalid input {raw_label}: 'server_label' must start with a letter and consist of only letters, digits, '-' and '_'"
1208 )
1209 .into(),
1210 );
1211 return Err(e);
1212 }
1213
1214 let normalized = raw_label.to_lowercase();
1215 if !seen_mcp_labels.insert(normalized) {
1216 let mut e = ValidationError::new("mcp_tool_duplicate_server_label");
1217 e.message = Some(
1218 format!("Duplicate MCP server_label '{raw_label}' found in 'tools' parameter.")
1219 .into(),
1220 );
1221 return Err(e);
1222 }
1223 }
1224 }
1225 Ok(())
1226}
1227
1228fn validate_text_format(text: &TextConfig) -> Result<(), ValidationError> {
1230 if let Some(TextFormat::JsonSchema { name, .. }) = &text.format {
1231 if name.is_empty() {
1232 let mut e = ValidationError::new("json_schema_name_empty");
1233 e.message = Some("JSON schema name cannot be empty".into());
1234 return Err(e);
1235 }
1236 }
1237 Ok(())
1238}
1239
1240pub fn normalize_input_item(item: &ResponseInputOutputItem) -> ResponseInputOutputItem {
1254 match item {
1255 ResponseInputOutputItem::SimpleInputMessage { content, role, .. } => {
1256 let content_vec = match content {
1257 StringOrContentParts::String(s) => {
1258 vec![ResponseContentPart::InputText { text: s.clone() }]
1259 }
1260 StringOrContentParts::Array(parts) => parts.clone(),
1261 };
1262
1263 ResponseInputOutputItem::Message {
1264 id: generate_id("msg"),
1265 role: role.clone(),
1266 content: content_vec,
1267 status: Some("completed".to_string()),
1268 }
1269 }
1270 _ => item.clone(),
1271 }
1272}
1273
1274pub fn generate_id(prefix: &str) -> String {
1275 use rand::RngCore;
1276 let mut rng = rand::rng();
1277 let mut bytes = [0u8; 25];
1279 rng.fill_bytes(&mut bytes);
1280 let hex_string: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
1281 format!("{prefix}_{hex_string}")
1282}
1283
1284#[serde_with::skip_serializing_none]
1285#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
1286pub struct ResponsesResponse {
1287 pub id: String,
1289
1290 #[serde(default = "default_object_type")]
1292 pub object: String,
1293
1294 pub created_at: i64,
1296
1297 pub status: ResponseStatus,
1299
1300 pub error: Option<Value>,
1302
1303 pub incomplete_details: Option<Value>,
1305
1306 pub instructions: Option<String>,
1308
1309 pub max_output_tokens: Option<u32>,
1311
1312 pub model: String,
1314
1315 #[serde(default)]
1317 pub output: Vec<ResponseOutputItem>,
1318
1319 #[serde(default = "default_true")]
1321 pub parallel_tool_calls: bool,
1322
1323 pub previous_response_id: Option<String>,
1325
1326 pub reasoning: Option<ReasoningInfo>,
1328
1329 #[serde(default = "default_true")]
1331 pub store: bool,
1332
1333 pub temperature: Option<f32>,
1335
1336 pub text: Option<TextConfig>,
1338
1339 #[serde(default = "default_tool_choice")]
1341 pub tool_choice: String,
1342
1343 #[serde(default)]
1345 pub tools: Vec<ResponseTool>,
1346
1347 pub top_p: Option<f32>,
1349
1350 pub truncation: Option<String>,
1352
1353 pub usage: Option<ResponsesUsage>,
1355
1356 pub user: Option<String>,
1358
1359 pub safety_identifier: Option<String>,
1361
1362 #[serde(default)]
1364 pub metadata: HashMap<String, Value>,
1365}
1366
1367fn default_object_type() -> String {
1368 "response".to_string()
1369}
1370
1371fn default_tool_choice() -> String {
1372 "auto".to_string()
1373}
1374
1375impl ResponsesResponse {
1376 pub fn builder(id: impl Into<String>, model: impl Into<String>) -> ResponsesResponseBuilder {
1378 ResponsesResponseBuilder::new(id, model)
1379 }
1380
1381 pub fn is_complete(&self) -> bool {
1383 matches!(self.status, ResponseStatus::Completed)
1384 }
1385
1386 pub fn is_in_progress(&self) -> bool {
1388 matches!(self.status, ResponseStatus::InProgress)
1389 }
1390
1391 pub fn is_failed(&self) -> bool {
1393 matches!(self.status, ResponseStatus::Failed)
1394 }
1395}
1396
1397impl ResponseOutputItem {
1398 pub fn new_message(
1400 id: String,
1401 role: String,
1402 content: Vec<ResponseContentPart>,
1403 status: String,
1404 ) -> Self {
1405 Self::Message {
1406 id,
1407 role,
1408 content,
1409 status,
1410 }
1411 }
1412
1413 pub fn new_reasoning(
1415 id: String,
1416 summary: Vec<String>,
1417 content: Vec<ResponseReasoningContent>,
1418 status: Option<String>,
1419 ) -> Self {
1420 Self::Reasoning {
1421 id,
1422 summary,
1423 content,
1424 status,
1425 }
1426 }
1427
1428 pub fn new_function_tool_call(
1430 id: String,
1431 call_id: String,
1432 name: String,
1433 arguments: String,
1434 output: Option<String>,
1435 status: String,
1436 ) -> Self {
1437 Self::FunctionToolCall {
1438 id,
1439 call_id,
1440 name,
1441 arguments,
1442 output,
1443 status,
1444 }
1445 }
1446}
1447
1448impl ResponseContentPart {
1449 pub fn new_text(
1451 text: String,
1452 annotations: Vec<String>,
1453 logprobs: Option<ChatLogProbs>,
1454 ) -> Self {
1455 Self::OutputText {
1456 text,
1457 annotations,
1458 logprobs,
1459 }
1460 }
1461}
1462
1463impl ResponseReasoningContent {
1464 pub fn new_reasoning_text(text: String) -> Self {
1466 Self::ReasoningText { text }
1467 }
1468}