pub struct ChatCompletionResponseMessage {
pub role: String,
pub content: Option<String>,
pub tool_calls: Option<Vec<ChatCompletionMessageToolCall>>,
pub function_call: Option<FunctionCall>,
pub content_blocks: Option<Vec<ContentBlock>>,
}Expand description
Chat completion response message
Fields§
§role: StringThe role of the message author
content: Option<String>The contents of the message
tool_calls: Option<Vec<ChatCompletionMessageToolCall>>Tool calls made by the model
function_call: Option<FunctionCall>Deprecated: Function call (use tool_calls instead)
content_blocks: Option<Vec<ContentBlock>>Content blocks (for providers with strict_openai_compliance=false)
Implementations§
Source§impl ChatCompletionResponseMessage
impl ChatCompletionResponseMessage
Sourcepub fn deserialize_content<T>(&self) -> Result<Option<T>>where
T: DeserializeOwned,
pub fn deserialize_content<T>(&self) -> Result<Option<T>>where
T: DeserializeOwned,
Deserializes the JSON content of the message into a custom type.
This is useful when using structured outputs with JSON schema, allowing you to deserialize the response into your custom type.
Returns Ok(None) if the message has no content, Ok(Some(T)) if deserialization succeeds,
or Err if the content is not valid JSON for the target type.
§Example
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyResponse {
message: String,
count: i32,
}
let response_message = /* ... get from API response ... */;
if let Some(parsed) = response_message.deserialize_content::<MyResponse>()? {
println!("Message: {}, Count: {}", parsed.message, parsed.count);
}Sourcepub fn deserialize_markdown<T>(&self) -> Result<Option<T>>where
T: DeserializeOwned,
pub fn deserialize_markdown<T>(&self) -> Result<Option<T>>where
T: DeserializeOwned,
Deserializes JSON from markdown code blocks within the message content.
This method searches for markdown code blocks ( ```json or ```) within the content
and extracts the JSON from within them. It can handle content that has text before or
after the code block. If no code block is found, it attempts to parse the entire content.
Returns Ok(None) if the message has no content, Ok(Some(T)) if deserialization succeeds,
or Err if the content is not valid JSON for the target type.
§Example
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct MyResponse {
message: String,
count: i32,
}
// Content might be: "Here's your data:\n```json\n{\"message\": \"Hello\", \"count\": 42}\n```\nEnjoy!"
let response_message = /* ... get from API response ... */;
if let Some(parsed) = response_message.deserialize_markdown::<MyResponse>()? {
println!("Message: {}, Count: {}", parsed.message, parsed.count);
}Trait Implementations§
Source§impl Clone for ChatCompletionResponseMessage
impl Clone for ChatCompletionResponseMessage
Source§fn clone(&self) -> ChatCompletionResponseMessage
fn clone(&self) -> ChatCompletionResponseMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more