Skip to main content

rig_core/providers/openai/completion/
mod.rs

1// ================================================================
2// OpenAI Completion API
3// ================================================================
4
5use super::{
6    client::{ApiErrorResponse, ApiResponse},
7    streaming::StreamingCompletionResponse,
8};
9use crate::completion::{
10    CompletionError, CompletionRequest as CoreCompletionRequest, GetTokenUsage,
11};
12use crate::http_client::{self, HttpClientExt};
13use crate::message::{AudioMediaType, DocumentSourceKind, ImageDetail, MimeType};
14use crate::one_or_many::string_or_one_or_many;
15use crate::telemetry::{ProviderResponseExt, SpanCombinator};
16use crate::wasm_compat::{WasmCompatSend, WasmCompatSync};
17use crate::{OneOrMany, completion, json_utils, message};
18use serde::{Deserialize, Serialize, Serializer};
19use std::convert::Infallible;
20use std::fmt;
21use tracing::{Instrument, Level, enabled, info_span};
22
23use std::str::FromStr;
24
25pub mod streaming;
26
27/// Serializes user content as a plain string when there's a single text item,
28/// otherwise as an array of content parts.
29fn serialize_user_content<S>(
30    content: &OneOrMany<UserContent>,
31    serializer: S,
32) -> Result<S::Ok, S::Error>
33where
34    S: Serializer,
35{
36    if content.len() == 1
37        && let UserContent::Text { text } = content.first_ref()
38    {
39        return serializer.serialize_str(text);
40    }
41    content.serialize(serializer)
42}
43
44/// `gpt-5.5` completion model
45pub const GPT_5_5: &str = "gpt-5.5";
46
47/// `gpt-5.2` completion model
48pub const GPT_5_2: &str = "gpt-5.2";
49
50/// `gpt-5.1` completion model
51pub const GPT_5_1: &str = "gpt-5.1";
52
53/// `gpt-5` completion model
54pub const GPT_5: &str = "gpt-5";
55/// `gpt-5` completion model
56pub const GPT_5_MINI: &str = "gpt-5-mini";
57/// `gpt-5` completion model
58pub const GPT_5_NANO: &str = "gpt-5-nano";
59
60/// `gpt-4.5-preview` completion model
61pub const GPT_4_5_PREVIEW: &str = "gpt-4.5-preview";
62/// `gpt-4.5-preview-2025-02-27` completion model
63pub const GPT_4_5_PREVIEW_2025_02_27: &str = "gpt-4.5-preview-2025-02-27";
64/// `gpt-4o-2024-11-20` completion model (this is newer than 4o)
65pub const GPT_4O_2024_11_20: &str = "gpt-4o-2024-11-20";
66/// `gpt-4o` completion model
67pub const GPT_4O: &str = "gpt-4o";
68/// `gpt-4o-mini` completion model
69pub const GPT_4O_MINI: &str = "gpt-4o-mini";
70/// `gpt-4o-2024-05-13` completion model
71pub const GPT_4O_2024_05_13: &str = "gpt-4o-2024-05-13";
72/// `gpt-4-turbo` completion model
73pub const GPT_4_TURBO: &str = "gpt-4-turbo";
74/// `gpt-4-turbo-2024-04-09` completion model
75pub const GPT_4_TURBO_2024_04_09: &str = "gpt-4-turbo-2024-04-09";
76/// `gpt-4-turbo-preview` completion model
77pub const GPT_4_TURBO_PREVIEW: &str = "gpt-4-turbo-preview";
78/// `gpt-4-0125-preview` completion model
79pub const GPT_4_0125_PREVIEW: &str = "gpt-4-0125-preview";
80/// `gpt-4-1106-preview` completion model
81pub const GPT_4_1106_PREVIEW: &str = "gpt-4-1106-preview";
82/// `gpt-4-vision-preview` completion model
83pub const GPT_4_VISION_PREVIEW: &str = "gpt-4-vision-preview";
84/// `gpt-4-1106-vision-preview` completion model
85pub const GPT_4_1106_VISION_PREVIEW: &str = "gpt-4-1106-vision-preview";
86/// `gpt-4` completion model
87pub const GPT_4: &str = "gpt-4";
88/// `gpt-4-0613` completion model
89pub const GPT_4_0613: &str = "gpt-4-0613";
90/// `gpt-4-32k` completion model
91pub const GPT_4_32K: &str = "gpt-4-32k";
92/// `gpt-4-32k-0613` completion model
93pub const GPT_4_32K_0613: &str = "gpt-4-32k-0613";
94
95/// `o4-mini-2025-04-16` completion model
96pub const O4_MINI_2025_04_16: &str = "o4-mini-2025-04-16";
97/// `o4-mini` completion model
98pub const O4_MINI: &str = "o4-mini";
99/// `o3` completion model
100pub const O3: &str = "o3";
101/// `o3-mini` completion model
102pub const O3_MINI: &str = "o3-mini";
103/// `o3-mini-2025-01-31` completion model
104pub const O3_MINI_2025_01_31: &str = "o3-mini-2025-01-31";
105/// `o1-pro` completion model
106pub const O1_PRO: &str = "o1-pro";
107/// `o1`` completion model
108pub const O1: &str = "o1";
109/// `o1-2024-12-17` completion model
110pub const O1_2024_12_17: &str = "o1-2024-12-17";
111/// `o1-preview` completion model
112pub const O1_PREVIEW: &str = "o1-preview";
113/// `o1-preview-2024-09-12` completion model
114pub const O1_PREVIEW_2024_09_12: &str = "o1-preview-2024-09-12";
115/// `o1-mini completion model
116pub const O1_MINI: &str = "o1-mini";
117/// `o1-mini-2024-09-12` completion model
118pub const O1_MINI_2024_09_12: &str = "o1-mini-2024-09-12";
119
120/// `gpt-4.1-mini` completion model
121pub const GPT_4_1_MINI: &str = "gpt-4.1-mini";
122/// `gpt-4.1-nano` completion model
123pub const GPT_4_1_NANO: &str = "gpt-4.1-nano";
124/// `gpt-4.1-2025-04-14` completion model
125pub const GPT_4_1_2025_04_14: &str = "gpt-4.1-2025-04-14";
126/// `gpt-4.1` completion model
127pub const GPT_4_1: &str = "gpt-4.1";
128
129impl From<ApiErrorResponse> for CompletionError {
130    fn from(err: ApiErrorResponse) -> Self {
131        CompletionError::ProviderError(err.message)
132    }
133}
134
135#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
136#[serde(tag = "role", rename_all = "lowercase")]
137pub enum Message {
138    #[serde(alias = "developer")]
139    System {
140        #[serde(deserialize_with = "string_or_one_or_many")]
141        content: OneOrMany<SystemContent>,
142        #[serde(skip_serializing_if = "Option::is_none")]
143        name: Option<String>,
144    },
145    User {
146        #[serde(
147            deserialize_with = "string_or_one_or_many",
148            serialize_with = "serialize_user_content"
149        )]
150        content: OneOrMany<UserContent>,
151        #[serde(skip_serializing_if = "Option::is_none")]
152        name: Option<String>,
153    },
154    Assistant {
155        #[serde(
156            default,
157            deserialize_with = "json_utils::string_or_vec",
158            skip_serializing_if = "Vec::is_empty",
159            serialize_with = "serialize_assistant_content_vec"
160        )]
161        content: Vec<AssistantContent>,
162        // OpenAI-compatible providers expose hidden reasoning on this non-standard
163        // field, and some require it to be echoed back on assistant tool-call turns.
164        #[serde(skip_serializing_if = "Option::is_none", rename = "reasoning_content")]
165        reasoning: Option<String>,
166        #[serde(skip_serializing_if = "Option::is_none")]
167        refusal: Option<String>,
168        #[serde(skip_serializing_if = "Option::is_none")]
169        audio: Option<AudioAssistant>,
170        #[serde(skip_serializing_if = "Option::is_none")]
171        name: Option<String>,
172        #[serde(
173            default,
174            deserialize_with = "json_utils::null_or_vec",
175            skip_serializing_if = "Vec::is_empty"
176        )]
177        tool_calls: Vec<ToolCall>,
178    },
179    #[serde(rename = "tool")]
180    ToolResult {
181        tool_call_id: String,
182        content: ToolResultContentValue,
183    },
184}
185
186impl Message {
187    pub fn system(content: &str) -> Self {
188        Message::System {
189            content: OneOrMany::one(content.to_owned().into()),
190            name: None,
191        }
192    }
193}
194
195fn history_contains_tool_result(messages: &[Message]) -> bool {
196    messages
197        .iter()
198        .any(|message| matches!(message, Message::ToolResult { .. }))
199}
200
201#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
202pub struct AudioAssistant {
203    pub id: String,
204}
205
206#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
207pub struct SystemContent {
208    #[serde(default)]
209    pub r#type: SystemContentType,
210    pub text: String,
211}
212
213#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone)]
214#[serde(rename_all = "lowercase")]
215pub enum SystemContentType {
216    #[default]
217    Text,
218}
219
220#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
221#[serde(tag = "type", rename_all = "lowercase")]
222pub enum AssistantContent {
223    Text { text: String },
224    Refusal { refusal: String },
225}
226
227impl From<AssistantContent> for completion::AssistantContent {
228    fn from(value: AssistantContent) -> Self {
229        match value {
230            AssistantContent::Text { text } => completion::AssistantContent::text(text),
231            AssistantContent::Refusal { refusal } => completion::AssistantContent::text(refusal),
232        }
233    }
234}
235
236#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
237#[serde(tag = "type", rename_all = "lowercase")]
238pub enum UserContent {
239    Text {
240        text: String,
241    },
242    #[serde(rename = "image_url")]
243    Image {
244        image_url: ImageUrl,
245    },
246    Audio {
247        input_audio: InputAudio,
248    },
249    /// File content part for documents such as PDFs.
250    ///
251    /// Maps to OpenAI's `{"type":"file","file":{...}}` content type. Either
252    /// `file_data` (a base64 data URI like `data:application/pdf;base64,...`)
253    /// or `file_id` (a previously uploaded file reference) must be set.
254    File {
255        file: FileData,
256    },
257}
258
259#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
260pub struct ImageUrl {
261    pub url: String,
262    #[serde(default)]
263    pub detail: ImageDetail,
264}
265
266#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
267pub struct InputAudio {
268    pub data: String,
269    pub format: AudioMediaType,
270}
271
272/// File payload for [`UserContent::File`].
273///
274/// At least one of `file_data` or `file_id` must be set for the content part
275/// to be accepted by OpenAI's chat completions API. `filename` is optional
276/// but recommended.
277#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
278pub struct FileData {
279    /// Inline file data as a base64 data URI, e.g.
280    /// `data:application/pdf;base64,JVBERi0xLjQK...`.
281    #[serde(skip_serializing_if = "Option::is_none")]
282    pub file_data: Option<String>,
283    /// Identifier of a previously uploaded file (OpenAI Files API).
284    #[serde(skip_serializing_if = "Option::is_none")]
285    pub file_id: Option<String>,
286    /// Display name of the file. Recommended for inline `file_data`.
287    #[serde(skip_serializing_if = "Option::is_none")]
288    pub filename: Option<String>,
289}
290
291#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
292pub struct ToolResultContent {
293    #[serde(default)]
294    r#type: ToolResultContentType,
295    pub text: String,
296}
297
298#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone)]
299#[serde(rename_all = "lowercase")]
300pub enum ToolResultContentType {
301    #[default]
302    Text,
303}
304
305impl FromStr for ToolResultContent {
306    type Err = Infallible;
307
308    fn from_str(s: &str) -> Result<Self, Self::Err> {
309        Ok(s.to_owned().into())
310    }
311}
312
313impl From<String> for ToolResultContent {
314    fn from(s: String) -> Self {
315        ToolResultContent {
316            r#type: ToolResultContentType::default(),
317            text: s,
318        }
319    }
320}
321
322#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
323#[serde(untagged)]
324pub enum ToolResultContentValue {
325    Array(Vec<ToolResultContent>),
326    String(String),
327}
328
329impl ToolResultContentValue {
330    pub fn from_string(s: String, use_array_format: bool) -> Self {
331        if use_array_format {
332            ToolResultContentValue::Array(vec![ToolResultContent::from(s)])
333        } else {
334            ToolResultContentValue::String(s)
335        }
336    }
337
338    pub fn as_text(&self) -> String {
339        match self {
340            ToolResultContentValue::Array(arr) => arr
341                .iter()
342                .map(|c| c.text.clone())
343                .collect::<Vec<_>>()
344                .join("\n"),
345            ToolResultContentValue::String(s) => s.clone(),
346        }
347    }
348
349    pub fn to_array(&self) -> Self {
350        match self {
351            ToolResultContentValue::Array(_) => self.clone(),
352            ToolResultContentValue::String(s) => {
353                ToolResultContentValue::Array(vec![ToolResultContent::from(s.clone())])
354            }
355        }
356    }
357}
358
359#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
360pub struct ToolCall {
361    pub id: String,
362    #[serde(default)]
363    pub r#type: ToolType,
364    pub function: Function,
365}
366
367#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Clone)]
368#[serde(rename_all = "lowercase")]
369pub enum ToolType {
370    #[default]
371    Function,
372}
373
374/// Function definition for a tool, with optional strict mode
375#[derive(Debug, Deserialize, Serialize, Clone)]
376pub struct FunctionDefinition {
377    pub name: String,
378    pub description: String,
379    pub parameters: serde_json::Value,
380    #[serde(skip_serializing_if = "Option::is_none")]
381    pub strict: Option<bool>,
382}
383
384#[derive(Debug, Deserialize, Serialize, Clone)]
385pub struct ToolDefinition {
386    pub r#type: String,
387    pub function: FunctionDefinition,
388}
389
390impl From<completion::ToolDefinition> for ToolDefinition {
391    fn from(tool: completion::ToolDefinition) -> Self {
392        Self {
393            r#type: "function".into(),
394            function: FunctionDefinition {
395                name: tool.name,
396                description: tool.description,
397                parameters: tool.parameters,
398                strict: None,
399            },
400        }
401    }
402}
403
404impl ToolDefinition {
405    /// Apply strict mode to this tool definition.
406    /// This sets `strict: true` and sanitizes the schema to meet OpenAI requirements.
407    pub fn with_strict(mut self) -> Self {
408        self.function.strict = Some(true);
409        super::sanitize_schema(&mut self.function.parameters);
410        self
411    }
412}
413
414#[derive(Default, Clone, Debug, Deserialize, Serialize, PartialEq)]
415#[serde(rename_all = "snake_case")]
416pub enum ToolChoice {
417    #[default]
418    Auto,
419    None,
420    Required,
421}
422
423impl TryFrom<crate::message::ToolChoice> for ToolChoice {
424    type Error = CompletionError;
425    fn try_from(value: crate::message::ToolChoice) -> Result<Self, Self::Error> {
426        let res = match value {
427            message::ToolChoice::Specific { .. } => {
428                return Err(CompletionError::ProviderError(
429                    "Provider doesn't support only using specific tools".to_string(),
430                ));
431            }
432            message::ToolChoice::Auto => Self::Auto,
433            message::ToolChoice::None => Self::None,
434            message::ToolChoice::Required => Self::Required,
435        };
436
437        Ok(res)
438    }
439}
440
441#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
442pub struct Function {
443    pub name: String,
444    #[serde(
445        serialize_with = "json_utils::stringified_json::serialize",
446        deserialize_with = "json_utils::stringified_json::deserialize_maybe_stringified"
447    )]
448    pub arguments: serde_json::Value,
449}
450
451impl TryFrom<message::ToolResult> for Message {
452    type Error = message::MessageError;
453
454    fn try_from(value: message::ToolResult) -> Result<Self, Self::Error> {
455        let text = value
456            .content
457            .into_iter()
458            .map(|content| {
459                match content {
460                message::ToolResultContent::Text(message::Text { text }) => Ok(text),
461                message::ToolResultContent::Image(_) => Err(message::MessageError::ConversionError(
462                    "OpenAI does not support images in tool results. Tool results must be text."
463                        .into(),
464                )),
465            }
466            })
467            .collect::<Result<Vec<_>, _>>()?
468            .join("\n");
469
470        Ok(Message::ToolResult {
471            tool_call_id: value.id,
472            content: ToolResultContentValue::String(text),
473        })
474    }
475}
476
477impl TryFrom<message::UserContent> for UserContent {
478    type Error = message::MessageError;
479
480    fn try_from(value: message::UserContent) -> Result<Self, Self::Error> {
481        match value {
482            message::UserContent::Text(message::Text { text }) => Ok(UserContent::Text { text }),
483            message::UserContent::Image(message::Image {
484                data,
485                detail,
486                media_type,
487                ..
488            }) => match data {
489                DocumentSourceKind::Url(url) => Ok(UserContent::Image {
490                    image_url: ImageUrl {
491                        url,
492                        detail: detail.unwrap_or_default(),
493                    },
494                }),
495                DocumentSourceKind::Base64(data) => {
496                    let url = format!(
497                        "data:{};base64,{}",
498                        media_type.map(|i| i.to_mime_type()).ok_or(
499                            message::MessageError::ConversionError(
500                                "OpenAI Image URI must have media type".into()
501                            )
502                        )?,
503                        data
504                    );
505
506                    let detail = detail.ok_or(message::MessageError::ConversionError(
507                        "OpenAI image URI must have image detail".into(),
508                    ))?;
509
510                    Ok(UserContent::Image {
511                        image_url: ImageUrl { url, detail },
512                    })
513                }
514                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
515                    "Raw files not supported, encode as base64 first".into(),
516                )),
517                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
518                    "File IDs are not supported for images".into(),
519                )),
520                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
521                    "Document has no body".into(),
522                )),
523                doc => Err(message::MessageError::ConversionError(format!(
524                    "Unsupported document type: {doc:?}"
525                ))),
526            },
527            message::UserContent::Document(message::Document {
528                data: DocumentSourceKind::FileId(file_id),
529                ..
530            }) => Ok(UserContent::File {
531                file: FileData {
532                    file_data: None,
533                    file_id: Some(file_id),
534                    filename: None,
535                },
536            }),
537            message::UserContent::Document(message::Document {
538                data,
539                media_type: Some(message::DocumentMediaType::PDF),
540                ..
541            }) => match data {
542                DocumentSourceKind::Base64(b64) => Ok(UserContent::File {
543                    file: FileData {
544                        file_data: Some(format!("data:application/pdf;base64,{b64}")),
545                        file_id: None,
546                        filename: Some("document.pdf".to_string()),
547                    },
548                }),
549                DocumentSourceKind::Url(_) => Err(message::MessageError::ConversionError(
550                    "OpenAI chat completions does not accept URL files; use the Responses API or pass base64-encoded bytes".into(),
551                )),
552                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
553                    "Raw files not supported, encode as base64 first".into(),
554                )),
555                DocumentSourceKind::String(_) => Err(message::MessageError::ConversionError(
556                    "PDF documents must be base64-encoded, not raw strings".into(),
557                )),
558                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
559                    "File ID documents should be converted without media type constraints".into(),
560                )),
561                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
562                    "Document has no body".into(),
563                )),
564            },
565            message::UserContent::Document(message::Document { data, .. }) => {
566                if let DocumentSourceKind::Base64(text) | DocumentSourceKind::String(text) = data {
567                    Ok(UserContent::Text { text })
568                } else {
569                    Err(message::MessageError::ConversionError(
570                        "Documents must be base64 or a string".into(),
571                    ))
572                }
573            }
574            message::UserContent::Audio(message::Audio {
575                data, media_type, ..
576            }) => match data {
577                DocumentSourceKind::Base64(data) => Ok(UserContent::Audio {
578                    input_audio: InputAudio {
579                        data,
580                        format: match media_type {
581                            Some(media_type) => media_type,
582                            None => AudioMediaType::MP3,
583                        },
584                    },
585                }),
586                DocumentSourceKind::Url(_) => Err(message::MessageError::ConversionError(
587                    "URLs are not supported for audio".into(),
588                )),
589                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
590                    "Raw files are not supported for audio".into(),
591                )),
592                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
593                    "File IDs are not supported for audio".into(),
594                )),
595                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
596                    "Audio has no body".into(),
597                )),
598                audio => Err(message::MessageError::ConversionError(format!(
599                    "Unsupported audio type: {audio:?}"
600                ))),
601            },
602            message::UserContent::ToolResult(_) => Err(message::MessageError::ConversionError(
603                "Tool result is in unsupported format".into(),
604            )),
605            message::UserContent::Video(_) => Err(message::MessageError::ConversionError(
606                "Video is in unsupported format".into(),
607            )),
608        }
609    }
610}
611
612impl TryFrom<OneOrMany<message::UserContent>> for Vec<Message> {
613    type Error = message::MessageError;
614
615    fn try_from(value: OneOrMany<message::UserContent>) -> Result<Self, Self::Error> {
616        let (tool_results, other_content): (Vec<_>, Vec<_>) = value
617            .into_iter()
618            .partition(|content| matches!(content, message::UserContent::ToolResult(_)));
619
620        // If there are messages with both tool results and user content, openai will only
621        //  handle tool results. It's unlikely that there will be both.
622        if !tool_results.is_empty() {
623            tool_results
624                .into_iter()
625                .map(|content| match content {
626                    message::UserContent::ToolResult(tool_result) => tool_result.try_into(),
627                    _ => Err(message::MessageError::ConversionError(
628                        "expected tool result content while converting OpenAI input".into(),
629                    )),
630                })
631                .collect::<Result<Vec<_>, _>>()
632        } else {
633            let other_content: Vec<UserContent> = other_content
634                .into_iter()
635                .map(|content| content.try_into())
636                .collect::<Result<Vec<_>, _>>()?;
637
638            let other_content = OneOrMany::many(other_content).map_err(|_| {
639                message::MessageError::ConversionError(
640                    "OpenAI user message did not contain any non-tool content".into(),
641                )
642            })?;
643
644            Ok(vec![Message::User {
645                content: other_content,
646                name: None,
647            }])
648        }
649    }
650}
651
652impl TryFrom<OneOrMany<message::AssistantContent>> for Vec<Message> {
653    type Error = message::MessageError;
654
655    fn try_from(value: OneOrMany<message::AssistantContent>) -> Result<Self, Self::Error> {
656        let mut text_content = Vec::new();
657        let mut tool_calls = Vec::new();
658        let mut reasoning_text = String::new();
659
660        for content in value {
661            match content {
662                message::AssistantContent::Text(text) => text_content.push(text),
663                message::AssistantContent::ToolCall(tool_call) => tool_calls.push(tool_call),
664                message::AssistantContent::Reasoning(reasoning) => {
665                    reasoning_text.push_str(&reasoning.display_text());
666                }
667                message::AssistantContent::Image(_) => {
668                    return Err(message::MessageError::ConversionError(
669                        "OpenAI assistant messages do not support image content in chat completions"
670                            .into(),
671                    ));
672                }
673            }
674        }
675
676        if text_content.is_empty() && tool_calls.is_empty() {
677            return Ok(vec![]);
678        }
679
680        Ok(vec![Message::Assistant {
681            content: text_content
682                .into_iter()
683                .map(|content| content.text.into())
684                .collect::<Vec<_>>(),
685            reasoning: if reasoning_text.is_empty() {
686                None
687            } else {
688                Some(reasoning_text)
689            },
690            refusal: None,
691            audio: None,
692            name: None,
693            tool_calls: tool_calls
694                .into_iter()
695                .map(|tool_call| tool_call.into())
696                .collect::<Vec<_>>(),
697        }])
698    }
699}
700
701impl TryFrom<message::Message> for Vec<Message> {
702    type Error = message::MessageError;
703
704    fn try_from(message: message::Message) -> Result<Self, Self::Error> {
705        match message {
706            message::Message::System { content } => Ok(vec![Message::system(&content)]),
707            message::Message::User { content } => content.try_into(),
708            message::Message::Assistant { content, .. } => content.try_into(),
709        }
710    }
711}
712
713impl From<message::ToolCall> for ToolCall {
714    fn from(tool_call: message::ToolCall) -> Self {
715        Self {
716            id: tool_call.id,
717            r#type: ToolType::default(),
718            function: Function {
719                name: tool_call.function.name,
720                arguments: tool_call.function.arguments,
721            },
722        }
723    }
724}
725
726impl From<ToolCall> for message::ToolCall {
727    fn from(tool_call: ToolCall) -> Self {
728        Self {
729            id: tool_call.id,
730            call_id: None,
731            function: message::ToolFunction {
732                name: tool_call.function.name,
733                arguments: tool_call.function.arguments,
734            },
735            signature: None,
736            additional_params: None,
737        }
738    }
739}
740
741impl TryFrom<Message> for message::Message {
742    type Error = message::MessageError;
743
744    fn try_from(message: Message) -> Result<Self, Self::Error> {
745        Ok(match message {
746            Message::User { content, .. } => message::Message::User {
747                content: content.map(|content| content.into()),
748            },
749            Message::Assistant {
750                content,
751                tool_calls,
752                reasoning,
753                ..
754            } => {
755                let mut assistant_content = Vec::new();
756
757                if let Some(reasoning) = reasoning
758                    && !reasoning.is_empty()
759                {
760                    assistant_content.push(message::AssistantContent::reasoning(reasoning));
761                }
762
763                assistant_content.extend(content.into_iter().map(|content| match content {
764                    AssistantContent::Text { text } => message::AssistantContent::text(text),
765                    AssistantContent::Refusal { refusal } => {
766                        message::AssistantContent::text(refusal)
767                    }
768                }));
769
770                assistant_content.extend(
771                    tool_calls
772                        .into_iter()
773                        .map(|tool_call| Ok(message::AssistantContent::ToolCall(tool_call.into())))
774                        .collect::<Result<Vec<_>, _>>()?,
775                );
776
777                message::Message::Assistant {
778                    id: None,
779                    content: OneOrMany::many(assistant_content).map_err(|_| {
780                        message::MessageError::ConversionError(
781                            "Neither `content` nor `tool_calls` was provided to the Message"
782                                .to_owned(),
783                        )
784                    })?,
785                }
786            }
787
788            Message::ToolResult {
789                tool_call_id,
790                content,
791            } => message::Message::User {
792                content: OneOrMany::one(message::UserContent::tool_result(
793                    tool_call_id,
794                    OneOrMany::one(message::ToolResultContent::text(content.as_text())),
795                )),
796            },
797
798            // System messages should get stripped out when converting messages, this is just a
799            // stop gap to avoid obnoxious error handling or panic occurring.
800            Message::System { content, .. } => message::Message::User {
801                content: content.map(|content| message::UserContent::text(content.text)),
802            },
803        })
804    }
805}
806
807impl From<UserContent> for message::UserContent {
808    fn from(content: UserContent) -> Self {
809        match content {
810            UserContent::Text { text } => message::UserContent::text(text),
811            UserContent::Image { image_url } => {
812                message::UserContent::image_url(image_url.url, None, Some(image_url.detail))
813            }
814            UserContent::Audio { input_audio } => {
815                message::UserContent::audio(input_audio.data, Some(input_audio.format))
816            }
817            UserContent::File {
818                file: FileData {
819                    file_data, file_id, ..
820                },
821            } => match file_data {
822                Some(data_url) => {
823                    let kind = match data_url.strip_prefix("data:application/pdf;base64,") {
824                        Some(b64) => DocumentSourceKind::Base64(b64.to_string()),
825                        None => DocumentSourceKind::String(data_url),
826                    };
827                    message::UserContent::Document(message::Document {
828                        data: kind,
829                        media_type: Some(message::DocumentMediaType::PDF),
830                        additional_params: None,
831                    })
832                }
833                None => match file_id {
834                    Some(id) => message::UserContent::Document(message::Document {
835                        data: DocumentSourceKind::FileId(id),
836                        media_type: None,
837                        additional_params: None,
838                    }),
839                    None => message::UserContent::text(String::new()),
840                },
841            },
842        }
843    }
844}
845
846impl From<String> for UserContent {
847    fn from(s: String) -> Self {
848        UserContent::Text { text: s }
849    }
850}
851
852impl FromStr for UserContent {
853    type Err = Infallible;
854
855    fn from_str(s: &str) -> Result<Self, Self::Err> {
856        Ok(UserContent::Text {
857            text: s.to_string(),
858        })
859    }
860}
861
862impl From<String> for AssistantContent {
863    fn from(s: String) -> Self {
864        AssistantContent::Text { text: s }
865    }
866}
867
868impl FromStr for AssistantContent {
869    type Err = Infallible;
870
871    fn from_str(s: &str) -> Result<Self, Self::Err> {
872        Ok(AssistantContent::Text {
873            text: s.to_string(),
874        })
875    }
876}
877impl From<String> for SystemContent {
878    fn from(s: String) -> Self {
879        SystemContent {
880            r#type: SystemContentType::default(),
881            text: s,
882        }
883    }
884}
885
886impl FromStr for SystemContent {
887    type Err = Infallible;
888
889    fn from_str(s: &str) -> Result<Self, Self::Err> {
890        Ok(SystemContent {
891            r#type: SystemContentType::default(),
892            text: s.to_string(),
893        })
894    }
895}
896
897#[derive(Debug, Deserialize, Serialize)]
898pub struct CompletionResponse {
899    pub id: String,
900    pub object: String,
901    pub created: u64,
902    pub model: String,
903    pub system_fingerprint: Option<String>,
904    pub choices: Vec<Choice>,
905    pub usage: Option<Usage>,
906}
907
908impl TryFrom<CompletionResponse> for completion::CompletionResponse<CompletionResponse> {
909    type Error = CompletionError;
910
911    fn try_from(response: CompletionResponse) -> Result<Self, Self::Error> {
912        let choice = response.choices.first().ok_or_else(|| {
913            CompletionError::ResponseError("Response contained no choices".to_owned())
914        })?;
915
916        let content = match &choice.message {
917            Message::Assistant {
918                content,
919                tool_calls,
920                reasoning,
921                ..
922            } => {
923                let mut content = content
924                    .iter()
925                    .filter_map(|c| {
926                        let s = match c {
927                            AssistantContent::Text { text } => text,
928                            AssistantContent::Refusal { refusal } => refusal,
929                        };
930                        if s.is_empty() {
931                            None
932                        } else {
933                            Some(completion::AssistantContent::text(s))
934                        }
935                    })
936                    .collect::<Vec<_>>();
937
938                if let Some(reasoning) = reasoning {
939                    // llama.cpp exposes hidden reasoning on a separate non-standard field.
940                    // Keep it structured here so the non-streaming path matches streaming
941                    // behavior and does not pollute plain-text response surfaces.
942                    content.push(completion::AssistantContent::reasoning(reasoning));
943                }
944
945                content.extend(
946                    tool_calls
947                        .iter()
948                        .map(|call| {
949                            completion::AssistantContent::tool_call(
950                                &call.id,
951                                &call.function.name,
952                                call.function.arguments.clone(),
953                            )
954                        })
955                        .collect::<Vec<_>>(),
956                );
957                Ok(content)
958            }
959            _ => Err(CompletionError::ResponseError(
960                "Response did not contain a valid message or tool call".into(),
961            )),
962        }?;
963
964        let choice = OneOrMany::many(content).map_err(|_| {
965            CompletionError::ResponseError(
966                "Response contained no message or tool call (empty)".to_owned(),
967            )
968        })?;
969
970        let usage = response
971            .usage
972            .as_ref()
973            .map(|usage| completion::Usage {
974                input_tokens: usage.prompt_tokens as u64,
975                output_tokens: (usage.total_tokens - usage.prompt_tokens) as u64,
976                total_tokens: usage.total_tokens as u64,
977                cached_input_tokens: usage
978                    .prompt_tokens_details
979                    .as_ref()
980                    .map(|d| d.cached_tokens as u64)
981                    .unwrap_or(0),
982                cache_creation_input_tokens: 0,
983                reasoning_tokens: 0,
984            })
985            .unwrap_or_default();
986
987        Ok(completion::CompletionResponse {
988            choice,
989            usage,
990            raw_response: response,
991            message_id: None,
992        })
993    }
994}
995
996impl ProviderResponseExt for CompletionResponse {
997    type OutputMessage = Choice;
998    type Usage = Usage;
999
1000    fn get_response_id(&self) -> Option<String> {
1001        Some(self.id.to_owned())
1002    }
1003
1004    fn get_response_model_name(&self) -> Option<String> {
1005        Some(self.model.to_owned())
1006    }
1007
1008    fn get_output_messages(&self) -> Vec<Self::OutputMessage> {
1009        self.choices.clone()
1010    }
1011
1012    fn get_text_response(&self) -> Option<String> {
1013        let response = self
1014            .choices
1015            .iter()
1016            .filter_map(|choice| assistant_message_text_response(&choice.message))
1017            .collect::<Vec<_>>()
1018            .join("\n");
1019
1020        if response.is_empty() {
1021            None
1022        } else {
1023            Some(response)
1024        }
1025    }
1026
1027    fn get_usage(&self) -> Option<Self::Usage> {
1028        self.usage.clone()
1029    }
1030}
1031
1032fn assistant_message_text_response(message: &Message) -> Option<String> {
1033    let Message::Assistant {
1034        content, refusal, ..
1035    } = message
1036    else {
1037        return None;
1038    };
1039
1040    let mut segments = content
1041        .iter()
1042        .filter_map(|content| match content {
1043            AssistantContent::Text { text } => (!text.is_empty()).then(|| text.clone()),
1044            AssistantContent::Refusal { refusal } => (!refusal.is_empty()).then(|| refusal.clone()),
1045        })
1046        .collect::<Vec<_>>();
1047
1048    if segments.is_empty()
1049        && let Some(refusal) = refusal.as_ref().filter(|refusal| !refusal.is_empty())
1050    {
1051        segments.push(refusal.clone());
1052    }
1053
1054    if segments.is_empty() {
1055        None
1056    } else {
1057        Some(segments.join("\n"))
1058    }
1059}
1060
1061#[derive(Clone, Debug, Serialize, Deserialize)]
1062pub struct Choice {
1063    pub index: usize,
1064    pub message: Message,
1065    pub logprobs: Option<serde_json::Value>,
1066    pub finish_reason: String,
1067}
1068
1069#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1070pub struct PromptTokensDetails {
1071    /// Cached tokens from prompt caching
1072    #[serde(default)]
1073    pub cached_tokens: usize,
1074}
1075
1076#[derive(Clone, Debug, Deserialize, Serialize)]
1077pub struct Usage {
1078    pub prompt_tokens: usize,
1079    pub total_tokens: usize,
1080    #[serde(skip_serializing_if = "Option::is_none")]
1081    pub prompt_tokens_details: Option<PromptTokensDetails>,
1082}
1083
1084impl Usage {
1085    pub fn new() -> Self {
1086        Self {
1087            prompt_tokens: 0,
1088            total_tokens: 0,
1089            prompt_tokens_details: None,
1090        }
1091    }
1092}
1093
1094impl Default for Usage {
1095    fn default() -> Self {
1096        Self::new()
1097    }
1098}
1099
1100impl fmt::Display for Usage {
1101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1102        let Usage {
1103            prompt_tokens,
1104            total_tokens,
1105            ..
1106        } = self;
1107        write!(
1108            f,
1109            "Prompt tokens: {prompt_tokens} Total tokens: {total_tokens}"
1110        )
1111    }
1112}
1113
1114impl GetTokenUsage for Usage {
1115    fn token_usage(&self) -> Option<crate::completion::Usage> {
1116        Some(crate::providers::internal::completion_usage(
1117            self.prompt_tokens as u64,
1118            (self.total_tokens - self.prompt_tokens) as u64,
1119            self.total_tokens as u64,
1120            self.prompt_tokens_details
1121                .as_ref()
1122                .map(|d| d.cached_tokens as u64)
1123                .unwrap_or(0),
1124        ))
1125    }
1126}
1127
1128#[doc(hidden)]
1129#[derive(Clone)]
1130pub struct GenericCompletionModel<Ext = super::OpenAICompletionsExt, H = reqwest::Client> {
1131    pub(crate) client: crate::client::Client<Ext, H>,
1132    pub model: String,
1133    pub strict_tools: bool,
1134    pub tool_result_array_content: bool,
1135}
1136
1137/// The completion model struct for OpenAI's Chat Completions API.
1138///
1139/// This preserves the historical public generic shape where the first generic
1140/// parameter is the HTTP client type.
1141pub type CompletionModel<H = reqwest::Client> =
1142    GenericCompletionModel<super::OpenAICompletionsExt, H>;
1143
1144impl<Ext, H> GenericCompletionModel<Ext, H>
1145where
1146    crate::client::Client<Ext, H>: std::fmt::Debug + Clone + 'static,
1147    Ext: crate::client::Provider + Clone + 'static,
1148{
1149    pub fn new(client: crate::client::Client<Ext, H>, model: impl Into<String>) -> Self {
1150        Self {
1151            client,
1152            model: model.into(),
1153            strict_tools: false,
1154            tool_result_array_content: false,
1155        }
1156    }
1157
1158    pub fn with_model(client: crate::client::Client<Ext, H>, model: &str) -> Self {
1159        Self {
1160            client,
1161            model: model.into(),
1162            strict_tools: false,
1163            tool_result_array_content: false,
1164        }
1165    }
1166
1167    /// Enable strict mode for tool schemas.
1168    ///
1169    /// When enabled, tool schemas are automatically sanitized to meet OpenAI's strict mode requirements:
1170    /// - `additionalProperties: false` is added to all objects
1171    /// - All properties are marked as required
1172    /// - `strict: true` is set on each function definition
1173    ///
1174    /// This allows OpenAI to guarantee that the model's tool calls will match the schema exactly.
1175    pub fn with_strict_tools(mut self) -> Self {
1176        self.strict_tools = true;
1177        self
1178    }
1179
1180    pub fn with_tool_result_array_content(mut self) -> Self {
1181        self.tool_result_array_content = true;
1182        self
1183    }
1184}
1185
1186#[derive(Debug, Serialize, Deserialize, Clone)]
1187pub struct CompletionRequest {
1188    model: String,
1189    messages: Vec<Message>,
1190    #[serde(skip_serializing_if = "Vec::is_empty")]
1191    tools: Vec<ToolDefinition>,
1192    #[serde(skip_serializing_if = "Option::is_none")]
1193    tool_choice: Option<ToolChoice>,
1194    #[serde(skip_serializing_if = "Option::is_none")]
1195    temperature: Option<f64>,
1196    #[serde(skip_serializing_if = "Option::is_none")]
1197    max_tokens: Option<u64>,
1198    #[serde(flatten)]
1199    additional_params: Option<serde_json::Value>,
1200}
1201
1202pub struct OpenAIRequestParams {
1203    pub model: String,
1204    pub request: CoreCompletionRequest,
1205    pub strict_tools: bool,
1206    pub tool_result_array_content: bool,
1207}
1208
1209impl TryFrom<OpenAIRequestParams> for CompletionRequest {
1210    type Error = CompletionError;
1211
1212    fn try_from(params: OpenAIRequestParams) -> Result<Self, Self::Error> {
1213        let OpenAIRequestParams {
1214            model,
1215            request: req,
1216            strict_tools,
1217            tool_result_array_content,
1218        } = params;
1219
1220        let mut partial_history = vec![];
1221        if let Some(docs) = req.normalized_documents() {
1222            partial_history.push(docs);
1223        }
1224        let CoreCompletionRequest {
1225            model: request_model,
1226            preamble,
1227            chat_history,
1228            tools,
1229            temperature,
1230            max_tokens,
1231            additional_params,
1232            tool_choice,
1233            output_schema,
1234            ..
1235        } = req;
1236
1237        partial_history.extend(chat_history);
1238
1239        let mut full_history: Vec<Message> =
1240            preamble.map_or_else(Vec::new, |preamble| vec![Message::system(&preamble)]);
1241
1242        full_history.extend(
1243            partial_history
1244                .into_iter()
1245                .map(message::Message::try_into)
1246                .collect::<Result<Vec<Vec<Message>>, _>>()?
1247                .into_iter()
1248                .flatten()
1249                .collect::<Vec<_>>(),
1250        );
1251
1252        if full_history.is_empty() {
1253            return Err(CompletionError::RequestError(
1254                std::io::Error::new(
1255                    std::io::ErrorKind::InvalidInput,
1256                    "OpenAI Chat Completions request has no provider-compatible messages after conversion",
1257                )
1258                .into(),
1259            ));
1260        }
1261
1262        if tool_result_array_content {
1263            for msg in &mut full_history {
1264                if let Message::ToolResult { content, .. } = msg {
1265                    *content = content.to_array();
1266                }
1267            }
1268        }
1269
1270        let history_has_tool_result = history_contains_tool_result(&full_history);
1271
1272        let tool_choice = tool_choice.map(ToolChoice::try_from).transpose()?;
1273
1274        let tools: Vec<ToolDefinition> = tools
1275            .into_iter()
1276            .map(|tool| {
1277                let def = ToolDefinition::from(tool);
1278                if strict_tools { def.with_strict() } else { def }
1279            })
1280            .collect();
1281
1282        // Some OpenAI-compatible backends such as llama.cpp will skip tool execution
1283        // if `response_format` is sent on the first turn alongside tools. Delay the
1284        // schema until after the conversation contains a tool result.
1285        let should_apply_response_format =
1286            output_schema.is_some() && (tools.is_empty() || history_has_tool_result);
1287
1288        // Map output_schema to OpenAI's response_format and merge into additional_params
1289        let additional_params = if let Some(schema) = output_schema
1290            && should_apply_response_format
1291        {
1292            let name = schema
1293                .as_object()
1294                .and_then(|o| o.get("title"))
1295                .and_then(|v| v.as_str())
1296                .unwrap_or("response_schema")
1297                .to_string();
1298            let mut schema_value = schema.to_value();
1299            super::sanitize_schema(&mut schema_value);
1300            let response_format = serde_json::json!({
1301                "response_format": {
1302                    "type": "json_schema",
1303                    "json_schema": {
1304                        "name": name,
1305                        "strict": true,
1306                        "schema": schema_value
1307                    }
1308                }
1309            });
1310            Some(match additional_params {
1311                Some(existing) => json_utils::merge(existing, response_format),
1312                None => response_format,
1313            })
1314        } else {
1315            additional_params
1316        };
1317
1318        let res = Self {
1319            model: request_model.unwrap_or(model),
1320            messages: full_history,
1321            tools,
1322            tool_choice,
1323            temperature,
1324            max_tokens,
1325            additional_params,
1326        };
1327
1328        Ok(res)
1329    }
1330}
1331
1332impl TryFrom<(String, CoreCompletionRequest)> for CompletionRequest {
1333    type Error = CompletionError;
1334
1335    fn try_from((model, req): (String, CoreCompletionRequest)) -> Result<Self, Self::Error> {
1336        CompletionRequest::try_from(OpenAIRequestParams {
1337            model,
1338            request: req,
1339            strict_tools: false,
1340            tool_result_array_content: false,
1341        })
1342    }
1343}
1344
1345impl crate::telemetry::ProviderRequestExt for CompletionRequest {
1346    type InputMessage = Message;
1347
1348    fn get_input_messages(&self) -> Vec<Self::InputMessage> {
1349        self.messages.clone()
1350    }
1351
1352    fn get_system_prompt(&self) -> Option<String> {
1353        let first_message = self.messages.first()?;
1354
1355        let Message::System { ref content, .. } = first_message.clone() else {
1356            return None;
1357        };
1358
1359        let SystemContent { text, .. } = content.first();
1360
1361        Some(text)
1362    }
1363
1364    fn get_prompt(&self) -> Option<String> {
1365        let last_message = self.messages.last()?;
1366
1367        let Message::User { ref content, .. } = last_message.clone() else {
1368            return None;
1369        };
1370
1371        let UserContent::Text { text } = content.first() else {
1372            return None;
1373        };
1374
1375        Some(text)
1376    }
1377
1378    fn get_model_name(&self) -> String {
1379        self.model.clone()
1380    }
1381}
1382
1383impl GenericCompletionModel<super::OpenAICompletionsExt, reqwest::Client> {
1384    pub fn into_agent_builder(self) -> crate::agent::AgentBuilder<Self> {
1385        crate::agent::AgentBuilder::new(self)
1386    }
1387}
1388
1389impl<Ext, H> completion::CompletionModel for GenericCompletionModel<Ext, H>
1390where
1391    crate::client::Client<Ext, H>:
1392        HttpClientExt + Clone + WasmCompatSend + WasmCompatSync + 'static,
1393    Ext: crate::client::Provider
1394        + crate::client::DebugExt
1395        + Clone
1396        + WasmCompatSend
1397        + WasmCompatSync
1398        + 'static,
1399    H: Clone + Default + std::fmt::Debug + WasmCompatSend + WasmCompatSync + 'static,
1400{
1401    type Response = CompletionResponse;
1402    type StreamingResponse = StreamingCompletionResponse;
1403
1404    type Client = crate::client::Client<Ext, H>;
1405
1406    fn make(client: &Self::Client, model: impl Into<String>) -> Self {
1407        Self::new(client.clone(), model)
1408    }
1409
1410    async fn completion(
1411        &self,
1412        completion_request: CoreCompletionRequest,
1413    ) -> Result<completion::CompletionResponse<CompletionResponse>, CompletionError> {
1414        let span = if tracing::Span::current().is_disabled() {
1415            info_span!(
1416                target: "rig::completions",
1417                "chat",
1418                gen_ai.operation.name = "chat",
1419                gen_ai.provider.name = "openai",
1420                gen_ai.request.model = self.model,
1421                gen_ai.system_instructions = &completion_request.preamble,
1422                gen_ai.response.id = tracing::field::Empty,
1423                gen_ai.response.model = tracing::field::Empty,
1424                gen_ai.usage.output_tokens = tracing::field::Empty,
1425                gen_ai.usage.input_tokens = tracing::field::Empty,
1426                gen_ai.usage.cache_read.input_tokens = tracing::field::Empty,
1427            )
1428        } else {
1429            tracing::Span::current()
1430        };
1431
1432        let request = CompletionRequest::try_from(OpenAIRequestParams {
1433            model: self.model.to_owned(),
1434            request: completion_request,
1435            strict_tools: self.strict_tools,
1436            tool_result_array_content: self.tool_result_array_content,
1437        })?;
1438
1439        if enabled!(Level::TRACE) {
1440            tracing::trace!(
1441                target: "rig::completions",
1442                "OpenAI Chat Completions completion request: {}",
1443                serde_json::to_string_pretty(&request)?
1444            );
1445        }
1446
1447        let body = serde_json::to_vec(&request)?;
1448
1449        let req = self
1450            .client
1451            .post("/chat/completions")?
1452            .body(body)
1453            .map_err(|e| CompletionError::HttpError(e.into()))?;
1454
1455        async move {
1456            let response = self.client.send(req).await?;
1457
1458            if response.status().is_success() {
1459                let text = http_client::text(response).await?;
1460
1461                match serde_json::from_str::<ApiResponse<CompletionResponse>>(&text)? {
1462                    ApiResponse::Ok(response) => {
1463                        let span = tracing::Span::current();
1464                        span.record_response_metadata(&response);
1465                        span.record_token_usage(&response.usage);
1466
1467                        if enabled!(Level::TRACE) {
1468                            tracing::trace!(
1469                                target: "rig::completions",
1470                                "OpenAI Chat Completions completion response: {}",
1471                                serde_json::to_string_pretty(&response)?
1472                            );
1473                        }
1474
1475                        response.try_into()
1476                    }
1477                    ApiResponse::Err(err) => Err(CompletionError::ProviderError(err.message)),
1478                }
1479            } else {
1480                let text = http_client::text(response).await?;
1481                Err(CompletionError::ProviderError(text))
1482            }
1483        }
1484        .instrument(span)
1485        .await
1486    }
1487
1488    async fn stream(
1489        &self,
1490        request: CoreCompletionRequest,
1491    ) -> Result<
1492        crate::streaming::StreamingCompletionResponse<Self::StreamingResponse>,
1493        CompletionError,
1494    > {
1495        GenericCompletionModel::stream(self, request).await
1496    }
1497}
1498
1499fn serialize_assistant_content_vec<S>(
1500    value: &Vec<AssistantContent>,
1501    serializer: S,
1502) -> Result<S::Ok, S::Error>
1503where
1504    S: Serializer,
1505{
1506    if value.is_empty() {
1507        serializer.serialize_str("")
1508    } else {
1509        value.serialize(serializer)
1510    }
1511}
1512
1513#[cfg(test)]
1514mod tests {
1515    use super::*;
1516    use crate::telemetry::ProviderResponseExt;
1517
1518    #[test]
1519    fn test_openai_request_uses_request_model_override() {
1520        let request = crate::completion::CompletionRequest {
1521            model: Some("gpt-4.1".to_string()),
1522            preamble: None,
1523            chat_history: crate::OneOrMany::one("Hello".into()),
1524            documents: vec![],
1525            tools: vec![],
1526            temperature: None,
1527            max_tokens: None,
1528            tool_choice: None,
1529            additional_params: None,
1530            output_schema: None,
1531        };
1532
1533        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1534            model: "gpt-4o-mini".to_string(),
1535            request,
1536            strict_tools: false,
1537            tool_result_array_content: false,
1538        })
1539        .expect("request conversion should succeed");
1540        let serialized =
1541            serde_json::to_value(openai_request).expect("serialization should succeed");
1542
1543        assert_eq!(serialized["model"], "gpt-4.1");
1544    }
1545
1546    #[test]
1547    fn test_openai_request_uses_default_model_when_override_unset() {
1548        let request = crate::completion::CompletionRequest {
1549            model: None,
1550            preamble: None,
1551            chat_history: crate::OneOrMany::one("Hello".into()),
1552            documents: vec![],
1553            tools: vec![],
1554            temperature: None,
1555            max_tokens: None,
1556            tool_choice: None,
1557            additional_params: None,
1558            output_schema: None,
1559        };
1560
1561        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1562            model: "gpt-4o-mini".to_string(),
1563            request,
1564            strict_tools: false,
1565            tool_result_array_content: false,
1566        })
1567        .expect("request conversion should succeed");
1568        let serialized =
1569            serde_json::to_value(openai_request).expect("serialization should succeed");
1570
1571        assert_eq!(serialized["model"], "gpt-4o-mini");
1572    }
1573
1574    #[test]
1575    fn assistant_reasoning_alone_is_dropped() {
1576        let assistant_content = OneOrMany::one(message::AssistantContent::reasoning("hidden"));
1577
1578        let converted: Vec<Message> = assistant_content
1579            .try_into()
1580            .expect("conversion should work");
1581
1582        assert!(converted.is_empty());
1583    }
1584
1585    // Regression test: providers that serve thinking models over the OpenAI
1586    // Chat Completions schema (DeepSeek-R1, GLM-4.6, Qwen3-Thinking) return
1587    // 400 "thinking is enabled but reasoning_content is missing" on the next
1588    // turn if the prior assistant tool-call message didn't echo the reasoning.
1589    #[test]
1590    fn assistant_reasoning_is_attached_to_tool_call_message() {
1591        let assistant_content = OneOrMany::many(vec![
1592            message::AssistantContent::reasoning("hidden"),
1593            message::AssistantContent::text("visible"),
1594            message::AssistantContent::tool_call(
1595                "call_1",
1596                "subtract",
1597                serde_json::json!({"x": 2, "y": 1}),
1598            ),
1599        ])
1600        .expect("non-empty assistant content");
1601
1602        let converted: Vec<Message> = assistant_content
1603            .try_into()
1604            .expect("conversion should work");
1605        assert_eq!(converted.len(), 1);
1606
1607        match &converted[0] {
1608            Message::Assistant {
1609                content,
1610                tool_calls,
1611                reasoning,
1612                ..
1613            } => {
1614                assert_eq!(
1615                    content,
1616                    &vec![AssistantContent::Text {
1617                        text: "visible".to_string()
1618                    }]
1619                );
1620                assert_eq!(tool_calls.len(), 1);
1621                assert_eq!(tool_calls[0].id, "call_1");
1622                assert_eq!(tool_calls[0].function.name, "subtract");
1623                assert_eq!(
1624                    tool_calls[0].function.arguments,
1625                    serde_json::json!({"x": 2, "y": 1})
1626                );
1627                assert_eq!(reasoning.as_deref(), Some("hidden"));
1628            }
1629            _ => panic!("expected assistant message"),
1630        }
1631
1632        let json = serde_json::to_value(&converted[0]).expect("serialize");
1633        assert_eq!(json["reasoning_content"], "hidden");
1634    }
1635
1636    #[test]
1637    fn assistant_reasoning_roundtrips_back_to_rig_message() {
1638        let assistant = Message::Assistant {
1639            content: vec![AssistantContent::Text {
1640                text: "visible".to_string(),
1641            }],
1642            reasoning: Some("hidden".to_string()),
1643            refusal: None,
1644            audio: None,
1645            name: None,
1646            tool_calls: vec![],
1647        };
1648
1649        let rig_msg: message::Message = assistant.try_into().expect("convert back");
1650
1651        let message::Message::Assistant { content, .. } = rig_msg else {
1652            panic!("expected assistant");
1653        };
1654
1655        let items: Vec<_> = content.into_iter().collect();
1656        assert_eq!(items.len(), 2);
1657        assert!(matches!(items[0], message::AssistantContent::Reasoning(_)));
1658        assert!(matches!(items[1], message::AssistantContent::Text(_)));
1659    }
1660
1661    #[test]
1662    fn provider_response_text_response_reads_assistant_multipart_output() {
1663        let response = CompletionResponse {
1664            id: "resp_123".to_owned(),
1665            object: "chat.completion".to_owned(),
1666            created: 0,
1667            model: GPT_4O.to_owned(),
1668            system_fingerprint: None,
1669            choices: vec![Choice {
1670                index: 0,
1671                message: Message::Assistant {
1672                    content: vec![
1673                        AssistantContent::Text {
1674                            text: "first".to_owned(),
1675                        },
1676                        AssistantContent::Refusal {
1677                            refusal: "second".to_owned(),
1678                        },
1679                        AssistantContent::Text {
1680                            text: "third".to_owned(),
1681                        },
1682                    ],
1683                    reasoning: Some("hidden".to_owned()),
1684                    refusal: None,
1685                    audio: None,
1686                    name: None,
1687                    tool_calls: vec![],
1688                },
1689                logprobs: None,
1690                finish_reason: "stop".to_owned(),
1691            }],
1692            usage: None,
1693        };
1694
1695        assert_eq!(
1696            response.get_text_response(),
1697            Some("first\nsecond\nthird".to_owned())
1698        );
1699    }
1700
1701    #[test]
1702    fn provider_response_text_response_falls_back_to_assistant_refusal_field() {
1703        let response = CompletionResponse {
1704            id: "resp_123".to_owned(),
1705            object: "chat.completion".to_owned(),
1706            created: 0,
1707            model: GPT_4O.to_owned(),
1708            system_fingerprint: None,
1709            choices: vec![Choice {
1710                index: 0,
1711                message: Message::Assistant {
1712                    content: vec![],
1713                    reasoning: None,
1714                    refusal: Some("blocked".to_owned()),
1715                    audio: None,
1716                    name: None,
1717                    tool_calls: vec![],
1718                },
1719                logprobs: None,
1720                finish_reason: "stop".to_owned(),
1721            }],
1722            usage: None,
1723        };
1724
1725        assert_eq!(response.get_text_response(), Some("blocked".to_owned()));
1726    }
1727
1728    #[test]
1729    fn test_max_tokens_is_forwarded_to_request() {
1730        let request = crate::completion::CompletionRequest {
1731            model: None,
1732            preamble: None,
1733            chat_history: crate::OneOrMany::one("Hello".into()),
1734            documents: vec![],
1735            tools: vec![],
1736            temperature: None,
1737            max_tokens: Some(4096),
1738            tool_choice: None,
1739            additional_params: None,
1740            output_schema: None,
1741        };
1742
1743        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1744            model: "gpt-4o-mini".to_string(),
1745            request,
1746            strict_tools: false,
1747            tool_result_array_content: false,
1748        })
1749        .expect("request conversion should succeed");
1750        let serialized =
1751            serde_json::to_value(openai_request).expect("serialization should succeed");
1752
1753        assert_eq!(serialized["max_tokens"], 4096);
1754    }
1755
1756    #[test]
1757    fn test_max_tokens_omitted_when_none() {
1758        let request = crate::completion::CompletionRequest {
1759            model: None,
1760            preamble: None,
1761            chat_history: crate::OneOrMany::one("Hello".into()),
1762            documents: vec![],
1763            tools: vec![],
1764            temperature: None,
1765            max_tokens: None,
1766            tool_choice: None,
1767            additional_params: None,
1768            output_schema: None,
1769        };
1770
1771        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1772            model: "gpt-4o-mini".to_string(),
1773            request,
1774            strict_tools: false,
1775            tool_result_array_content: false,
1776        })
1777        .expect("request conversion should succeed");
1778        let serialized =
1779            serde_json::to_value(openai_request).expect("serialization should succeed");
1780
1781        assert!(serialized.get("max_tokens").is_none());
1782    }
1783
1784    #[test]
1785    fn request_conversion_errors_when_all_messages_are_filtered() {
1786        let request = CoreCompletionRequest {
1787            model: None,
1788            preamble: None,
1789            chat_history: OneOrMany::one(message::Message::Assistant {
1790                id: None,
1791                content: OneOrMany::one(message::AssistantContent::reasoning("hidden")),
1792            }),
1793            documents: vec![],
1794            tools: vec![],
1795            temperature: None,
1796            max_tokens: None,
1797            tool_choice: None,
1798            additional_params: None,
1799            output_schema: None,
1800        };
1801
1802        let result = CompletionRequest::try_from(OpenAIRequestParams {
1803            model: "gpt-4o-mini".to_string(),
1804            request,
1805            strict_tools: false,
1806            tool_result_array_content: false,
1807        });
1808
1809        assert!(matches!(result, Err(CompletionError::RequestError(_))));
1810    }
1811
1812    #[test]
1813    fn request_conversion_omits_response_format_on_initial_tool_turn() {
1814        let request = CoreCompletionRequest {
1815            model: None,
1816            preamble: None,
1817            chat_history: OneOrMany::one(message::Message::user(
1818                "Hello, whats the weather in London?",
1819            )),
1820            documents: vec![],
1821            tools: vec![completion::ToolDefinition {
1822                name: "weather".to_string(),
1823                description: "Get the weather".to_string(),
1824                parameters: serde_json::json!({
1825                    "type": "object",
1826                    "properties": {
1827                        "city": { "type": "string" }
1828                    },
1829                    "required": ["city"]
1830                }),
1831            }],
1832            temperature: None,
1833            max_tokens: None,
1834            tool_choice: None,
1835            additional_params: None,
1836            output_schema: Some(
1837                serde_json::from_value(serde_json::json!({
1838                    "title": "WeatherResponse",
1839                    "type": "object",
1840                    "properties": {
1841                        "city": { "type": "string" },
1842                        "weather": { "type": "string" }
1843                    },
1844                    "required": ["city", "weather"]
1845                }))
1846                .expect("schema should deserialize"),
1847            ),
1848        };
1849
1850        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1851            model: "gpt-4o-mini".to_string(),
1852            request,
1853            strict_tools: false,
1854            tool_result_array_content: false,
1855        })
1856        .expect("request conversion should succeed");
1857
1858        let serialized =
1859            serde_json::to_value(openai_request).expect("serialization should succeed");
1860
1861        assert!(
1862            serialized.get("response_format").is_none(),
1863            "initial tool turn should omit response_format: {serialized:?}"
1864        );
1865    }
1866
1867    #[test]
1868    fn request_conversion_restores_response_format_after_tool_result() {
1869        let request = CoreCompletionRequest {
1870            model: None,
1871            preamble: None,
1872            chat_history: OneOrMany::many(vec![
1873                message::Message::user("Hello, whats the weather in London?"),
1874                message::Message::Assistant {
1875                    id: None,
1876                    content: OneOrMany::one(message::AssistantContent::tool_call(
1877                        "call_1",
1878                        "weather",
1879                        serde_json::json!({ "city": "London" }),
1880                    )),
1881                },
1882                message::Message::tool_result(
1883                    "call_1",
1884                    "The weather in London is all fire and brimstone",
1885                ),
1886            ])
1887            .expect("history should be non-empty"),
1888            documents: vec![],
1889            tools: vec![completion::ToolDefinition {
1890                name: "weather".to_string(),
1891                description: "Get the weather".to_string(),
1892                parameters: serde_json::json!({
1893                    "type": "object",
1894                    "properties": {
1895                        "city": { "type": "string" }
1896                    },
1897                    "required": ["city"]
1898                }),
1899            }],
1900            temperature: None,
1901            max_tokens: None,
1902            tool_choice: None,
1903            additional_params: None,
1904            output_schema: Some(
1905                serde_json::from_value(serde_json::json!({
1906                    "title": "WeatherResponse",
1907                    "type": "object",
1908                    "properties": {
1909                        "city": { "type": "string" },
1910                        "weather": { "type": "string" }
1911                    },
1912                    "required": ["city", "weather"]
1913                }))
1914                .expect("schema should deserialize"),
1915            ),
1916        };
1917
1918        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1919            model: "gpt-4o-mini".to_string(),
1920            request,
1921            strict_tools: false,
1922            tool_result_array_content: false,
1923        })
1924        .expect("request conversion should succeed");
1925
1926        let serialized =
1927            serde_json::to_value(openai_request).expect("serialization should succeed");
1928
1929        assert!(
1930            serialized.get("response_format").is_some(),
1931            "follow-up turn should restore response_format: {serialized:?}"
1932        );
1933    }
1934
1935    #[test]
1936    fn deserialize_llama_cpp_tool_call() {
1937        let request = r#"{
1938            "choices": [{
1939                "finish_reason": "tool_calls",
1940                "index": 0,
1941                "message": {
1942                    "role": "assistant",
1943                    "content": "",
1944                    "tool_calls": [{ "type": "function", "function": { "name": "hello_world", "arguments": { "city": "Paris" } }, "id": "xxx" }]
1945                }
1946            }],
1947            "created": 0,
1948            "model": "gpt-4o-mini",
1949            "system_fingerprint": "fp_xxx",
1950            "object": "chat.completion",
1951            "usage": { "completion_tokens": 13, "prompt_tokens": 255, "total_tokens": 268 },
1952            "id": "xxx"
1953        }
1954        "#;
1955        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
1956
1957        let ApiResponse::Ok(response) = response else {
1958            panic!("expected successful completion response");
1959        };
1960        assert_eq!(response.choices.len(), 1);
1961
1962        let Message::Assistant { tool_calls, .. } = &response.choices[0].message else {
1963            panic!("expected assistant message");
1964        };
1965        assert_eq!(tool_calls.len(), 1);
1966        assert_eq!(tool_calls[0].id, "xxx");
1967        assert_eq!(tool_calls[0].function.name, "hello_world");
1968        assert_eq!(
1969            tool_calls[0].function.arguments,
1970            serde_json::json!({"city": "Paris"})
1971        );
1972    }
1973
1974    #[test]
1975    fn deserialize_openai_stringified_tool_call() {
1976        let request = r#"{
1977            "choices": [{
1978                "finish_reason": "tool_calls",
1979                "index": 0,
1980                "message": {
1981                    "role": "assistant",
1982                    "content": "",
1983                    "tool_calls": [{ "type": "function", "function": { "name": "hello_world", "arguments": "{\"city\":\"Paris\"}" }, "id": "xxx" }]
1984                }
1985            }],
1986            "created": 0,
1987            "model": "gpt-4o-mini",
1988            "system_fingerprint": "fp_xxx",
1989            "object": "chat.completion",
1990            "usage": { "completion_tokens": 13, "prompt_tokens": 255, "total_tokens": 268 },
1991            "id": "xxx"
1992        }
1993        "#;
1994        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
1995
1996        let ApiResponse::Ok(response) = response else {
1997            panic!("expected successful completion response");
1998        };
1999        assert_eq!(response.choices.len(), 1);
2000
2001        let Message::Assistant { tool_calls, .. } = &response.choices[0].message else {
2002            panic!("expected assistant message");
2003        };
2004        assert_eq!(tool_calls.len(), 1);
2005        assert_eq!(tool_calls[0].id, "xxx");
2006        assert_eq!(tool_calls[0].function.name, "hello_world");
2007        assert_eq!(
2008            tool_calls[0].function.arguments,
2009            serde_json::json!({"city": "Paris"})
2010        );
2011    }
2012
2013    #[test]
2014    fn deserialize_llama_cpp_response_with_reasoning_content() {
2015        let request = r#"
2016        {
2017            "choices": [
2018                {
2019                    "finish_reason": "stop",
2020                    "index": 0,
2021                    "message": {
2022                        "role": "assistant",
2023                        "content": "",
2024                        "reasoning_content": "Now I understand the structure better. I need to: ..."
2025                    }
2026                }
2027            ],
2028            "created": 1776750378,
2029            "model": "unsloth/Qwen3.6-35B-A3B-GGUF:Q8_0",
2030            "system_fingerprint": "fp_xxx",
2031            "object": "chat.completion",
2032            "usage": {
2033                "completion_tokens": 920,
2034                "prompt_tokens": 27806,
2035                "total_tokens": 28726,
2036                "prompt_tokens_details": { "cached_tokens": 18698 }
2037            },
2038            "id": "chatcmpl-xxxx",
2039            "timings": {
2040                "cache_n": 18698,
2041                "prompt_n": 9108,
2042                "prompt_ms": 226645.81,
2043                "prompt_per_token_ms": 24.884256697408873,
2044                "prompt_per_second": 40.186050648807495,
2045                "predicted_n": 920,
2046                "predicted_ms": 177167.955,
2047                "predicted_per_token_ms": 192.57386413043477,
2048                "predicted_per_second": 5.192812661860888
2049            }
2050        }
2051        "#;
2052        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
2053        let ApiResponse::Ok(response) = response else {
2054            panic!("expected successful completion response");
2055        };
2056
2057        let response: completion::CompletionResponse<CompletionResponse> =
2058            response.try_into().unwrap();
2059
2060        assert_eq!(response.choice.len(), 1);
2061
2062        let completion::message::AssistantContent::Reasoning(reasoning) = response.choice.first()
2063        else {
2064            panic!("expected assistant content to be reasoning");
2065        };
2066        assert_eq!(
2067            reasoning.first_text(),
2068            Some("Now I understand the structure better. I need to: ...")
2069        );
2070    }
2071
2072    #[test]
2073    fn pdf_base64_document_serializes_as_file_content_part() {
2074        let doc = message::UserContent::Document(message::Document {
2075            data: DocumentSourceKind::Base64("JVBERi0xLjQK".into()),
2076            media_type: Some(message::DocumentMediaType::PDF),
2077            additional_params: None,
2078        });
2079        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2080        let json = serde_json::to_value(&converted).expect("serialize");
2081
2082        assert_eq!(json["type"], "file");
2083        assert_eq!(
2084            json["file"]["file_data"],
2085            "data:application/pdf;base64,JVBERi0xLjQK"
2086        );
2087        assert_eq!(json["file"]["filename"], "document.pdf");
2088        assert!(json["file"].get("file_id").is_none());
2089    }
2090
2091    #[test]
2092    fn file_id_document_serializes_as_file_content_part() {
2093        let doc = message::UserContent::Document(message::Document {
2094            data: DocumentSourceKind::FileId("file_abc".into()),
2095            media_type: None,
2096            additional_params: None,
2097        });
2098        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2099        let json = serde_json::to_value(&converted).expect("serialize");
2100
2101        assert_eq!(json["type"], "file");
2102        assert_eq!(json["file"]["file_id"], "file_abc");
2103        assert!(json["file"].get("file_data").is_none());
2104    }
2105
2106    // Regression guard: callers passing markdown/plain text wrapped in
2107    // `UserContent::Document` should keep getting flattened to `text`.
2108    #[test]
2109    fn non_pdf_document_still_serializes_as_text() {
2110        let doc = message::UserContent::Document(message::Document {
2111            data: DocumentSourceKind::String("# Markdown".into()),
2112            media_type: None,
2113            additional_params: None,
2114        });
2115        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2116        let json = serde_json::to_value(&converted).expect("serialize");
2117
2118        assert_eq!(json["type"], "text");
2119        assert_eq!(json["text"], "# Markdown");
2120    }
2121
2122    #[test]
2123    fn pdf_url_document_returns_conversion_error() {
2124        let doc = message::UserContent::Document(message::Document {
2125            data: DocumentSourceKind::Url("https://example.com/x.pdf".into()),
2126            media_type: Some(message::DocumentMediaType::PDF),
2127            additional_params: None,
2128        });
2129        let res: Result<UserContent, _> = doc.try_into();
2130        assert!(matches!(
2131            res,
2132            Err(message::MessageError::ConversionError(_))
2133        ));
2134    }
2135
2136    #[test]
2137    fn pdf_raw_document_returns_conversion_error() {
2138        let doc = message::UserContent::Document(message::Document {
2139            data: DocumentSourceKind::Raw(b"%PDF-1.4\n".to_vec()),
2140            media_type: Some(message::DocumentMediaType::PDF),
2141            additional_params: None,
2142        });
2143        let res: Result<UserContent, _> = doc.try_into();
2144        assert!(matches!(
2145            res,
2146            Err(message::MessageError::ConversionError(_))
2147        ));
2148    }
2149
2150    #[test]
2151    fn file_user_content_deserializes_from_wire_json() {
2152        let raw = r#"{"type":"file","file":{"file_data":"data:application/pdf;base64,AAAA","filename":"x.pdf"}}"#;
2153        let parsed: UserContent = serde_json::from_str(raw).expect("deserialize");
2154        let UserContent::File { file } = parsed else {
2155            panic!("expected File variant");
2156        };
2157        assert_eq!(
2158            file.file_data.as_deref(),
2159            Some("data:application/pdf;base64,AAAA")
2160        );
2161        assert_eq!(file.filename.as_deref(), Some("x.pdf"));
2162        assert!(file.file_id.is_none());
2163    }
2164
2165    #[test]
2166    fn file_variant_round_trips_back_to_pdf_document() {
2167        let wire = UserContent::File {
2168            file: FileData {
2169                file_data: Some("data:application/pdf;base64,QUJD".to_string()),
2170                file_id: None,
2171                filename: Some("document.pdf".to_string()),
2172            },
2173        };
2174        let rig: message::UserContent = wire.into();
2175        let message::UserContent::Document(doc) = rig else {
2176            panic!("expected Document");
2177        };
2178        assert_eq!(doc.media_type, Some(message::DocumentMediaType::PDF));
2179        assert!(matches!(doc.data, DocumentSourceKind::Base64(ref b) if b == "QUJD"));
2180    }
2181
2182    #[test]
2183    fn file_variant_with_file_id_only_round_trips_to_document_file_id() {
2184        let wire = UserContent::File {
2185            file: FileData {
2186                file_data: None,
2187                file_id: Some("file_abc".to_string()),
2188                filename: None,
2189            },
2190        };
2191        let rig: message::UserContent = wire.into();
2192        let message::UserContent::Document(doc) = rig else {
2193            panic!("expected Document");
2194        };
2195        assert_eq!(doc.media_type, None);
2196        assert!(matches!(doc.data, DocumentSourceKind::FileId(ref id) if id == "file_abc"));
2197
2198        let converted: UserContent = message::UserContent::Document(doc)
2199            .try_into()
2200            .expect("conversion should succeed");
2201        let json = serde_json::to_value(&converted).expect("serialize");
2202
2203        assert_eq!(json["type"], "file");
2204        assert_eq!(json["file"]["file_id"], "file_abc");
2205        assert!(json["file"].get("file_data").is_none());
2206    }
2207
2208    // Guards against `OneOrMany::many` flattening at the User content site:
2209    // a mixed text + PDF message must produce one User message with both parts.
2210    #[test]
2211    fn mixed_text_and_pdf_user_message_produces_two_content_parts() {
2212        let user = message::Message::User {
2213            content: OneOrMany::many(vec![
2214                message::UserContent::text("What is in this PDF?"),
2215                message::UserContent::Document(message::Document {
2216                    data: DocumentSourceKind::Base64("JVBERi0K".into()),
2217                    media_type: Some(message::DocumentMediaType::PDF),
2218                    additional_params: None,
2219                }),
2220            ])
2221            .expect("non-empty content"),
2222        };
2223        let converted: Vec<Message> = user.try_into().expect("conversion should succeed");
2224        assert_eq!(converted.len(), 1);
2225        let Message::User { content, .. } = &converted[0] else {
2226            panic!("expected user message");
2227        };
2228        let parts: Vec<&UserContent> = content.iter().collect();
2229        assert_eq!(parts.len(), 2);
2230        assert!(matches!(parts[0], UserContent::Text { .. }));
2231        assert!(matches!(parts[1], UserContent::File { .. }));
2232    }
2233}