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.unwrap_or_default();
507
508                    Ok(UserContent::Image {
509                        image_url: ImageUrl { url, detail },
510                    })
511                }
512                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
513                    "Raw files not supported, encode as base64 first".into(),
514                )),
515                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
516                    "File IDs are not supported for images".into(),
517                )),
518                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
519                    "Document has no body".into(),
520                )),
521                doc => Err(message::MessageError::ConversionError(format!(
522                    "Unsupported document type: {doc:?}"
523                ))),
524            },
525            message::UserContent::Document(message::Document {
526                data: DocumentSourceKind::FileId(file_id),
527                ..
528            }) => Ok(UserContent::File {
529                file: FileData {
530                    file_data: None,
531                    file_id: Some(file_id),
532                    filename: None,
533                },
534            }),
535            message::UserContent::Document(message::Document {
536                data,
537                media_type: Some(message::DocumentMediaType::PDF),
538                ..
539            }) => match data {
540                DocumentSourceKind::Base64(b64) => Ok(UserContent::File {
541                    file: FileData {
542                        file_data: Some(format!("data:application/pdf;base64,{b64}")),
543                        file_id: None,
544                        filename: Some("document.pdf".to_string()),
545                    },
546                }),
547                DocumentSourceKind::Url(_) => Err(message::MessageError::ConversionError(
548                    "OpenAI chat completions does not accept URL files; use the Responses API or pass base64-encoded bytes".into(),
549                )),
550                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
551                    "Raw files not supported, encode as base64 first".into(),
552                )),
553                DocumentSourceKind::String(_) => Err(message::MessageError::ConversionError(
554                    "PDF documents must be base64-encoded, not raw strings".into(),
555                )),
556                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
557                    "File ID documents should be converted without media type constraints".into(),
558                )),
559                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
560                    "Document has no body".into(),
561                )),
562            },
563            message::UserContent::Document(message::Document { data, .. }) => {
564                if let DocumentSourceKind::Base64(text) | DocumentSourceKind::String(text) = data {
565                    Ok(UserContent::Text { text })
566                } else {
567                    Err(message::MessageError::ConversionError(
568                        "Documents must be base64 or a string".into(),
569                    ))
570                }
571            }
572            message::UserContent::Audio(message::Audio {
573                data, media_type, ..
574            }) => match data {
575                DocumentSourceKind::Base64(data) => Ok(UserContent::Audio {
576                    input_audio: InputAudio {
577                        data,
578                        format: match media_type {
579                            Some(media_type) => media_type,
580                            None => AudioMediaType::MP3,
581                        },
582                    },
583                }),
584                DocumentSourceKind::Url(_) => Err(message::MessageError::ConversionError(
585                    "URLs are not supported for audio".into(),
586                )),
587                DocumentSourceKind::Raw(_) => Err(message::MessageError::ConversionError(
588                    "Raw files are not supported for audio".into(),
589                )),
590                DocumentSourceKind::FileId(_) => Err(message::MessageError::ConversionError(
591                    "File IDs are not supported for audio".into(),
592                )),
593                DocumentSourceKind::Unknown => Err(message::MessageError::ConversionError(
594                    "Audio has no body".into(),
595                )),
596                audio => Err(message::MessageError::ConversionError(format!(
597                    "Unsupported audio type: {audio:?}"
598                ))),
599            },
600            message::UserContent::ToolResult(_) => Err(message::MessageError::ConversionError(
601                "Tool result is in unsupported format".into(),
602            )),
603            message::UserContent::Video(_) => Err(message::MessageError::ConversionError(
604                "Video is in unsupported format".into(),
605            )),
606        }
607    }
608}
609
610impl TryFrom<OneOrMany<message::UserContent>> for Vec<Message> {
611    type Error = message::MessageError;
612
613    fn try_from(value: OneOrMany<message::UserContent>) -> Result<Self, Self::Error> {
614        let (tool_results, other_content): (Vec<_>, Vec<_>) = value
615            .into_iter()
616            .partition(|content| matches!(content, message::UserContent::ToolResult(_)));
617
618        // If there are messages with both tool results and user content, openai will only
619        //  handle tool results. It's unlikely that there will be both.
620        if !tool_results.is_empty() {
621            tool_results
622                .into_iter()
623                .map(|content| match content {
624                    message::UserContent::ToolResult(tool_result) => tool_result.try_into(),
625                    _ => Err(message::MessageError::ConversionError(
626                        "expected tool result content while converting OpenAI input".into(),
627                    )),
628                })
629                .collect::<Result<Vec<_>, _>>()
630        } else {
631            let other_content: Vec<UserContent> = other_content
632                .into_iter()
633                .map(|content| content.try_into())
634                .collect::<Result<Vec<_>, _>>()?;
635
636            let other_content = OneOrMany::many(other_content).map_err(|_| {
637                message::MessageError::ConversionError(
638                    "OpenAI user message did not contain any non-tool content".into(),
639                )
640            })?;
641
642            Ok(vec![Message::User {
643                content: other_content,
644                name: None,
645            }])
646        }
647    }
648}
649
650impl TryFrom<OneOrMany<message::AssistantContent>> for Vec<Message> {
651    type Error = message::MessageError;
652
653    fn try_from(value: OneOrMany<message::AssistantContent>) -> Result<Self, Self::Error> {
654        let mut text_content = Vec::new();
655        let mut tool_calls = Vec::new();
656        let mut reasoning_text = String::new();
657
658        for content in value {
659            match content {
660                message::AssistantContent::Text(text) => text_content.push(text),
661                message::AssistantContent::ToolCall(tool_call) => tool_calls.push(tool_call),
662                message::AssistantContent::Reasoning(reasoning) => {
663                    reasoning_text.push_str(&reasoning.display_text());
664                }
665                message::AssistantContent::Image(_) => {
666                    return Err(message::MessageError::ConversionError(
667                        "OpenAI assistant messages do not support image content in chat completions"
668                            .into(),
669                    ));
670                }
671            }
672        }
673
674        if text_content.is_empty() && tool_calls.is_empty() {
675            return Ok(vec![]);
676        }
677
678        Ok(vec![Message::Assistant {
679            content: text_content
680                .into_iter()
681                .map(|content| content.text.into())
682                .collect::<Vec<_>>(),
683            reasoning: if reasoning_text.is_empty() {
684                None
685            } else {
686                Some(reasoning_text)
687            },
688            refusal: None,
689            audio: None,
690            name: None,
691            tool_calls: tool_calls
692                .into_iter()
693                .map(|tool_call| tool_call.into())
694                .collect::<Vec<_>>(),
695        }])
696    }
697}
698
699impl TryFrom<message::Message> for Vec<Message> {
700    type Error = message::MessageError;
701
702    fn try_from(message: message::Message) -> Result<Self, Self::Error> {
703        match message {
704            message::Message::System { content } => Ok(vec![Message::system(&content)]),
705            message::Message::User { content } => content.try_into(),
706            message::Message::Assistant { content, .. } => content.try_into(),
707        }
708    }
709}
710
711impl From<message::ToolCall> for ToolCall {
712    fn from(tool_call: message::ToolCall) -> Self {
713        Self {
714            id: tool_call.id,
715            r#type: ToolType::default(),
716            function: Function {
717                name: tool_call.function.name,
718                arguments: tool_call.function.arguments,
719            },
720        }
721    }
722}
723
724impl From<ToolCall> for message::ToolCall {
725    fn from(tool_call: ToolCall) -> Self {
726        Self {
727            id: tool_call.id,
728            call_id: None,
729            function: message::ToolFunction {
730                name: tool_call.function.name,
731                arguments: tool_call.function.arguments,
732            },
733            signature: None,
734            additional_params: None,
735        }
736    }
737}
738
739impl TryFrom<Message> for message::Message {
740    type Error = message::MessageError;
741
742    fn try_from(message: Message) -> Result<Self, Self::Error> {
743        Ok(match message {
744            Message::User { content, .. } => message::Message::User {
745                content: content.map(|content| content.into()),
746            },
747            Message::Assistant {
748                content,
749                tool_calls,
750                reasoning,
751                ..
752            } => {
753                let mut assistant_content = Vec::new();
754
755                if let Some(reasoning) = reasoning
756                    && !reasoning.is_empty()
757                {
758                    assistant_content.push(message::AssistantContent::reasoning(reasoning));
759                }
760
761                assistant_content.extend(content.into_iter().map(|content| match content {
762                    AssistantContent::Text { text, .. } => message::AssistantContent::text(text),
763                    AssistantContent::Refusal { refusal } => {
764                        message::AssistantContent::text(refusal)
765                    }
766                }));
767
768                assistant_content.extend(
769                    tool_calls
770                        .into_iter()
771                        .map(|tool_call| Ok(message::AssistantContent::ToolCall(tool_call.into())))
772                        .collect::<Result<Vec<_>, _>>()?,
773                );
774
775                message::Message::Assistant {
776                    id: None,
777                    content: OneOrMany::many(assistant_content).map_err(|_| {
778                        message::MessageError::ConversionError(
779                            "Neither `content` nor `tool_calls` was provided to the Message"
780                                .to_owned(),
781                        )
782                    })?,
783                }
784            }
785
786            Message::ToolResult {
787                tool_call_id,
788                content,
789            } => message::Message::User {
790                content: OneOrMany::one(message::UserContent::tool_result(
791                    tool_call_id,
792                    OneOrMany::one(message::ToolResultContent::text(content.as_text())),
793                )),
794            },
795
796            // System messages should get stripped out when converting messages, this is just a
797            // stop gap to avoid obnoxious error handling or panic occurring.
798            Message::System { content, .. } => message::Message::User {
799                content: content.map(|content| message::UserContent::text(content.text)),
800            },
801        })
802    }
803}
804
805impl From<UserContent> for message::UserContent {
806    fn from(content: UserContent) -> Self {
807        match content {
808            UserContent::Text { text, .. } => message::UserContent::text(text),
809            UserContent::Image { image_url } => {
810                message::UserContent::image_url(image_url.url, None, Some(image_url.detail))
811            }
812            UserContent::Audio { input_audio } => {
813                message::UserContent::audio(input_audio.data, Some(input_audio.format))
814            }
815            UserContent::File {
816                file: FileData {
817                    file_data, file_id, ..
818                },
819            } => match file_data {
820                Some(data_url) => {
821                    let kind = match data_url.strip_prefix("data:application/pdf;base64,") {
822                        Some(b64) => DocumentSourceKind::Base64(b64.to_string()),
823                        None => DocumentSourceKind::String(data_url),
824                    };
825                    message::UserContent::Document(message::Document {
826                        data: kind,
827                        media_type: Some(message::DocumentMediaType::PDF),
828                        additional_params: None,
829                    })
830                }
831                None => match file_id {
832                    Some(id) => message::UserContent::Document(message::Document {
833                        data: DocumentSourceKind::FileId(id),
834                        media_type: None,
835                        additional_params: None,
836                    }),
837                    None => message::UserContent::text(String::new()),
838                },
839            },
840        }
841    }
842}
843
844impl From<String> for UserContent {
845    fn from(s: String) -> Self {
846        UserContent::Text { text: s }
847    }
848}
849
850impl FromStr for UserContent {
851    type Err = Infallible;
852
853    fn from_str(s: &str) -> Result<Self, Self::Err> {
854        Ok(UserContent::Text {
855            text: s.to_string(),
856        })
857    }
858}
859
860impl From<String> for AssistantContent {
861    fn from(s: String) -> Self {
862        AssistantContent::Text { text: s }
863    }
864}
865
866impl FromStr for AssistantContent {
867    type Err = Infallible;
868
869    fn from_str(s: &str) -> Result<Self, Self::Err> {
870        Ok(AssistantContent::Text {
871            text: s.to_string(),
872        })
873    }
874}
875impl From<String> for SystemContent {
876    fn from(s: String) -> Self {
877        SystemContent {
878            r#type: SystemContentType::default(),
879            text: s,
880        }
881    }
882}
883
884impl FromStr for SystemContent {
885    type Err = Infallible;
886
887    fn from_str(s: &str) -> Result<Self, Self::Err> {
888        Ok(SystemContent {
889            r#type: SystemContentType::default(),
890            text: s.to_string(),
891        })
892    }
893}
894
895#[derive(Debug, Deserialize, Serialize)]
896pub struct CompletionResponse {
897    pub id: String,
898    pub object: String,
899    pub created: u64,
900    pub model: String,
901    pub system_fingerprint: Option<String>,
902    pub choices: Vec<Choice>,
903    pub usage: Option<Usage>,
904}
905
906impl TryFrom<CompletionResponse> for completion::CompletionResponse<CompletionResponse> {
907    type Error = CompletionError;
908
909    fn try_from(response: CompletionResponse) -> Result<Self, Self::Error> {
910        let choice = response.choices.first().ok_or_else(|| {
911            CompletionError::ResponseError("Response contained no choices".to_owned())
912        })?;
913
914        let content = match &choice.message {
915            Message::Assistant {
916                content,
917                tool_calls,
918                reasoning,
919                ..
920            } => {
921                let mut content = content
922                    .iter()
923                    .filter_map(|c| {
924                        let s = match c {
925                            AssistantContent::Text { text, .. } => text,
926                            AssistantContent::Refusal { refusal } => refusal,
927                        };
928                        if s.is_empty() {
929                            None
930                        } else {
931                            Some(completion::AssistantContent::text(s))
932                        }
933                    })
934                    .collect::<Vec<_>>();
935
936                if let Some(reasoning) = reasoning {
937                    // llama.cpp exposes hidden reasoning on a separate non-standard field.
938                    // Keep it structured here so the non-streaming path matches streaming
939                    // behavior and does not pollute plain-text response surfaces.
940                    content.push(completion::AssistantContent::reasoning(reasoning));
941                }
942
943                content.extend(
944                    tool_calls
945                        .iter()
946                        .map(|call| {
947                            completion::AssistantContent::tool_call(
948                                &call.id,
949                                &call.function.name,
950                                call.function.arguments.clone(),
951                            )
952                        })
953                        .collect::<Vec<_>>(),
954                );
955                Ok(content)
956            }
957            _ => Err(CompletionError::ResponseError(
958                "Response did not contain a valid message or tool call".into(),
959            )),
960        }?;
961
962        let choice = OneOrMany::many(content).map_err(|_| {
963            CompletionError::ResponseError(
964                "Response contained no message or tool call (empty)".to_owned(),
965            )
966        })?;
967
968        let usage = response
969            .usage
970            .as_ref()
971            .map(|usage| completion::Usage {
972                input_tokens: usage.prompt_tokens as u64,
973                output_tokens: (usage.total_tokens - usage.prompt_tokens) as u64,
974                total_tokens: usage.total_tokens as u64,
975                cached_input_tokens: usage
976                    .prompt_tokens_details
977                    .as_ref()
978                    .map(|d| d.cached_tokens as u64)
979                    .unwrap_or(0),
980                cache_creation_input_tokens: 0,
981                tool_use_prompt_tokens: 0,
982                reasoning_tokens: 0,
983            })
984            .unwrap_or_default();
985
986        Ok(completion::CompletionResponse {
987            choice,
988            usage,
989            raw_response: response,
990            message_id: None,
991        })
992    }
993}
994
995impl ProviderResponseExt for CompletionResponse {
996    type OutputMessage = Choice;
997    type Usage = Usage;
998
999    fn get_response_id(&self) -> Option<String> {
1000        Some(self.id.to_owned())
1001    }
1002
1003    fn get_response_model_name(&self) -> Option<String> {
1004        Some(self.model.to_owned())
1005    }
1006
1007    fn get_output_messages(&self) -> Vec<Self::OutputMessage> {
1008        self.choices.clone()
1009    }
1010
1011    fn get_text_response(&self) -> Option<String> {
1012        let response = self
1013            .choices
1014            .iter()
1015            .filter_map(|choice| assistant_message_text_response(&choice.message))
1016            .collect::<Vec<_>>()
1017            .join("\n");
1018
1019        if response.is_empty() {
1020            None
1021        } else {
1022            Some(response)
1023        }
1024    }
1025
1026    fn get_usage(&self) -> Option<Self::Usage> {
1027        self.usage.clone()
1028    }
1029}
1030
1031fn assistant_message_text_response(message: &Message) -> Option<String> {
1032    let Message::Assistant {
1033        content, refusal, ..
1034    } = message
1035    else {
1036        return None;
1037    };
1038
1039    let mut segments = content
1040        .iter()
1041        .filter_map(|content| match content {
1042            AssistantContent::Text { text, .. } => (!text.is_empty()).then(|| text.clone()),
1043            AssistantContent::Refusal { refusal } => (!refusal.is_empty()).then(|| refusal.clone()),
1044        })
1045        .collect::<Vec<_>>();
1046
1047    if segments.is_empty()
1048        && let Some(refusal) = refusal.as_ref().filter(|refusal| !refusal.is_empty())
1049    {
1050        segments.push(refusal.clone());
1051    }
1052
1053    if segments.is_empty() {
1054        None
1055    } else {
1056        Some(segments.join("\n"))
1057    }
1058}
1059
1060#[derive(Clone, Debug, Serialize, Deserialize)]
1061pub struct Choice {
1062    pub index: usize,
1063    pub message: Message,
1064    pub logprobs: Option<serde_json::Value>,
1065    pub finish_reason: String,
1066}
1067
1068#[derive(Clone, Debug, Deserialize, Serialize, Default)]
1069pub struct PromptTokensDetails {
1070    /// Cached tokens from prompt caching
1071    #[serde(default)]
1072    pub cached_tokens: usize,
1073}
1074
1075#[derive(Clone, Debug, Deserialize, Serialize)]
1076pub struct Usage {
1077    pub prompt_tokens: usize,
1078    pub total_tokens: usize,
1079    #[serde(skip_serializing_if = "Option::is_none")]
1080    pub prompt_tokens_details: Option<PromptTokensDetails>,
1081}
1082
1083impl Usage {
1084    pub fn new() -> Self {
1085        Self {
1086            prompt_tokens: 0,
1087            total_tokens: 0,
1088            prompt_tokens_details: None,
1089        }
1090    }
1091}
1092
1093impl Default for Usage {
1094    fn default() -> Self {
1095        Self::new()
1096    }
1097}
1098
1099impl fmt::Display for Usage {
1100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1101        let Usage {
1102            prompt_tokens,
1103            total_tokens,
1104            ..
1105        } = self;
1106        write!(
1107            f,
1108            "Prompt tokens: {prompt_tokens} Total tokens: {total_tokens}"
1109        )
1110    }
1111}
1112
1113impl GetTokenUsage for Usage {
1114    fn token_usage(&self) -> Option<crate::completion::Usage> {
1115        Some(crate::providers::internal::completion_usage(
1116            self.prompt_tokens as u64,
1117            (self.total_tokens - self.prompt_tokens) as u64,
1118            self.total_tokens as u64,
1119            self.prompt_tokens_details
1120                .as_ref()
1121                .map(|d| d.cached_tokens as u64)
1122                .unwrap_or(0),
1123        ))
1124    }
1125}
1126
1127#[doc(hidden)]
1128#[derive(Clone)]
1129pub struct GenericCompletionModel<Ext = super::OpenAICompletionsExt, H = reqwest::Client> {
1130    pub(crate) client: crate::client::Client<Ext, H>,
1131    pub model: String,
1132    pub strict_tools: bool,
1133    pub tool_result_array_content: bool,
1134}
1135
1136/// The completion model struct for OpenAI's Chat Completions API.
1137///
1138/// This preserves the historical public generic shape where the first generic
1139/// parameter is the HTTP client type.
1140pub type CompletionModel<H = reqwest::Client> =
1141    GenericCompletionModel<super::OpenAICompletionsExt, H>;
1142
1143impl<Ext, H> GenericCompletionModel<Ext, H>
1144where
1145    crate::client::Client<Ext, H>: std::fmt::Debug + Clone + 'static,
1146    Ext: crate::client::Provider + Clone + 'static,
1147{
1148    pub fn new(client: crate::client::Client<Ext, H>, model: impl Into<String>) -> Self {
1149        Self {
1150            client,
1151            model: model.into(),
1152            strict_tools: false,
1153            tool_result_array_content: false,
1154        }
1155    }
1156
1157    pub fn with_model(client: crate::client::Client<Ext, H>, model: &str) -> Self {
1158        Self {
1159            client,
1160            model: model.into(),
1161            strict_tools: false,
1162            tool_result_array_content: false,
1163        }
1164    }
1165
1166    /// Enable strict mode for tool schemas.
1167    ///
1168    /// When enabled, tool schemas are automatically sanitized to meet OpenAI's strict mode requirements:
1169    /// - `additionalProperties: false` is added to all objects
1170    /// - All properties are marked as required
1171    /// - `strict: true` is set on each function definition
1172    ///
1173    /// This allows OpenAI to guarantee that the model's tool calls will match the schema exactly.
1174    pub fn with_strict_tools(mut self) -> Self {
1175        self.strict_tools = true;
1176        self
1177    }
1178
1179    pub fn with_tool_result_array_content(mut self) -> Self {
1180        self.tool_result_array_content = true;
1181        self
1182    }
1183}
1184
1185#[derive(Debug, Serialize, Deserialize, Clone)]
1186pub struct CompletionRequest {
1187    model: String,
1188    messages: Vec<Message>,
1189    #[serde(skip_serializing_if = "Vec::is_empty")]
1190    tools: Vec<ToolDefinition>,
1191    #[serde(skip_serializing_if = "Option::is_none")]
1192    tool_choice: Option<ToolChoice>,
1193    #[serde(skip_serializing_if = "Option::is_none")]
1194    temperature: Option<f64>,
1195    #[serde(skip_serializing_if = "Option::is_none")]
1196    max_tokens: Option<u64>,
1197    #[serde(flatten)]
1198    additional_params: Option<serde_json::Value>,
1199}
1200
1201pub struct OpenAIRequestParams {
1202    pub model: String,
1203    pub request: CoreCompletionRequest,
1204    pub strict_tools: bool,
1205    pub tool_result_array_content: bool,
1206}
1207
1208impl TryFrom<OpenAIRequestParams> for CompletionRequest {
1209    type Error = CompletionError;
1210
1211    fn try_from(params: OpenAIRequestParams) -> Result<Self, Self::Error> {
1212        let OpenAIRequestParams {
1213            model,
1214            request: req,
1215            strict_tools,
1216            tool_result_array_content,
1217        } = params;
1218
1219        let mut partial_history = vec![];
1220        if let Some(docs) = req.normalized_documents() {
1221            partial_history.push(docs);
1222        }
1223        let CoreCompletionRequest {
1224            model: request_model,
1225            preamble,
1226            chat_history,
1227            tools,
1228            temperature,
1229            max_tokens,
1230            additional_params,
1231            tool_choice,
1232            output_schema,
1233            ..
1234        } = req;
1235
1236        partial_history.extend(chat_history);
1237
1238        let mut full_history: Vec<Message> =
1239            preamble.map_or_else(Vec::new, |preamble| vec![Message::system(&preamble)]);
1240
1241        full_history.extend(
1242            partial_history
1243                .into_iter()
1244                .map(message::Message::try_into)
1245                .collect::<Result<Vec<Vec<Message>>, _>>()?
1246                .into_iter()
1247                .flatten()
1248                .collect::<Vec<_>>(),
1249        );
1250
1251        if full_history.is_empty() {
1252            return Err(CompletionError::RequestError(
1253                std::io::Error::new(
1254                    std::io::ErrorKind::InvalidInput,
1255                    "OpenAI Chat Completions request has no provider-compatible messages after conversion",
1256                )
1257                .into(),
1258            ));
1259        }
1260
1261        if tool_result_array_content {
1262            for msg in &mut full_history {
1263                if let Message::ToolResult { content, .. } = msg {
1264                    *content = content.to_array();
1265                }
1266            }
1267        }
1268
1269        let history_has_tool_result = history_contains_tool_result(&full_history);
1270
1271        let tool_choice = tool_choice.map(ToolChoice::try_from).transpose()?;
1272
1273        let tools: Vec<ToolDefinition> = tools
1274            .into_iter()
1275            .map(|tool| {
1276                let def = ToolDefinition::from(tool);
1277                if strict_tools { def.with_strict() } else { def }
1278            })
1279            .collect();
1280
1281        // Some OpenAI-compatible backends such as llama.cpp will skip tool execution
1282        // if `response_format` is sent on the first turn alongside tools. Delay the
1283        // schema until after the conversation contains a tool result.
1284        let should_apply_response_format =
1285            output_schema.is_some() && (tools.is_empty() || history_has_tool_result);
1286
1287        // Map output_schema to OpenAI's response_format and merge into additional_params
1288        let additional_params = if let Some(schema) = output_schema
1289            && should_apply_response_format
1290        {
1291            let name = schema
1292                .as_object()
1293                .and_then(|o| o.get("title"))
1294                .and_then(|v| v.as_str())
1295                .unwrap_or("response_schema")
1296                .to_string();
1297            let mut schema_value = schema.to_value();
1298            super::sanitize_schema(&mut schema_value);
1299            let response_format = serde_json::json!({
1300                "response_format": {
1301                    "type": "json_schema",
1302                    "json_schema": {
1303                        "name": name,
1304                        "strict": true,
1305                        "schema": schema_value
1306                    }
1307                }
1308            });
1309            Some(match additional_params {
1310                Some(existing) => json_utils::merge(existing, response_format),
1311                None => response_format,
1312            })
1313        } else {
1314            additional_params
1315        };
1316
1317        let res = Self {
1318            model: request_model.unwrap_or(model),
1319            messages: full_history,
1320            tools,
1321            tool_choice,
1322            temperature,
1323            max_tokens,
1324            additional_params,
1325        };
1326
1327        Ok(res)
1328    }
1329}
1330
1331impl TryFrom<(String, CoreCompletionRequest)> for CompletionRequest {
1332    type Error = CompletionError;
1333
1334    fn try_from((model, req): (String, CoreCompletionRequest)) -> Result<Self, Self::Error> {
1335        CompletionRequest::try_from(OpenAIRequestParams {
1336            model,
1337            request: req,
1338            strict_tools: false,
1339            tool_result_array_content: false,
1340        })
1341    }
1342}
1343
1344impl crate::telemetry::ProviderRequestExt for CompletionRequest {
1345    type InputMessage = Message;
1346
1347    fn get_input_messages(&self) -> Vec<Self::InputMessage> {
1348        self.messages.clone()
1349    }
1350
1351    fn get_system_prompt(&self) -> Option<String> {
1352        let first_message = self.messages.first()?;
1353
1354        let Message::System { ref content, .. } = first_message.clone() else {
1355            return None;
1356        };
1357
1358        let SystemContent { text, .. } = content.first();
1359
1360        Some(text)
1361    }
1362
1363    fn get_prompt(&self) -> Option<String> {
1364        let last_message = self.messages.last()?;
1365
1366        let Message::User { ref content, .. } = last_message.clone() else {
1367            return None;
1368        };
1369
1370        let UserContent::Text { text, .. } = content.first() else {
1371            return None;
1372        };
1373
1374        Some(text)
1375    }
1376
1377    fn get_model_name(&self) -> String {
1378        self.model.clone()
1379    }
1380}
1381
1382impl GenericCompletionModel<super::OpenAICompletionsExt, reqwest::Client> {
1383    pub fn into_agent_builder(self) -> crate::agent::AgentBuilder<Self> {
1384        crate::agent::AgentBuilder::new(self)
1385    }
1386}
1387
1388impl<Ext, H> completion::CompletionModel for GenericCompletionModel<Ext, H>
1389where
1390    crate::client::Client<Ext, H>:
1391        HttpClientExt + Clone + WasmCompatSend + WasmCompatSync + 'static,
1392    Ext: crate::client::Provider
1393        + crate::client::DebugExt
1394        + Clone
1395        + WasmCompatSend
1396        + WasmCompatSync
1397        + 'static,
1398    H: Clone + Default + std::fmt::Debug + WasmCompatSend + WasmCompatSync + 'static,
1399{
1400    type Response = CompletionResponse;
1401    type StreamingResponse = StreamingCompletionResponse;
1402
1403    type Client = crate::client::Client<Ext, H>;
1404
1405    fn make(client: &Self::Client, model: impl Into<String>) -> Self {
1406        Self::new(client.clone(), model)
1407    }
1408
1409    async fn completion(
1410        &self,
1411        completion_request: CoreCompletionRequest,
1412    ) -> Result<completion::CompletionResponse<CompletionResponse>, CompletionError> {
1413        let span = if tracing::Span::current().is_disabled() {
1414            info_span!(
1415                target: "rig::completions",
1416                "chat",
1417                gen_ai.operation.name = "chat",
1418                gen_ai.provider.name = "openai",
1419                gen_ai.request.model = self.model,
1420                gen_ai.system_instructions = &completion_request.preamble,
1421                gen_ai.response.id = tracing::field::Empty,
1422                gen_ai.response.model = tracing::field::Empty,
1423                gen_ai.usage.output_tokens = tracing::field::Empty,
1424                gen_ai.usage.input_tokens = tracing::field::Empty,
1425                gen_ai.usage.cache_read.input_tokens = tracing::field::Empty,
1426            )
1427        } else {
1428            tracing::Span::current()
1429        };
1430
1431        let request = CompletionRequest::try_from(OpenAIRequestParams {
1432            model: self.model.to_owned(),
1433            request: completion_request,
1434            strict_tools: self.strict_tools,
1435            tool_result_array_content: self.tool_result_array_content,
1436        })?;
1437
1438        if enabled!(Level::TRACE) {
1439            tracing::trace!(
1440                target: "rig::completions",
1441                "OpenAI Chat Completions completion request: {}",
1442                serde_json::to_string_pretty(&request)?
1443            );
1444        }
1445
1446        let body = serde_json::to_vec(&request)?;
1447
1448        let req = self
1449            .client
1450            .post("/chat/completions")?
1451            .body(body)
1452            .map_err(|e| CompletionError::HttpError(e.into()))?;
1453
1454        async move {
1455            let response = self.client.send(req).await?;
1456
1457            if response.status().is_success() {
1458                let text = http_client::text(response).await?;
1459
1460                match serde_json::from_str::<ApiResponse<CompletionResponse>>(&text)? {
1461                    ApiResponse::Ok(response) => {
1462                        let span = tracing::Span::current();
1463                        span.record_response_metadata(&response);
1464                        span.record_token_usage(&response.usage);
1465
1466                        if enabled!(Level::TRACE) {
1467                            tracing::trace!(
1468                                target: "rig::completions",
1469                                "OpenAI Chat Completions completion response: {}",
1470                                serde_json::to_string_pretty(&response)?
1471                            );
1472                        }
1473
1474                        response.try_into()
1475                    }
1476                    ApiResponse::Err(err) => Err(CompletionError::ProviderError(err.message)),
1477                }
1478            } else {
1479                let text = http_client::text(response).await?;
1480                Err(CompletionError::ProviderError(text))
1481            }
1482        }
1483        .instrument(span)
1484        .await
1485    }
1486
1487    async fn stream(
1488        &self,
1489        request: CoreCompletionRequest,
1490    ) -> Result<
1491        crate::streaming::StreamingCompletionResponse<Self::StreamingResponse>,
1492        CompletionError,
1493    > {
1494        GenericCompletionModel::stream(self, request).await
1495    }
1496}
1497
1498fn serialize_assistant_content_vec<S>(
1499    value: &Vec<AssistantContent>,
1500    serializer: S,
1501) -> Result<S::Ok, S::Error>
1502where
1503    S: Serializer,
1504{
1505    if value.is_empty() {
1506        serializer.serialize_str("")
1507    } else {
1508        value.serialize(serializer)
1509    }
1510}
1511
1512#[cfg(test)]
1513mod tests {
1514    use super::*;
1515    use crate::telemetry::ProviderResponseExt;
1516
1517    #[test]
1518    fn test_openai_request_uses_request_model_override() {
1519        let request = crate::completion::CompletionRequest {
1520            model: Some("gpt-4.1".to_string()),
1521            preamble: None,
1522            chat_history: crate::OneOrMany::one("Hello".into()),
1523            documents: vec![],
1524            tools: vec![],
1525            temperature: None,
1526            max_tokens: None,
1527            tool_choice: None,
1528            additional_params: None,
1529            output_schema: None,
1530        };
1531
1532        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1533            model: "gpt-4o-mini".to_string(),
1534            request,
1535            strict_tools: false,
1536            tool_result_array_content: false,
1537        })
1538        .expect("request conversion should succeed");
1539        let serialized =
1540            serde_json::to_value(openai_request).expect("serialization should succeed");
1541
1542        assert_eq!(serialized["model"], "gpt-4.1");
1543    }
1544
1545    #[test]
1546    fn test_openai_request_uses_default_model_when_override_unset() {
1547        let request = crate::completion::CompletionRequest {
1548            model: None,
1549            preamble: None,
1550            chat_history: crate::OneOrMany::one("Hello".into()),
1551            documents: vec![],
1552            tools: vec![],
1553            temperature: None,
1554            max_tokens: None,
1555            tool_choice: None,
1556            additional_params: None,
1557            output_schema: None,
1558        };
1559
1560        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1561            model: "gpt-4o-mini".to_string(),
1562            request,
1563            strict_tools: false,
1564            tool_result_array_content: false,
1565        })
1566        .expect("request conversion should succeed");
1567        let serialized =
1568            serde_json::to_value(openai_request).expect("serialization should succeed");
1569
1570        assert_eq!(serialized["model"], "gpt-4o-mini");
1571    }
1572
1573    #[test]
1574    fn assistant_reasoning_alone_is_dropped() {
1575        let assistant_content = OneOrMany::one(message::AssistantContent::reasoning("hidden"));
1576
1577        let converted: Vec<Message> = assistant_content
1578            .try_into()
1579            .expect("conversion should work");
1580
1581        assert!(converted.is_empty());
1582    }
1583
1584    // Regression test: providers that serve thinking models over the OpenAI
1585    // Chat Completions schema (DeepSeek-R1, GLM-4.6, Qwen3-Thinking) return
1586    // 400 "thinking is enabled but reasoning_content is missing" on the next
1587    // turn if the prior assistant tool-call message didn't echo the reasoning.
1588    #[test]
1589    fn assistant_reasoning_is_attached_to_tool_call_message() {
1590        let assistant_content = OneOrMany::many(vec![
1591            message::AssistantContent::reasoning("hidden"),
1592            message::AssistantContent::text("visible"),
1593            message::AssistantContent::tool_call(
1594                "call_1",
1595                "subtract",
1596                serde_json::json!({"x": 2, "y": 1}),
1597            ),
1598        ])
1599        .expect("non-empty assistant content");
1600
1601        let converted: Vec<Message> = assistant_content
1602            .try_into()
1603            .expect("conversion should work");
1604        assert_eq!(converted.len(), 1);
1605
1606        match &converted[0] {
1607            Message::Assistant {
1608                content,
1609                tool_calls,
1610                reasoning,
1611                ..
1612            } => {
1613                assert_eq!(
1614                    content,
1615                    &vec![AssistantContent::Text {
1616                        text: "visible".to_string()
1617                    }]
1618                );
1619                assert_eq!(tool_calls.len(), 1);
1620                assert_eq!(tool_calls[0].id, "call_1");
1621                assert_eq!(tool_calls[0].function.name, "subtract");
1622                assert_eq!(
1623                    tool_calls[0].function.arguments,
1624                    serde_json::json!({"x": 2, "y": 1})
1625                );
1626                assert_eq!(reasoning.as_deref(), Some("hidden"));
1627            }
1628            _ => panic!("expected assistant message"),
1629        }
1630
1631        let json = serde_json::to_value(&converted[0]).expect("serialize");
1632        assert_eq!(json["reasoning_content"], "hidden");
1633    }
1634
1635    #[test]
1636    fn assistant_reasoning_roundtrips_back_to_rig_message() {
1637        let assistant = Message::Assistant {
1638            content: vec![AssistantContent::Text {
1639                text: "visible".to_string(),
1640            }],
1641            reasoning: Some("hidden".to_string()),
1642            refusal: None,
1643            audio: None,
1644            name: None,
1645            tool_calls: vec![],
1646        };
1647
1648        let rig_msg: message::Message = assistant.try_into().expect("convert back");
1649
1650        let message::Message::Assistant { content, .. } = rig_msg else {
1651            panic!("expected assistant");
1652        };
1653
1654        let items: Vec<_> = content.into_iter().collect();
1655        assert_eq!(items.len(), 2);
1656        assert!(matches!(items[0], message::AssistantContent::Reasoning(_)));
1657        assert!(matches!(items[1], message::AssistantContent::Text(_)));
1658    }
1659
1660    #[test]
1661    fn provider_response_text_response_reads_assistant_multipart_output() {
1662        let response = CompletionResponse {
1663            id: "resp_123".to_owned(),
1664            object: "chat.completion".to_owned(),
1665            created: 0,
1666            model: GPT_4O.to_owned(),
1667            system_fingerprint: None,
1668            choices: vec![Choice {
1669                index: 0,
1670                message: Message::Assistant {
1671                    content: vec![
1672                        AssistantContent::Text {
1673                            text: "first".to_owned(),
1674                        },
1675                        AssistantContent::Refusal {
1676                            refusal: "second".to_owned(),
1677                        },
1678                        AssistantContent::Text {
1679                            text: "third".to_owned(),
1680                        },
1681                    ],
1682                    reasoning: Some("hidden".to_owned()),
1683                    refusal: None,
1684                    audio: None,
1685                    name: None,
1686                    tool_calls: vec![],
1687                },
1688                logprobs: None,
1689                finish_reason: "stop".to_owned(),
1690            }],
1691            usage: None,
1692        };
1693
1694        assert_eq!(
1695            response.get_text_response(),
1696            Some("first\nsecond\nthird".to_owned())
1697        );
1698    }
1699
1700    #[test]
1701    fn provider_response_text_response_falls_back_to_assistant_refusal_field() {
1702        let response = CompletionResponse {
1703            id: "resp_123".to_owned(),
1704            object: "chat.completion".to_owned(),
1705            created: 0,
1706            model: GPT_4O.to_owned(),
1707            system_fingerprint: None,
1708            choices: vec![Choice {
1709                index: 0,
1710                message: Message::Assistant {
1711                    content: vec![],
1712                    reasoning: None,
1713                    refusal: Some("blocked".to_owned()),
1714                    audio: None,
1715                    name: None,
1716                    tool_calls: vec![],
1717                },
1718                logprobs: None,
1719                finish_reason: "stop".to_owned(),
1720            }],
1721            usage: None,
1722        };
1723
1724        assert_eq!(response.get_text_response(), Some("blocked".to_owned()));
1725    }
1726
1727    #[test]
1728    fn test_max_tokens_is_forwarded_to_request() {
1729        let request = crate::completion::CompletionRequest {
1730            model: None,
1731            preamble: None,
1732            chat_history: crate::OneOrMany::one("Hello".into()),
1733            documents: vec![],
1734            tools: vec![],
1735            temperature: None,
1736            max_tokens: Some(4096),
1737            tool_choice: None,
1738            additional_params: None,
1739            output_schema: None,
1740        };
1741
1742        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1743            model: "gpt-4o-mini".to_string(),
1744            request,
1745            strict_tools: false,
1746            tool_result_array_content: false,
1747        })
1748        .expect("request conversion should succeed");
1749        let serialized =
1750            serde_json::to_value(openai_request).expect("serialization should succeed");
1751
1752        assert_eq!(serialized["max_tokens"], 4096);
1753    }
1754
1755    #[test]
1756    fn test_max_tokens_omitted_when_none() {
1757        let request = crate::completion::CompletionRequest {
1758            model: None,
1759            preamble: None,
1760            chat_history: crate::OneOrMany::one("Hello".into()),
1761            documents: vec![],
1762            tools: vec![],
1763            temperature: None,
1764            max_tokens: None,
1765            tool_choice: None,
1766            additional_params: None,
1767            output_schema: None,
1768        };
1769
1770        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1771            model: "gpt-4o-mini".to_string(),
1772            request,
1773            strict_tools: false,
1774            tool_result_array_content: false,
1775        })
1776        .expect("request conversion should succeed");
1777        let serialized =
1778            serde_json::to_value(openai_request).expect("serialization should succeed");
1779
1780        assert!(serialized.get("max_tokens").is_none());
1781    }
1782
1783    #[test]
1784    fn request_conversion_errors_when_all_messages_are_filtered() {
1785        let request = CoreCompletionRequest {
1786            model: None,
1787            preamble: None,
1788            chat_history: OneOrMany::one(message::Message::Assistant {
1789                id: None,
1790                content: OneOrMany::one(message::AssistantContent::reasoning("hidden")),
1791            }),
1792            documents: vec![],
1793            tools: vec![],
1794            temperature: None,
1795            max_tokens: None,
1796            tool_choice: None,
1797            additional_params: None,
1798            output_schema: None,
1799        };
1800
1801        let result = CompletionRequest::try_from(OpenAIRequestParams {
1802            model: "gpt-4o-mini".to_string(),
1803            request,
1804            strict_tools: false,
1805            tool_result_array_content: false,
1806        });
1807
1808        assert!(matches!(result, Err(CompletionError::RequestError(_))));
1809    }
1810
1811    #[test]
1812    fn request_conversion_omits_response_format_on_initial_tool_turn() {
1813        let request = CoreCompletionRequest {
1814            model: None,
1815            preamble: None,
1816            chat_history: OneOrMany::one(message::Message::user(
1817                "Hello, whats the weather in London?",
1818            )),
1819            documents: vec![],
1820            tools: vec![completion::ToolDefinition {
1821                name: "weather".to_string(),
1822                description: "Get the weather".to_string(),
1823                parameters: serde_json::json!({
1824                    "type": "object",
1825                    "properties": {
1826                        "city": { "type": "string" }
1827                    },
1828                    "required": ["city"]
1829                }),
1830            }],
1831            temperature: None,
1832            max_tokens: None,
1833            tool_choice: None,
1834            additional_params: None,
1835            output_schema: Some(
1836                serde_json::from_value(serde_json::json!({
1837                    "title": "WeatherResponse",
1838                    "type": "object",
1839                    "properties": {
1840                        "city": { "type": "string" },
1841                        "weather": { "type": "string" }
1842                    },
1843                    "required": ["city", "weather"]
1844                }))
1845                .expect("schema should deserialize"),
1846            ),
1847        };
1848
1849        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1850            model: "gpt-4o-mini".to_string(),
1851            request,
1852            strict_tools: false,
1853            tool_result_array_content: false,
1854        })
1855        .expect("request conversion should succeed");
1856
1857        let serialized =
1858            serde_json::to_value(openai_request).expect("serialization should succeed");
1859
1860        assert!(
1861            serialized.get("response_format").is_none(),
1862            "initial tool turn should omit response_format: {serialized:?}"
1863        );
1864    }
1865
1866    #[test]
1867    fn request_conversion_restores_response_format_after_tool_result() {
1868        let request = CoreCompletionRequest {
1869            model: None,
1870            preamble: None,
1871            chat_history: OneOrMany::many(vec![
1872                message::Message::user("Hello, whats the weather in London?"),
1873                message::Message::Assistant {
1874                    id: None,
1875                    content: OneOrMany::one(message::AssistantContent::tool_call(
1876                        "call_1",
1877                        "weather",
1878                        serde_json::json!({ "city": "London" }),
1879                    )),
1880                },
1881                message::Message::tool_result(
1882                    "call_1",
1883                    "The weather in London is all fire and brimstone",
1884                ),
1885            ])
1886            .expect("history should be non-empty"),
1887            documents: vec![],
1888            tools: vec![completion::ToolDefinition {
1889                name: "weather".to_string(),
1890                description: "Get the weather".to_string(),
1891                parameters: serde_json::json!({
1892                    "type": "object",
1893                    "properties": {
1894                        "city": { "type": "string" }
1895                    },
1896                    "required": ["city"]
1897                }),
1898            }],
1899            temperature: None,
1900            max_tokens: None,
1901            tool_choice: None,
1902            additional_params: None,
1903            output_schema: Some(
1904                serde_json::from_value(serde_json::json!({
1905                    "title": "WeatherResponse",
1906                    "type": "object",
1907                    "properties": {
1908                        "city": { "type": "string" },
1909                        "weather": { "type": "string" }
1910                    },
1911                    "required": ["city", "weather"]
1912                }))
1913                .expect("schema should deserialize"),
1914            ),
1915        };
1916
1917        let openai_request = CompletionRequest::try_from(OpenAIRequestParams {
1918            model: "gpt-4o-mini".to_string(),
1919            request,
1920            strict_tools: false,
1921            tool_result_array_content: false,
1922        })
1923        .expect("request conversion should succeed");
1924
1925        let serialized =
1926            serde_json::to_value(openai_request).expect("serialization should succeed");
1927
1928        assert!(
1929            serialized.get("response_format").is_some(),
1930            "follow-up turn should restore response_format: {serialized:?}"
1931        );
1932    }
1933
1934    #[test]
1935    fn deserialize_llama_cpp_tool_call() {
1936        let request = r#"{
1937            "choices": [{
1938                "finish_reason": "tool_calls",
1939                "index": 0,
1940                "message": {
1941                    "role": "assistant",
1942                    "content": "",
1943                    "tool_calls": [{ "type": "function", "function": { "name": "hello_world", "arguments": { "city": "Paris" } }, "id": "xxx" }]
1944                }
1945            }],
1946            "created": 0,
1947            "model": "gpt-4o-mini",
1948            "system_fingerprint": "fp_xxx",
1949            "object": "chat.completion",
1950            "usage": { "completion_tokens": 13, "prompt_tokens": 255, "total_tokens": 268 },
1951            "id": "xxx"
1952        }
1953        "#;
1954        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
1955
1956        let ApiResponse::Ok(response) = response else {
1957            panic!("expected successful completion response");
1958        };
1959        assert_eq!(response.choices.len(), 1);
1960
1961        let Message::Assistant { tool_calls, .. } = &response.choices[0].message else {
1962            panic!("expected assistant message");
1963        };
1964        assert_eq!(tool_calls.len(), 1);
1965        assert_eq!(tool_calls[0].id, "xxx");
1966        assert_eq!(tool_calls[0].function.name, "hello_world");
1967        assert_eq!(
1968            tool_calls[0].function.arguments,
1969            serde_json::json!({"city": "Paris"})
1970        );
1971    }
1972
1973    #[test]
1974    fn deserialize_openai_stringified_tool_call() {
1975        let request = r#"{
1976            "choices": [{
1977                "finish_reason": "tool_calls",
1978                "index": 0,
1979                "message": {
1980                    "role": "assistant",
1981                    "content": "",
1982                    "tool_calls": [{ "type": "function", "function": { "name": "hello_world", "arguments": "{\"city\":\"Paris\"}" }, "id": "xxx" }]
1983                }
1984            }],
1985            "created": 0,
1986            "model": "gpt-4o-mini",
1987            "system_fingerprint": "fp_xxx",
1988            "object": "chat.completion",
1989            "usage": { "completion_tokens": 13, "prompt_tokens": 255, "total_tokens": 268 },
1990            "id": "xxx"
1991        }
1992        "#;
1993        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
1994
1995        let ApiResponse::Ok(response) = response else {
1996            panic!("expected successful completion response");
1997        };
1998        assert_eq!(response.choices.len(), 1);
1999
2000        let Message::Assistant { tool_calls, .. } = &response.choices[0].message else {
2001            panic!("expected assistant message");
2002        };
2003        assert_eq!(tool_calls.len(), 1);
2004        assert_eq!(tool_calls[0].id, "xxx");
2005        assert_eq!(tool_calls[0].function.name, "hello_world");
2006        assert_eq!(
2007            tool_calls[0].function.arguments,
2008            serde_json::json!({"city": "Paris"})
2009        );
2010    }
2011
2012    #[test]
2013    fn deserialize_llama_cpp_response_with_reasoning_content() {
2014        let request = r#"
2015        {
2016            "choices": [
2017                {
2018                    "finish_reason": "stop",
2019                    "index": 0,
2020                    "message": {
2021                        "role": "assistant",
2022                        "content": "",
2023                        "reasoning_content": "Now I understand the structure better. I need to: ..."
2024                    }
2025                }
2026            ],
2027            "created": 1776750378,
2028            "model": "unsloth/Qwen3.6-35B-A3B-GGUF:Q8_0",
2029            "system_fingerprint": "fp_xxx",
2030            "object": "chat.completion",
2031            "usage": {
2032                "completion_tokens": 920,
2033                "prompt_tokens": 27806,
2034                "total_tokens": 28726,
2035                "prompt_tokens_details": { "cached_tokens": 18698 }
2036            },
2037            "id": "chatcmpl-xxxx",
2038            "timings": {
2039                "cache_n": 18698,
2040                "prompt_n": 9108,
2041                "prompt_ms": 226645.81,
2042                "prompt_per_token_ms": 24.884256697408873,
2043                "prompt_per_second": 40.186050648807495,
2044                "predicted_n": 920,
2045                "predicted_ms": 177167.955,
2046                "predicted_per_token_ms": 192.57386413043477,
2047                "predicted_per_second": 5.192812661860888
2048            }
2049        }
2050        "#;
2051        let response = serde_json::from_str::<ApiResponse<CompletionResponse>>(request).unwrap();
2052        let ApiResponse::Ok(response) = response else {
2053            panic!("expected successful completion response");
2054        };
2055
2056        let response: completion::CompletionResponse<CompletionResponse> =
2057            response.try_into().unwrap();
2058
2059        assert_eq!(response.choice.len(), 1);
2060
2061        let completion::message::AssistantContent::Reasoning(reasoning) = response.choice.first()
2062        else {
2063            panic!("expected assistant content to be reasoning");
2064        };
2065        assert_eq!(
2066            reasoning.first_text(),
2067            Some("Now I understand the structure better. I need to: ...")
2068        );
2069    }
2070
2071    #[test]
2072    fn pdf_base64_document_serializes_as_file_content_part() {
2073        let doc = message::UserContent::Document(message::Document {
2074            data: DocumentSourceKind::Base64("JVBERi0xLjQK".into()),
2075            media_type: Some(message::DocumentMediaType::PDF),
2076            additional_params: None,
2077        });
2078        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2079        let json = serde_json::to_value(&converted).expect("serialize");
2080
2081        assert_eq!(json["type"], "file");
2082        assert_eq!(
2083            json["file"]["file_data"],
2084            "data:application/pdf;base64,JVBERi0xLjQK"
2085        );
2086        assert_eq!(json["file"]["filename"], "document.pdf");
2087        assert!(json["file"].get("file_id").is_none());
2088    }
2089
2090    #[test]
2091    fn file_id_document_serializes_as_file_content_part() {
2092        let doc = message::UserContent::Document(message::Document {
2093            data: DocumentSourceKind::FileId("file_abc".into()),
2094            media_type: None,
2095            additional_params: None,
2096        });
2097        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2098        let json = serde_json::to_value(&converted).expect("serialize");
2099
2100        assert_eq!(json["type"], "file");
2101        assert_eq!(json["file"]["file_id"], "file_abc");
2102        assert!(json["file"].get("file_data").is_none());
2103    }
2104
2105    #[test]
2106    fn base64_image_without_detail_defaults_to_auto() {
2107        let image = message::UserContent::Image(message::Image {
2108            data: DocumentSourceKind::Base64("iVBORw0KGgo=".into()),
2109            media_type: Some(message::ImageMediaType::PNG),
2110            detail: None,
2111            additional_params: None,
2112        });
2113        let converted: UserContent = image.try_into().expect("conversion should succeed");
2114        let UserContent::Image { image_url } = converted else {
2115            panic!("expected image content");
2116        };
2117
2118        assert_eq!(image_url.url, "data:image/png;base64,iVBORw0KGgo=");
2119        assert_eq!(image_url.detail, ImageDetail::Auto);
2120    }
2121
2122    // Regression guard: callers passing markdown/plain text wrapped in
2123    // `UserContent::Document` should keep getting flattened to `text`.
2124    #[test]
2125    fn non_pdf_document_still_serializes_as_text() {
2126        let doc = message::UserContent::Document(message::Document {
2127            data: DocumentSourceKind::String("# Markdown".into()),
2128            media_type: None,
2129            additional_params: None,
2130        });
2131        let converted: UserContent = doc.try_into().expect("conversion should succeed");
2132        let json = serde_json::to_value(&converted).expect("serialize");
2133
2134        assert_eq!(json["type"], "text");
2135        assert_eq!(json["text"], "# Markdown");
2136    }
2137
2138    #[test]
2139    fn pdf_url_document_returns_conversion_error() {
2140        let doc = message::UserContent::Document(message::Document {
2141            data: DocumentSourceKind::Url("https://example.com/x.pdf".into()),
2142            media_type: Some(message::DocumentMediaType::PDF),
2143            additional_params: None,
2144        });
2145        let res: Result<UserContent, _> = doc.try_into();
2146        assert!(matches!(
2147            res,
2148            Err(message::MessageError::ConversionError(_))
2149        ));
2150    }
2151
2152    #[test]
2153    fn pdf_raw_document_returns_conversion_error() {
2154        let doc = message::UserContent::Document(message::Document {
2155            data: DocumentSourceKind::Raw(b"%PDF-1.4\n".to_vec()),
2156            media_type: Some(message::DocumentMediaType::PDF),
2157            additional_params: None,
2158        });
2159        let res: Result<UserContent, _> = doc.try_into();
2160        assert!(matches!(
2161            res,
2162            Err(message::MessageError::ConversionError(_))
2163        ));
2164    }
2165
2166    #[test]
2167    fn file_user_content_deserializes_from_wire_json() {
2168        let raw = r#"{"type":"file","file":{"file_data":"data:application/pdf;base64,AAAA","filename":"x.pdf"}}"#;
2169        let parsed: UserContent = serde_json::from_str(raw).expect("deserialize");
2170        let UserContent::File { file } = parsed else {
2171            panic!("expected File variant");
2172        };
2173        assert_eq!(
2174            file.file_data.as_deref(),
2175            Some("data:application/pdf;base64,AAAA")
2176        );
2177        assert_eq!(file.filename.as_deref(), Some("x.pdf"));
2178        assert!(file.file_id.is_none());
2179    }
2180
2181    #[test]
2182    fn file_variant_round_trips_back_to_pdf_document() {
2183        let wire = UserContent::File {
2184            file: FileData {
2185                file_data: Some("data:application/pdf;base64,QUJD".to_string()),
2186                file_id: None,
2187                filename: Some("document.pdf".to_string()),
2188            },
2189        };
2190        let rig: message::UserContent = wire.into();
2191        let message::UserContent::Document(doc) = rig else {
2192            panic!("expected Document");
2193        };
2194        assert_eq!(doc.media_type, Some(message::DocumentMediaType::PDF));
2195        assert!(matches!(doc.data, DocumentSourceKind::Base64(ref b) if b == "QUJD"));
2196    }
2197
2198    #[test]
2199    fn file_variant_with_file_id_only_round_trips_to_document_file_id() {
2200        let wire = UserContent::File {
2201            file: FileData {
2202                file_data: None,
2203                file_id: Some("file_abc".to_string()),
2204                filename: None,
2205            },
2206        };
2207        let rig: message::UserContent = wire.into();
2208        let message::UserContent::Document(doc) = rig else {
2209            panic!("expected Document");
2210        };
2211        assert_eq!(doc.media_type, None);
2212        assert!(matches!(doc.data, DocumentSourceKind::FileId(ref id) if id == "file_abc"));
2213
2214        let converted: UserContent = message::UserContent::Document(doc)
2215            .try_into()
2216            .expect("conversion should succeed");
2217        let json = serde_json::to_value(&converted).expect("serialize");
2218
2219        assert_eq!(json["type"], "file");
2220        assert_eq!(json["file"]["file_id"], "file_abc");
2221        assert!(json["file"].get("file_data").is_none());
2222    }
2223
2224    // Guards against `OneOrMany::many` flattening at the User content site:
2225    // a mixed text + PDF message must produce one User message with both parts.
2226    #[test]
2227    fn mixed_text_and_pdf_user_message_produces_two_content_parts() {
2228        let user = message::Message::User {
2229            content: OneOrMany::many(vec![
2230                message::UserContent::text("What is in this PDF?"),
2231                message::UserContent::Document(message::Document {
2232                    data: DocumentSourceKind::Base64("JVBERi0K".into()),
2233                    media_type: Some(message::DocumentMediaType::PDF),
2234                    additional_params: None,
2235                }),
2236            ])
2237            .expect("non-empty content"),
2238        };
2239        let converted: Vec<Message> = user.try_into().expect("conversion should succeed");
2240        assert_eq!(converted.len(), 1);
2241        let Message::User { content, .. } = &converted[0] else {
2242            panic!("expected user message");
2243        };
2244        let parts: Vec<&UserContent> = content.iter().collect();
2245        assert_eq!(parts.len(), 2);
2246        assert!(matches!(parts[0], UserContent::Text { .. }));
2247        assert!(matches!(parts[1], UserContent::File { .. }));
2248    }
2249}