Skip to main content

rig/providers/cohere/
completion.rs

1use crate::{
2    OneOrMany,
3    completion::{self, CompletionError, GetTokenUsage},
4    http_client::{self, HttpClientExt},
5    json_utils,
6    message::{self, Reasoning, ToolChoice},
7    telemetry::SpanCombinator,
8};
9use std::collections::HashMap;
10
11use super::client::Client;
12use crate::completion::CompletionRequest;
13use crate::providers::cohere::streaming::StreamingCompletionResponse;
14use serde::{Deserialize, Serialize};
15use tracing::{Instrument, Level, enabled, info_span};
16
17#[derive(Debug, Deserialize, Serialize)]
18pub struct CompletionResponse {
19    pub id: String,
20    pub finish_reason: FinishReason,
21    message: Message,
22    #[serde(default)]
23    pub usage: Option<Usage>,
24}
25
26type AssistantMessageParts = (Vec<AssistantContent>, Vec<Citation>, Vec<ToolCall>);
27
28impl CompletionResponse {
29    /// Return that parts of the response for assistant messages w/o dealing with the other variants
30    pub fn message(&self) -> Result<AssistantMessageParts, CompletionError> {
31        let Message::Assistant {
32            content,
33            citations,
34            tool_calls,
35            ..
36        } = self.message.clone()
37        else {
38            return Err(CompletionError::ResponseError(
39                "completion response did not contain an assistant message".into(),
40            ));
41        };
42
43        Ok((content, citations, tool_calls))
44    }
45}
46
47impl crate::telemetry::ProviderResponseExt for CompletionResponse {
48    type OutputMessage = Message;
49    type Usage = Usage;
50
51    fn get_response_id(&self) -> Option<String> {
52        Some(self.id.clone())
53    }
54
55    fn get_response_model_name(&self) -> Option<String> {
56        None
57    }
58
59    fn get_output_messages(&self) -> Vec<Self::OutputMessage> {
60        vec![self.message.clone()]
61    }
62
63    fn get_text_response(&self) -> Option<String> {
64        let Message::Assistant { ref content, .. } = self.message else {
65            return None;
66        };
67
68        let res = content
69            .iter()
70            .filter_map(|x| {
71                if let AssistantContent::Text { text } = x {
72                    Some(text.to_string())
73                } else {
74                    None
75                }
76            })
77            .collect::<Vec<String>>()
78            .join("\n");
79
80        if res.is_empty() { None } else { Some(res) }
81    }
82
83    fn get_usage(&self) -> Option<Self::Usage> {
84        self.usage.clone()
85    }
86}
87
88#[derive(Debug, Deserialize, PartialEq, Eq, Clone, Serialize)]
89#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
90pub enum FinishReason {
91    MaxTokens,
92    StopSequence,
93    Complete,
94    Error,
95    ToolCall,
96}
97
98#[derive(Debug, Deserialize, Clone, Serialize)]
99pub struct Usage {
100    #[serde(default)]
101    pub billed_units: Option<BilledUnits>,
102    #[serde(default)]
103    pub tokens: Option<Tokens>,
104}
105
106impl GetTokenUsage for Usage {
107    fn token_usage(&self) -> Option<crate::completion::Usage> {
108        let mut usage = crate::completion::Usage::new();
109
110        if let Some(ref billed_units) = self.billed_units {
111            usage.input_tokens = billed_units.input_tokens.unwrap_or_default() as u64;
112            usage.output_tokens = billed_units.output_tokens.unwrap_or_default() as u64;
113            usage.total_tokens = usage.input_tokens + usage.output_tokens;
114        }
115
116        Some(usage)
117    }
118}
119
120#[derive(Debug, Deserialize, Clone, Serialize)]
121pub struct BilledUnits {
122    #[serde(default)]
123    pub output_tokens: Option<f64>,
124    #[serde(default)]
125    pub classifications: Option<f64>,
126    #[serde(default)]
127    pub search_units: Option<f64>,
128    #[serde(default)]
129    pub input_tokens: Option<f64>,
130}
131
132#[derive(Debug, Deserialize, Clone, Serialize)]
133pub struct Tokens {
134    #[serde(default)]
135    pub input_tokens: Option<f64>,
136    #[serde(default)]
137    pub output_tokens: Option<f64>,
138}
139
140impl TryFrom<CompletionResponse> for completion::CompletionResponse<CompletionResponse> {
141    type Error = CompletionError;
142
143    fn try_from(response: CompletionResponse) -> Result<Self, Self::Error> {
144        let (content, _, tool_calls) = response.message()?;
145
146        let model_response = if !tool_calls.is_empty() {
147            OneOrMany::many(
148                tool_calls
149                    .into_iter()
150                    .filter_map(|tool_call| {
151                        let ToolCallFunction { name, arguments } = tool_call.function?;
152                        let id = tool_call.id.unwrap_or_else(|| name.clone());
153
154                        Some(completion::AssistantContent::tool_call(id, name, arguments))
155                    })
156                    .collect::<Vec<_>>(),
157            )
158            .map_err(|_| {
159                CompletionError::ResponseError(
160                    "response contained tool call metadata without any callable tool content"
161                        .to_owned(),
162                )
163            })?
164        } else {
165            OneOrMany::many(content.into_iter().map(|content| match content {
166                AssistantContent::Text { text } => completion::AssistantContent::text(text),
167                AssistantContent::Thinking { thinking } => {
168                    completion::AssistantContent::Reasoning(Reasoning::new(&thinking))
169                }
170            }))
171            .map_err(|_| {
172                CompletionError::ResponseError(
173                    "Response contained no message or tool call (empty)".to_owned(),
174                )
175            })?
176        };
177
178        let usage = response
179            .usage
180            .as_ref()
181            .and_then(|usage| usage.tokens.as_ref())
182            .map(|tokens| {
183                let input_tokens = tokens.input_tokens.unwrap_or(0.0);
184                let output_tokens = tokens.output_tokens.unwrap_or(0.0);
185
186                completion::Usage {
187                    input_tokens: input_tokens as u64,
188                    output_tokens: output_tokens as u64,
189                    total_tokens: (input_tokens + output_tokens) as u64,
190                    cached_input_tokens: 0,
191                    cache_creation_input_tokens: 0,
192                }
193            })
194            .unwrap_or_default();
195
196        Ok(completion::CompletionResponse {
197            choice: model_response,
198            usage,
199            raw_response: response,
200            message_id: None,
201        })
202    }
203}
204
205#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
206pub struct Document {
207    pub id: String,
208    pub data: HashMap<String, serde_json::Value>,
209}
210
211impl From<completion::Document> for Document {
212    fn from(document: completion::Document) -> Self {
213        let mut data: HashMap<String, serde_json::Value> = HashMap::new();
214
215        // We use `.into()` here explicitly since the `document.additional_props` type will likely
216        //  evolve into `serde_json::Value` in the future.
217        document
218            .additional_props
219            .into_iter()
220            .for_each(|(key, value)| {
221                data.insert(key, value.into());
222            });
223
224        data.insert("text".to_string(), document.text.into());
225
226        Self {
227            id: document.id,
228            data,
229        }
230    }
231}
232
233#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
234pub struct ToolCall {
235    #[serde(default)]
236    pub id: Option<String>,
237    #[serde(default)]
238    pub r#type: Option<ToolType>,
239    #[serde(default)]
240    pub function: Option<ToolCallFunction>,
241}
242
243#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
244pub struct ToolCallFunction {
245    pub name: String,
246    #[serde(with = "json_utils::stringified_json")]
247    pub arguments: serde_json::Value,
248}
249
250#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
251#[serde(rename_all = "lowercase")]
252pub enum ToolType {
253    #[default]
254    Function,
255}
256
257#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
258pub struct Tool {
259    pub r#type: ToolType,
260    pub function: Function,
261}
262
263#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
264pub struct Function {
265    pub name: String,
266    #[serde(default)]
267    pub description: Option<String>,
268    pub parameters: serde_json::Value,
269}
270
271impl From<completion::ToolDefinition> for Tool {
272    fn from(tool: completion::ToolDefinition) -> Self {
273        Self {
274            r#type: ToolType::default(),
275            function: Function {
276                name: tool.name,
277                description: Some(tool.description),
278                parameters: tool.parameters,
279            },
280        }
281    }
282}
283
284#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
285#[serde(tag = "role", rename_all = "lowercase")]
286pub enum Message {
287    User {
288        content: OneOrMany<UserContent>,
289    },
290
291    Assistant {
292        #[serde(default)]
293        content: Vec<AssistantContent>,
294        #[serde(default)]
295        citations: Vec<Citation>,
296        #[serde(default)]
297        tool_calls: Vec<ToolCall>,
298        #[serde(default)]
299        tool_plan: Option<String>,
300    },
301
302    Tool {
303        content: OneOrMany<ToolResultContent>,
304        tool_call_id: String,
305    },
306
307    System {
308        content: String,
309    },
310}
311
312#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
313#[serde(tag = "type", rename_all = "lowercase")]
314pub enum UserContent {
315    Text { text: String },
316    ImageUrl { image_url: ImageUrl },
317}
318
319#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
320#[serde(tag = "type", rename_all = "lowercase")]
321pub enum AssistantContent {
322    Text { text: String },
323    Thinking { thinking: String },
324}
325
326#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
327pub struct ImageUrl {
328    pub url: String,
329}
330
331#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
332pub enum ToolResultContent {
333    Text { text: String },
334    Document { document: Document },
335}
336
337#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
338pub struct Citation {
339    #[serde(default)]
340    pub start: Option<u32>,
341    #[serde(default)]
342    pub end: Option<u32>,
343    #[serde(default)]
344    pub text: Option<String>,
345    #[serde(rename = "type")]
346    pub citation_type: Option<CitationType>,
347    #[serde(default)]
348    pub sources: Vec<Source>,
349}
350
351#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
352#[serde(tag = "type", rename_all = "lowercase")]
353pub enum Source {
354    Document {
355        id: Option<String>,
356        document: Option<serde_json::Map<String, serde_json::Value>>,
357    },
358    Tool {
359        id: Option<String>,
360        tool_output: Option<serde_json::Map<String, serde_json::Value>>,
361    },
362}
363
364#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
365#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
366pub enum CitationType {
367    TextContent,
368    Plan,
369}
370
371impl TryFrom<message::Message> for Vec<Message> {
372    type Error = message::MessageError;
373
374    fn try_from(message: message::Message) -> Result<Self, Self::Error> {
375        Ok(match message {
376            message::Message::User { content } => content
377                .into_iter()
378                .map(|content| match content {
379                    message::UserContent::Text(message::Text { text }) => Ok(Message::User {
380                        content: OneOrMany::one(UserContent::Text { text }),
381                    }),
382                    message::UserContent::ToolResult(message::ToolResult {
383                        id, content, ..
384                    }) => Ok(Message::Tool {
385                        tool_call_id: id,
386                        content: content.try_map(|content| match content {
387                            message::ToolResultContent::Text(text) => {
388                                Ok(ToolResultContent::Text { text: text.text })
389                            }
390                            _ => Err(message::MessageError::ConversionError(
391                                "Only text tool result content is supported by Cohere".to_owned(),
392                            )),
393                        })?,
394                    }),
395                    _ => Err(message::MessageError::ConversionError(
396                        "Only text content is supported by Cohere".to_owned(),
397                    )),
398                })
399                .collect::<Result<Vec<_>, _>>()?,
400            message::Message::System { content } => {
401                vec![Message::System { content }]
402            }
403            message::Message::Assistant { content, .. } => {
404                let mut text_content = vec![];
405                let mut tool_calls = vec![];
406
407                for content in content.into_iter() {
408                    match content {
409                        message::AssistantContent::Text(message::Text { text }) => {
410                            text_content.push(AssistantContent::Text { text });
411                        }
412                        message::AssistantContent::ToolCall(message::ToolCall {
413                            id,
414                            function:
415                                message::ToolFunction {
416                                    name, arguments, ..
417                                },
418                            ..
419                        }) => {
420                            tool_calls.push(ToolCall {
421                                id: Some(id),
422                                r#type: Some(ToolType::Function),
423                                function: Some(ToolCallFunction {
424                                    name,
425                                    arguments: serde_json::to_value(arguments).unwrap_or_default(),
426                                }),
427                            });
428                        }
429                        message::AssistantContent::Reasoning(reasoning) => {
430                            let thinking = reasoning.display_text();
431                            text_content.push(AssistantContent::Thinking { thinking });
432                        }
433                        message::AssistantContent::Image(_) => {
434                            return Err(message::MessageError::ConversionError(
435                                "Cohere currently doesn't support images.".to_owned(),
436                            ));
437                        }
438                    }
439                }
440
441                vec![Message::Assistant {
442                    content: text_content,
443                    citations: vec![],
444                    tool_calls,
445                    tool_plan: None,
446                }]
447            }
448        })
449    }
450}
451
452impl TryFrom<Message> for message::Message {
453    type Error = message::MessageError;
454
455    fn try_from(message: Message) -> Result<Self, Self::Error> {
456        match message {
457            Message::User { content } => Ok(message::Message::User {
458                content: content.map(|content| match content {
459                    UserContent::Text { text } => {
460                        message::UserContent::Text(message::Text { text })
461                    }
462                    UserContent::ImageUrl { image_url } => {
463                        message::UserContent::image_url(image_url.url, None, None)
464                    }
465                }),
466            }),
467            Message::Assistant {
468                content,
469                tool_calls,
470                ..
471            } => {
472                let mut content = content
473                    .into_iter()
474                    .map(|content| match content {
475                        AssistantContent::Text { text } => message::AssistantContent::text(text),
476                        AssistantContent::Thinking { thinking } => {
477                            message::AssistantContent::Reasoning(Reasoning::new(&thinking))
478                        }
479                    })
480                    .collect::<Vec<_>>();
481
482                content.extend(tool_calls.into_iter().filter_map(|tool_call| {
483                    let ToolCallFunction { name, arguments } = tool_call.function?;
484
485                    Some(message::AssistantContent::tool_call(
486                        tool_call.id.unwrap_or_else(|| name.clone()),
487                        name,
488                        arguments,
489                    ))
490                }));
491
492                let content = OneOrMany::many(content).map_err(|_| {
493                    message::MessageError::ConversionError(
494                        "Expected either text content or tool calls".to_string(),
495                    )
496                })?;
497
498                Ok(message::Message::Assistant { id: None, content })
499            }
500            Message::Tool {
501                content,
502                tool_call_id,
503            } => {
504                let content = content.try_map(|content| {
505                    Ok(match content {
506                        ToolResultContent::Text { text } => message::ToolResultContent::text(text),
507                        ToolResultContent::Document { document } => {
508                            message::ToolResultContent::text(
509                                serde_json::to_string(&document.data).map_err(|e| {
510                                    message::MessageError::ConversionError(
511                                        format!("Failed to convert tool result document content into text: {e}"),
512                                    )
513                                })?,
514                            )
515                        }
516                    })
517                })?;
518
519                Ok(message::Message::User {
520                    content: OneOrMany::one(message::UserContent::tool_result(
521                        tool_call_id,
522                        content,
523                    )),
524                })
525            }
526            Message::System { content } => Ok(message::Message::user(content)),
527        }
528    }
529}
530
531#[derive(Clone)]
532pub struct CompletionModel<T = reqwest::Client> {
533    pub(crate) client: Client<T>,
534    pub model: String,
535}
536
537#[derive(Debug, Serialize, Deserialize)]
538pub(super) struct CohereCompletionRequest {
539    model: String,
540    pub messages: Vec<Message>,
541    documents: Vec<crate::completion::Document>,
542    #[serde(skip_serializing_if = "Option::is_none")]
543    temperature: Option<f64>,
544    #[serde(skip_serializing_if = "Vec::is_empty")]
545    tools: Vec<Tool>,
546    #[serde(skip_serializing_if = "Option::is_none")]
547    tool_choice: Option<ToolChoice>,
548    #[serde(flatten, skip_serializing_if = "Option::is_none")]
549    pub additional_params: Option<serde_json::Value>,
550}
551
552impl TryFrom<(&str, CompletionRequest)> for CohereCompletionRequest {
553    type Error = CompletionError;
554
555    fn try_from((model, req): (&str, CompletionRequest)) -> Result<Self, Self::Error> {
556        if req.output_schema.is_some() {
557            tracing::warn!("Structured outputs currently not supported for Cohere");
558        }
559
560        let model = req.model.clone().unwrap_or_else(|| model.to_string());
561        let mut partial_history = vec![];
562        if let Some(docs) = req.normalized_documents() {
563            partial_history.push(docs);
564        }
565        partial_history.extend(req.chat_history);
566
567        let mut full_history: Vec<Message> = req.preamble.map_or_else(Vec::new, |preamble| {
568            vec![Message::System { content: preamble }]
569        });
570
571        full_history.extend(
572            partial_history
573                .into_iter()
574                .map(message::Message::try_into)
575                .collect::<Result<Vec<Vec<Message>>, _>>()?
576                .into_iter()
577                .flatten()
578                .collect::<Vec<_>>(),
579        );
580
581        let tool_choice = if let Some(tool_choice) = req.tool_choice {
582            if !matches!(tool_choice, ToolChoice::Auto) {
583                Some(tool_choice)
584            } else {
585                return Err(CompletionError::RequestError(
586                    "\"auto\" is not an allowed tool_choice value in the Cohere API".into(),
587                ));
588            }
589        } else {
590            None
591        };
592
593        Ok(Self {
594            model: model.to_string(),
595            messages: full_history,
596            documents: req.documents,
597            temperature: req.temperature,
598            tools: req.tools.into_iter().map(Tool::from).collect::<Vec<_>>(),
599            tool_choice,
600            additional_params: req.additional_params,
601        })
602    }
603}
604
605impl<T> CompletionModel<T>
606where
607    T: HttpClientExt,
608{
609    pub fn new(client: Client<T>, model: impl Into<String>) -> Self {
610        Self {
611            client,
612            model: model.into(),
613        }
614    }
615}
616
617impl<T> completion::CompletionModel for CompletionModel<T>
618where
619    T: HttpClientExt + Clone + 'static,
620{
621    type Response = CompletionResponse;
622    type StreamingResponse = StreamingCompletionResponse;
623    type Client = Client<T>;
624
625    fn make(client: &Self::Client, model: impl Into<String>) -> Self {
626        Self::new(client.clone(), model.into())
627    }
628
629    async fn completion(
630        &self,
631        completion_request: completion::CompletionRequest,
632    ) -> Result<completion::CompletionResponse<CompletionResponse>, CompletionError> {
633        let request = CohereCompletionRequest::try_from((self.model.as_ref(), completion_request))?;
634
635        let llm_span = if tracing::Span::current().is_disabled() {
636            info_span!(
637            target: "rig::completions",
638            "chat",
639            gen_ai.operation.name = "chat",
640            gen_ai.provider.name = "cohere",
641            gen_ai.request.model = self.model,
642            gen_ai.response.id = tracing::field::Empty,
643            gen_ai.response.model = self.model,
644            gen_ai.usage.output_tokens = tracing::field::Empty,
645            gen_ai.usage.input_tokens = tracing::field::Empty,
646            gen_ai.usage.cache_read.input_tokens = tracing::field::Empty,
647            )
648        } else {
649            tracing::Span::current()
650        };
651
652        if enabled!(Level::TRACE) {
653            tracing::trace!(
654                "Cohere completion request: {}",
655                serde_json::to_string_pretty(&request)?
656            );
657        }
658
659        let req_body = serde_json::to_vec(&request)?;
660
661        let req = self
662            .client
663            .post("/v2/chat")?
664            .body(req_body)
665            .map_err(|e| CompletionError::HttpError(e.into()))?;
666
667        async {
668            let response = self
669                .client
670                .send::<_, bytes::Bytes>(req)
671                .await
672                .map_err(|e| http_client::Error::Instance(e.into()))?;
673
674            let status = response.status();
675            let body = response.into_body().into_future().await?.to_owned();
676
677            if status.is_success() {
678                let json_response: CompletionResponse = serde_json::from_slice(&body)?;
679                let span = tracing::Span::current();
680                span.record_token_usage(&json_response.usage);
681                span.record_response_metadata(&json_response);
682
683                if enabled!(Level::TRACE) {
684                    tracing::trace!(
685                        target: "rig::completions",
686                        "Cohere completion response: {}",
687                        serde_json::to_string_pretty(&json_response)?
688                    );
689                }
690
691                let completion: completion::CompletionResponse<CompletionResponse> =
692                    json_response.try_into()?;
693                Ok(completion)
694            } else {
695                Err(CompletionError::ProviderError(
696                    String::from_utf8_lossy(&body).to_string(),
697                ))
698            }
699        }
700        .instrument(llm_span)
701        .await
702    }
703
704    async fn stream(
705        &self,
706        request: CompletionRequest,
707    ) -> Result<
708        crate::streaming::StreamingCompletionResponse<Self::StreamingResponse>,
709        CompletionError,
710    > {
711        CompletionModel::stream(self, request).await
712    }
713}
714#[cfg(test)]
715mod tests {
716    use super::*;
717    use serde_path_to_error::deserialize;
718
719    #[test]
720    fn test_deserialize_completion_response() {
721        let json_data = r#"
722        {
723            "id": "abc123",
724            "message": {
725                "role": "assistant",
726                "tool_plan": "I will use the subtract tool to find the difference between 2 and 5.",
727                "tool_calls": [
728                        {
729                            "id": "subtract_sm6ps6fb6y9f",
730                            "type": "function",
731                            "function": {
732                                "name": "subtract",
733                                "arguments": "{\"x\":5,\"y\":2}"
734                            }
735                        }
736                    ]
737                },
738                "finish_reason": "TOOL_CALL",
739                "usage": {
740                "billed_units": {
741                    "input_tokens": 78,
742                    "output_tokens": 27
743                },
744                "tokens": {
745                    "input_tokens": 1028,
746                    "output_tokens": 63
747                }
748            }
749        }
750        "#;
751
752        let mut deserializer = serde_json::Deserializer::from_str(json_data);
753        let result: Result<CompletionResponse, _> = deserialize(&mut deserializer);
754
755        let response = result.unwrap();
756        let (_, citations, tool_calls) = response.message().expect("assistant message");
757        let CompletionResponse {
758            id,
759            finish_reason,
760            usage,
761            ..
762        } = response;
763
764        assert_eq!(id, "abc123");
765        assert_eq!(finish_reason, FinishReason::ToolCall);
766
767        let Usage {
768            billed_units,
769            tokens,
770        } = usage.unwrap();
771        let BilledUnits {
772            input_tokens: billed_input_tokens,
773            output_tokens: billed_output_tokens,
774            ..
775        } = billed_units.unwrap();
776        let Tokens {
777            input_tokens,
778            output_tokens,
779        } = tokens.unwrap();
780
781        assert_eq!(billed_input_tokens.unwrap(), 78.0);
782        assert_eq!(billed_output_tokens.unwrap(), 27.0);
783        assert_eq!(input_tokens.unwrap(), 1028.0);
784        assert_eq!(output_tokens.unwrap(), 63.0);
785
786        assert!(citations.is_empty());
787        assert_eq!(tool_calls.len(), 1);
788
789        let ToolCallFunction { name, arguments } = tool_calls[0].function.clone().unwrap();
790
791        assert_eq!(name, "subtract");
792        assert_eq!(arguments, serde_json::json!({"x": 5, "y": 2}));
793    }
794
795    #[test]
796    fn test_convert_completion_message_to_message_and_back() {
797        let completion_message = completion::Message::User {
798            content: OneOrMany::one(completion::message::UserContent::Text(
799                completion::message::Text {
800                    text: "Hello, world!".to_string(),
801                },
802            )),
803        };
804
805        let messages: Vec<Message> = completion_message.clone().try_into().unwrap();
806        let _converted_back: Vec<completion::Message> = messages
807            .into_iter()
808            .map(|msg| msg.try_into().unwrap())
809            .collect::<Vec<_>>();
810    }
811
812    #[test]
813    fn test_convert_message_to_completion_message_and_back() {
814        let message = Message::User {
815            content: OneOrMany::one(UserContent::Text {
816                text: "Hello, world!".to_string(),
817            }),
818        };
819
820        let completion_message: completion::Message = message.clone().try_into().unwrap();
821        let _converted_back: Vec<Message> = completion_message.try_into().unwrap();
822    }
823}