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_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
576impl ResponsesUsage {
577 pub fn to_response_usage(&self) -> ResponseUsage {
578 match self {
579 ResponsesUsage::Classic(usage) => usage.to_response_usage(),
580 ResponsesUsage::Modern(usage) => usage.clone(),
581 }
582 }
583
584 pub fn to_usage_info(&self) -> UsageInfo {
585 match self {
586 ResponsesUsage::Classic(usage) => usage.clone(),
587 ResponsesUsage::Modern(usage) => usage.to_usage_info(),
588 }
589 }
590}
591
592fn default_top_k() -> i32 {
597 -1
598}
599
600fn default_repetition_penalty() -> f32 {
601 1.0
602}
603
604#[expect(
605 clippy::unnecessary_wraps,
606 reason = "serde default function must match field type Option<T>"
607)]
608fn default_temperature() -> Option<f32> {
609 Some(1.0)
610}
611
612#[expect(
613 clippy::unnecessary_wraps,
614 reason = "serde default function must match field type Option<T>"
615)]
616fn default_top_p() -> Option<f32> {
617 Some(1.0)
618}
619
620#[derive(Debug, Clone, Deserialize, Serialize, Validate, schemars::JsonSchema)]
625#[validate(schema(function = "validate_responses_cross_parameters"))]
626pub struct ResponsesRequest {
627 #[serde(skip_serializing_if = "Option::is_none")]
629 pub background: Option<bool>,
630
631 #[serde(skip_serializing_if = "Option::is_none")]
633 pub include: Option<Vec<IncludeField>>,
634
635 #[validate(custom(function = "validate_response_input"))]
637 pub input: ResponseInput,
638
639 #[serde(skip_serializing_if = "Option::is_none")]
641 pub instructions: Option<String>,
642
643 #[serde(skip_serializing_if = "Option::is_none")]
645 #[validate(range(min = 1))]
646 pub max_output_tokens: Option<u32>,
647
648 #[serde(skip_serializing_if = "Option::is_none")]
650 #[validate(range(min = 1))]
651 pub max_tool_calls: Option<u32>,
652
653 #[serde(skip_serializing_if = "Option::is_none")]
655 pub metadata: Option<HashMap<String, Value>>,
656
657 pub model: String,
659
660 #[serde(skip_serializing_if = "Option::is_none")]
662 #[validate(custom(function = "validate_conversation_id"))]
663 pub conversation: Option<String>,
664
665 #[serde(skip_serializing_if = "Option::is_none")]
667 pub parallel_tool_calls: Option<bool>,
668
669 #[serde(skip_serializing_if = "Option::is_none")]
671 pub previous_response_id: Option<String>,
672
673 #[serde(skip_serializing_if = "Option::is_none")]
675 pub reasoning: Option<ResponseReasoningParam>,
676
677 #[serde(skip_serializing_if = "Option::is_none")]
679 pub service_tier: Option<ServiceTier>,
680
681 #[serde(skip_serializing_if = "Option::is_none")]
683 pub store: Option<bool>,
684
685 #[serde(default)]
687 pub stream: Option<bool>,
688
689 #[serde(
691 default = "default_temperature",
692 skip_serializing_if = "Option::is_none"
693 )]
694 #[validate(range(min = 0.0, max = 2.0))]
695 pub temperature: Option<f32>,
696
697 #[serde(skip_serializing_if = "Option::is_none")]
699 pub tool_choice: Option<ToolChoice>,
700
701 #[serde(skip_serializing_if = "Option::is_none")]
703 #[validate(custom(function = "validate_response_tools"))]
704 pub tools: Option<Vec<ResponseTool>>,
705
706 #[serde(skip_serializing_if = "Option::is_none")]
708 #[validate(range(min = 0, max = 20))]
709 pub top_logprobs: Option<u32>,
710
711 #[serde(default = "default_top_p", skip_serializing_if = "Option::is_none")]
713 #[validate(custom(function = "validate_top_p_value"))]
714 pub top_p: Option<f32>,
715
716 #[serde(skip_serializing_if = "Option::is_none")]
718 pub truncation: Option<Truncation>,
719
720 #[serde(skip_serializing_if = "Option::is_none")]
722 #[validate(custom(function = "validate_text_format"))]
723 pub text: Option<TextConfig>,
724
725 #[serde(skip_serializing_if = "Option::is_none")]
727 pub user: Option<String>,
728
729 #[serde(skip_serializing_if = "Option::is_none")]
731 pub request_id: Option<String>,
732
733 #[serde(default)]
735 pub priority: i32,
736
737 #[serde(skip_serializing_if = "Option::is_none")]
739 #[validate(range(min = -2.0, max = 2.0))]
740 pub frequency_penalty: Option<f32>,
741
742 #[serde(skip_serializing_if = "Option::is_none")]
744 #[validate(range(min = -2.0, max = 2.0))]
745 pub presence_penalty: Option<f32>,
746
747 #[serde(skip_serializing_if = "Option::is_none")]
749 #[validate(custom(function = "validate_stop"))]
750 pub stop: Option<StringOrArray>,
751
752 #[serde(default = "default_top_k")]
754 #[validate(custom(function = "validate_top_k_value"))]
755 pub top_k: i32,
756
757 #[serde(default)]
759 #[validate(range(min = 0.0, max = 1.0))]
760 pub min_p: f32,
761
762 #[serde(default = "default_repetition_penalty")]
764 #[validate(range(min = 0.0, max = 2.0))]
765 pub repetition_penalty: f32,
766}
767
768#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
769#[serde(untagged)]
770pub enum ResponseInput {
771 Items(Vec<ResponseInputOutputItem>),
772 Text(String),
773}
774
775impl Default for ResponsesRequest {
776 fn default() -> Self {
777 Self {
778 background: None,
779 include: None,
780 input: ResponseInput::Text(String::new()),
781 instructions: None,
782 max_output_tokens: None,
783 max_tool_calls: None,
784 metadata: None,
785 model: String::new(),
786 conversation: None,
787 parallel_tool_calls: None,
788 previous_response_id: None,
789 reasoning: None,
790 service_tier: None,
791 store: None,
792 stream: None,
793 temperature: None,
794 tool_choice: None,
795 tools: None,
796 top_logprobs: None,
797 top_p: None,
798 truncation: None,
799 text: None,
800 user: None,
801 request_id: None,
802 priority: 0,
803 frequency_penalty: None,
804 presence_penalty: None,
805 stop: None,
806 top_k: default_top_k(),
807 min_p: 0.0,
808 repetition_penalty: default_repetition_penalty(),
809 }
810 }
811}
812
813impl Normalizable for ResponsesRequest {
814 fn normalize(&mut self) {
819 if self.tool_choice.is_none() {
821 if let Some(tools) = &self.tools {
822 let choice_value = if tools.is_empty() {
823 ToolChoiceValue::None
824 } else {
825 ToolChoiceValue::Auto
826 };
827 self.tool_choice = Some(ToolChoice::Value(choice_value));
828 }
829 }
831
832 if self.parallel_tool_calls.is_none() && self.tools.is_some() {
834 self.parallel_tool_calls = Some(true);
835 }
836
837 if self.store.is_none() {
839 self.store = Some(true);
840 }
841 }
842}
843
844impl GenerationRequest for ResponsesRequest {
845 fn is_stream(&self) -> bool {
846 self.stream.unwrap_or(false)
847 }
848
849 fn get_model(&self) -> Option<&str> {
850 Some(self.model.as_str())
851 }
852
853 fn extract_text_for_routing(&self) -> String {
854 match &self.input {
855 ResponseInput::Text(text) => text.clone(),
856 ResponseInput::Items(items) => {
857 let mut result = String::with_capacity(256);
858 let mut has_parts = false;
859
860 let mut append_text = |text: &str| {
861 if has_parts {
862 result.push(' ');
863 }
864 has_parts = true;
865 result.push_str(text);
866 };
867
868 for item in items {
869 match item {
870 ResponseInputOutputItem::Message { content, .. } => {
871 for part in content {
872 let text = match part {
873 ResponseContentPart::OutputText { text, .. } => {
874 Some(text.as_str())
875 }
876 ResponseContentPart::InputText { text } => Some(text.as_str()),
877 ResponseContentPart::Unknown => None,
878 };
879 if let Some(t) = text {
880 append_text(t);
881 }
882 }
883 }
884 ResponseInputOutputItem::SimpleInputMessage { content, .. } => {
885 match content {
886 StringOrContentParts::String(s) => {
887 append_text(s.as_str());
888 }
889 StringOrContentParts::Array(parts) => {
890 for part in parts {
891 let text = match part {
892 ResponseContentPart::OutputText { text, .. } => {
893 Some(text.as_str())
894 }
895 ResponseContentPart::InputText { text } => {
896 Some(text.as_str())
897 }
898 ResponseContentPart::Unknown => None,
899 };
900 if let Some(t) = text {
901 append_text(t);
902 }
903 }
904 }
905 }
906 }
907 ResponseInputOutputItem::Reasoning { content, .. } => {
908 for part in content {
909 match part {
910 ResponseReasoningContent::ReasoningText { text } => {
911 append_text(text.as_str());
912 }
913 }
914 }
915 }
916 ResponseInputOutputItem::FunctionToolCall { .. }
917 | ResponseInputOutputItem::FunctionCallOutput { .. }
918 | ResponseInputOutputItem::McpApprovalRequest { .. }
919 | ResponseInputOutputItem::McpApprovalResponse { .. } => {}
920 }
921 }
922
923 result
924 }
925 }
926 }
927}
928
929pub fn validate_conversation_id(conv_id: &str) -> Result<(), ValidationError> {
931 if !conv_id.starts_with("conv_") {
932 let mut error = ValidationError::new("invalid_conversation_id");
933 error.message = Some(std::borrow::Cow::Owned(format!(
934 "Invalid 'conversation': '{conv_id}'. Expected an ID that begins with 'conv_'."
935 )));
936 return Err(error);
937 }
938
939 let is_valid = conv_id
941 .chars()
942 .all(|c| c.is_alphanumeric() || c == '_' || c == '-');
943
944 if !is_valid {
945 let mut error = ValidationError::new("invalid_conversation_id");
946 error.message = Some(std::borrow::Cow::Owned(format!(
947 "Invalid 'conversation': '{conv_id}'. Expected an ID that contains letters, numbers, underscores, or dashes, but this value contained additional characters."
948 )));
949 return Err(error);
950 }
951 Ok(())
952}
953
954fn validate_tool_choice_with_tools(request: &ResponsesRequest) -> Result<(), ValidationError> {
956 let Some(tool_choice) = &request.tool_choice else {
957 return Ok(());
958 };
959
960 let has_tools = request.tools.as_ref().is_some_and(|t| !t.is_empty());
961 let is_some_choice = !matches!(tool_choice, ToolChoice::Value(ToolChoiceValue::None));
962
963 if is_some_choice && !has_tools {
965 let mut e = ValidationError::new("tool_choice_requires_tools");
966 e.message = Some("Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.".into());
967 return Err(e);
968 }
969
970 if !has_tools {
972 return Ok(());
973 }
974
975 let Some(tools) = request.tools.as_ref() else {
978 return Ok(());
979 };
980 let function_tool_names: Vec<&str> = tools
981 .iter()
982 .filter_map(|t| match t {
983 ResponseTool::Function(ft) => Some(ft.function.name.as_str()),
984 _ => None,
985 })
986 .collect();
987
988 match tool_choice {
990 ToolChoice::Function { function, .. } => {
991 if !function_tool_names.contains(&function.name.as_str()) {
992 let mut e = ValidationError::new("tool_choice_function_not_found");
993 e.message = Some(
994 format!(
995 "Invalid value for 'tool_choice': function '{}' not found in 'tools'.",
996 function.name
997 )
998 .into(),
999 );
1000 return Err(e);
1001 }
1002 }
1003 ToolChoice::AllowedTools {
1004 mode,
1005 tools: allowed_tools,
1006 ..
1007 } => {
1008 if mode != "auto" && mode != "required" {
1010 let mut e = ValidationError::new("tool_choice_invalid_mode");
1011 e.message = Some(
1012 format!(
1013 "Invalid value for 'tool_choice.mode': must be 'auto' or 'required', got '{mode}'."
1014 )
1015 .into(),
1016 );
1017 return Err(e);
1018 }
1019
1020 for tool_ref in allowed_tools {
1022 if let ToolReference::Function { name } = tool_ref {
1023 if !function_tool_names.contains(&name.as_str()) {
1024 let mut e = ValidationError::new("tool_choice_tool_not_found");
1025 e.message = Some(
1026 format!(
1027 "Invalid value for 'tool_choice.tools': tool '{name}' not found in 'tools'."
1028 )
1029 .into(),
1030 );
1031 return Err(e);
1032 }
1033 }
1034 }
1037 }
1038 ToolChoice::Value(_) => {}
1039 }
1040
1041 Ok(())
1042}
1043
1044fn validate_responses_cross_parameters(request: &ResponsesRequest) -> Result<(), ValidationError> {
1046 validate_tool_choice_with_tools(request)?;
1048
1049 if request.top_logprobs.is_some() {
1051 let has_logprobs_include = request
1052 .include
1053 .as_ref()
1054 .is_some_and(|inc| inc.contains(&IncludeField::MessageOutputTextLogprobs));
1055
1056 if !has_logprobs_include {
1057 let mut e = ValidationError::new("top_logprobs_requires_include");
1058 e.message = Some(
1059 "top_logprobs requires include field with 'message.output_text.logprobs'".into(),
1060 );
1061 return Err(e);
1062 }
1063 }
1064
1065 if request.background == Some(true) && request.stream == Some(true) {
1067 let mut e = ValidationError::new("background_conflicts_with_stream");
1068 e.message = Some("Cannot use background mode with streaming".into());
1069 return Err(e);
1070 }
1071
1072 if request.conversation.is_some() && request.previous_response_id.is_some() {
1074 let mut e = ValidationError::new("mutually_exclusive_parameters");
1075 e.message = Some("Mutually exclusive parameters. Ensure you are only providing one of: 'previous_response_id' or 'conversation'.".into());
1076 return Err(e);
1077 }
1078
1079 if let ResponseInput::Items(items) = &request.input {
1081 let has_valid_input = items.iter().any(|item| {
1083 matches!(
1084 item,
1085 ResponseInputOutputItem::Message { .. }
1086 | ResponseInputOutputItem::SimpleInputMessage { .. }
1087 )
1088 });
1089
1090 if !has_valid_input {
1091 let mut e = ValidationError::new("input_missing_user_message");
1092 e.message = Some("Input items must contain at least one message".into());
1093 return Err(e);
1094 }
1095 }
1096
1097 Ok(())
1102}
1103
1104fn validate_response_input(input: &ResponseInput) -> Result<(), ValidationError> {
1110 match input {
1111 ResponseInput::Text(text) => {
1112 if text.is_empty() {
1113 let mut e = ValidationError::new("input_text_empty");
1114 e.message = Some("Input text cannot be empty".into());
1115 return Err(e);
1116 }
1117 }
1118 ResponseInput::Items(items) => {
1119 if items.is_empty() {
1120 let mut e = ValidationError::new("input_items_empty");
1121 e.message = Some("Input items cannot be empty".into());
1122 return Err(e);
1123 }
1124 for item in items {
1126 validate_input_item(item)?;
1127 }
1128 }
1129 }
1130 Ok(())
1131}
1132
1133fn validate_input_item(item: &ResponseInputOutputItem) -> Result<(), ValidationError> {
1135 match item {
1136 ResponseInputOutputItem::Message { content, .. } => {
1137 if content.is_empty() {
1138 let mut e = ValidationError::new("message_content_empty");
1139 e.message = Some("Message content cannot be empty".into());
1140 return Err(e);
1141 }
1142 }
1143 ResponseInputOutputItem::SimpleInputMessage { content, .. } => match content {
1144 StringOrContentParts::String(s) if s.is_empty() => {
1145 let mut e = ValidationError::new("message_content_empty");
1146 e.message = Some("Message content cannot be empty".into());
1147 return Err(e);
1148 }
1149 StringOrContentParts::Array(parts) if parts.is_empty() => {
1150 let mut e = ValidationError::new("message_content_empty");
1151 e.message = Some("Message content parts cannot be empty".into());
1152 return Err(e);
1153 }
1154 _ => {}
1155 },
1156 ResponseInputOutputItem::Reasoning { .. } => {
1157 }
1159 ResponseInputOutputItem::FunctionCallOutput { output, .. } => {
1160 if output.is_empty() {
1161 let mut e = ValidationError::new("function_output_empty");
1162 e.message = Some("Function call output cannot be empty".into());
1163 return Err(e);
1164 }
1165 }
1166 ResponseInputOutputItem::FunctionToolCall { .. } => {}
1167 ResponseInputOutputItem::McpApprovalRequest { .. } => {}
1168 ResponseInputOutputItem::McpApprovalResponse { .. } => {}
1169 }
1170 Ok(())
1171}
1172
1173fn validate_response_tools(tools: &[ResponseTool]) -> Result<(), ValidationError> {
1175 let mut seen_mcp_labels: HashSet<String> = HashSet::new();
1177
1178 for (idx, tool) in tools.iter().enumerate() {
1179 if let ResponseTool::Mcp(mcp) = tool {
1180 let raw_label = mcp.server_label.as_str();
1181 if raw_label.is_empty() {
1182 let mut e = ValidationError::new("missing_required_parameter");
1183 e.message = Some(
1184 format!("Missing required parameter: 'tools[{idx}].server_label'.").into(),
1185 );
1186 return Err(e);
1187 }
1188
1189 let valid = raw_label.starts_with(|c: char| c.is_ascii_alphabetic())
1192 && raw_label
1193 .chars()
1194 .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
1195 if !valid {
1196 let mut e = ValidationError::new("invalid_server_label");
1197 e.message = Some(
1198 format!(
1199 "Invalid input {raw_label}: 'server_label' must start with a letter and consist of only letters, digits, '-' and '_'"
1200 )
1201 .into(),
1202 );
1203 return Err(e);
1204 }
1205
1206 let normalized = raw_label.to_lowercase();
1207 if !seen_mcp_labels.insert(normalized) {
1208 let mut e = ValidationError::new("mcp_tool_duplicate_server_label");
1209 e.message = Some(
1210 format!("Duplicate MCP server_label '{raw_label}' found in 'tools' parameter.")
1211 .into(),
1212 );
1213 return Err(e);
1214 }
1215 }
1216 }
1217 Ok(())
1218}
1219
1220fn validate_text_format(text: &TextConfig) -> Result<(), ValidationError> {
1222 if let Some(TextFormat::JsonSchema { name, .. }) = &text.format {
1223 if name.is_empty() {
1224 let mut e = ValidationError::new("json_schema_name_empty");
1225 e.message = Some("JSON schema name cannot be empty".into());
1226 return Err(e);
1227 }
1228 }
1229 Ok(())
1230}
1231
1232pub fn normalize_input_item(item: &ResponseInputOutputItem) -> ResponseInputOutputItem {
1246 match item {
1247 ResponseInputOutputItem::SimpleInputMessage { content, role, .. } => {
1248 let content_vec = match content {
1249 StringOrContentParts::String(s) => {
1250 vec![ResponseContentPart::InputText { text: s.clone() }]
1251 }
1252 StringOrContentParts::Array(parts) => parts.clone(),
1253 };
1254
1255 ResponseInputOutputItem::Message {
1256 id: generate_id("msg"),
1257 role: role.clone(),
1258 content: content_vec,
1259 status: Some("completed".to_string()),
1260 }
1261 }
1262 _ => item.clone(),
1263 }
1264}
1265
1266pub fn generate_id(prefix: &str) -> String {
1267 use rand::RngCore;
1268 let mut rng = rand::rng();
1269 let mut bytes = [0u8; 25];
1271 rng.fill_bytes(&mut bytes);
1272 let hex_string: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
1273 format!("{prefix}_{hex_string}")
1274}
1275
1276#[serde_with::skip_serializing_none]
1277#[derive(Debug, Clone, Deserialize, Serialize, schemars::JsonSchema)]
1278pub struct ResponsesResponse {
1279 pub id: String,
1281
1282 #[serde(default = "default_object_type")]
1284 pub object: String,
1285
1286 pub created_at: i64,
1288
1289 pub status: ResponseStatus,
1291
1292 pub error: Option<Value>,
1294
1295 pub incomplete_details: Option<Value>,
1297
1298 pub instructions: Option<String>,
1300
1301 pub max_output_tokens: Option<u32>,
1303
1304 pub model: String,
1306
1307 #[serde(default)]
1309 pub output: Vec<ResponseOutputItem>,
1310
1311 #[serde(default = "default_true")]
1313 pub parallel_tool_calls: bool,
1314
1315 pub previous_response_id: Option<String>,
1317
1318 pub reasoning: Option<ReasoningInfo>,
1320
1321 #[serde(default = "default_true")]
1323 pub store: bool,
1324
1325 pub temperature: Option<f32>,
1327
1328 pub text: Option<TextConfig>,
1330
1331 #[serde(default = "default_tool_choice")]
1333 pub tool_choice: String,
1334
1335 #[serde(default)]
1337 pub tools: Vec<ResponseTool>,
1338
1339 pub top_p: Option<f32>,
1341
1342 pub truncation: Option<String>,
1344
1345 pub usage: Option<ResponsesUsage>,
1347
1348 pub user: Option<String>,
1350
1351 pub safety_identifier: Option<String>,
1353
1354 #[serde(default)]
1356 pub metadata: HashMap<String, Value>,
1357}
1358
1359fn default_object_type() -> String {
1360 "response".to_string()
1361}
1362
1363fn default_tool_choice() -> String {
1364 "auto".to_string()
1365}
1366
1367impl ResponsesResponse {
1368 pub fn builder(id: impl Into<String>, model: impl Into<String>) -> ResponsesResponseBuilder {
1370 ResponsesResponseBuilder::new(id, model)
1371 }
1372
1373 pub fn is_complete(&self) -> bool {
1375 matches!(self.status, ResponseStatus::Completed)
1376 }
1377
1378 pub fn is_in_progress(&self) -> bool {
1380 matches!(self.status, ResponseStatus::InProgress)
1381 }
1382
1383 pub fn is_failed(&self) -> bool {
1385 matches!(self.status, ResponseStatus::Failed)
1386 }
1387}
1388
1389impl ResponseOutputItem {
1390 pub fn new_message(
1392 id: String,
1393 role: String,
1394 content: Vec<ResponseContentPart>,
1395 status: String,
1396 ) -> Self {
1397 Self::Message {
1398 id,
1399 role,
1400 content,
1401 status,
1402 }
1403 }
1404
1405 pub fn new_reasoning(
1407 id: String,
1408 summary: Vec<String>,
1409 content: Vec<ResponseReasoningContent>,
1410 status: Option<String>,
1411 ) -> Self {
1412 Self::Reasoning {
1413 id,
1414 summary,
1415 content,
1416 status,
1417 }
1418 }
1419
1420 pub fn new_function_tool_call(
1422 id: String,
1423 call_id: String,
1424 name: String,
1425 arguments: String,
1426 output: Option<String>,
1427 status: String,
1428 ) -> Self {
1429 Self::FunctionToolCall {
1430 id,
1431 call_id,
1432 name,
1433 arguments,
1434 output,
1435 status,
1436 }
1437 }
1438}
1439
1440impl ResponseContentPart {
1441 pub fn new_text(
1443 text: String,
1444 annotations: Vec<String>,
1445 logprobs: Option<ChatLogProbs>,
1446 ) -> Self {
1447 Self::OutputText {
1448 text,
1449 annotations,
1450 logprobs,
1451 }
1452 }
1453}
1454
1455impl ResponseReasoningContent {
1456 pub fn new_reasoning_text(text: String) -> Self {
1458 Self::ReasoningText { text }
1459 }
1460}