pub struct Message {
pub role: MessageRole,
pub content: MessageContent,
pub reasoning: Option<String>,
pub reasoning_details: Option<Vec<Value>>,
pub tool_calls: Option<Vec<ToolCall>>,
pub tool_call_id: Option<String>,
pub phase: Option<AssistantPhase>,
pub origin_tool: Option<String>,
pub metadata: Option<MessageMetadata>,
pub clear_at: Option<MessageClearAt>,
}Expand description
Universal message structure supporting both text and image content
Fields§
§role: MessageRole§content: MessageContentContent can be a string (for backward compatibility) or an array of content parts
reasoning: Option<String>§reasoning_details: Option<Vec<Value>>§tool_calls: Option<Vec<ToolCall>>§tool_call_id: Option<String>§phase: Option<AssistantPhase>Optional assistant-only phase metadata used by OpenAI Responses workflows.
origin_tool: Option<String>Optional origin tool name for tracking which tool generated this message Used in tool-aware context retention to preserve results from recently-active tools
metadata: Option<MessageMetadata>Optional per-message metadata (timestamp, importance, compression status, etc.).
clear_at: Option<MessageClearAt>Optional provider-specific clear scope for a mid-conversation system message.
Implementations§
Source§impl Message
impl Message
Sourcepub fn estimate_tokens(&self) -> usize
pub fn estimate_tokens(&self) -> usize
Estimate the number of tokens in this message (rough approximation).
Sourcepub fn user_with_parts(content_parts: Vec<ContentPart>) -> Self
pub fn user_with_parts(content_parts: Vec<ContentPart>) -> Self
Create a user message with multiple content parts (text and images)
Sourcepub fn assistant_with_parts(content_parts: Vec<ContentPart>) -> Self
pub fn assistant_with_parts(content_parts: Vec<ContentPart>) -> Self
Create an assistant message with multiple content parts
Sourcepub fn assistant_with_tools(content: String, tool_calls: Vec<ToolCall>) -> Self
pub fn assistant_with_tools(content: String, tool_calls: Vec<ToolCall>) -> Self
Create an assistant message with tool calls Based on OpenAI Cookbook patterns for function calling
Sourcepub fn assistant_with_tools_and_parts(
content_parts: Vec<ContentPart>,
tool_calls: Vec<ToolCall>,
) -> Self
pub fn assistant_with_tools_and_parts( content_parts: Vec<ContentPart>, tool_calls: Vec<ToolCall>, ) -> Self
Create an assistant message with tool calls and multiple content parts
Sourcepub fn assistant_with_tools_and_reasoning(
content: String,
tool_calls: Vec<ToolCall>,
reasoning_details: Option<Vec<Value>>,
) -> Self
pub fn assistant_with_tools_and_reasoning( content: String, tool_calls: Vec<ToolCall>, reasoning_details: Option<Vec<Value>>, ) -> Self
Create an assistant message with tool calls and reasoning details Used for preserving reasoning state in multi-turn conversations
Sourcepub fn turn_scoped_system(content: String) -> Self
pub fn turn_scoped_system(content: String) -> Self
Create a system message whose lifecycle is scoped to the current turn. Provider/model routes that advertise native support let Anthropic clear it when the next user turn arrives. Other routes receive the same text as an ordinary system/history directive after the runtime removes the Anthropic-only lifecycle field; canonical history retains the typed marker for replay fidelity.
Sourcepub fn tool_response(tool_call_id: String, content: String) -> Self
pub fn tool_response(tool_call_id: String, content: String) -> Self
Create a tool response message This follows the exact pattern from OpenAI Cookbook:
{
"role": "tool",
"tool_call_id": "call_123",
"content": "Function result"
}Sourcepub fn tool_response_with_name(
tool_call_id: String,
_function_name: String,
content: String,
) -> Self
pub fn tool_response_with_name( tool_call_id: String, _function_name: String, content: String, ) -> Self
Create a tool response message with function name (for compatibility) Some providers might need the function name in addition to tool_call_id
Sourcepub fn tool_response_with_origin(
tool_call_id: String,
content: String,
origin_tool: String,
) -> Self
pub fn tool_response_with_origin( tool_call_id: String, content: String, origin_tool: String, ) -> Self
Create a tool response message with origin tool tracking The origin_tool field helps with tool-aware context retention
Sourcepub async fn user_with_local_image<P: AsRef<Path>>(
file_path: P,
) -> Result<Self, Error>
pub async fn user_with_local_image<P: AsRef<Path>>( file_path: P, ) -> Result<Self, Error>
Create a user message with image from a local file
Sourcepub async fn user_with_text_and_local_image<P: AsRef<Path>>(
text: String,
file_path: P,
) -> Result<Self, Error>
pub async fn user_with_text_and_local_image<P: AsRef<Path>>( text: String, file_path: P, ) -> Result<Self, Error>
Create a user message with text and a local image
Sourcepub fn with_reasoning(self, reasoning: Option<String>) -> Self
pub fn with_reasoning(self, reasoning: Option<String>) -> Self
Attach provider-visible reasoning trace for archival without affecting payloads.
Sourcepub fn with_tool_calls(self, tool_calls: Vec<ToolCall>) -> Self
pub fn with_tool_calls(self, tool_calls: Vec<ToolCall>) -> Self
Attach tool calls to this message.
Sourcepub fn with_reasoning_details(
self,
reasoning_details: Option<Vec<Value>>,
) -> Self
pub fn with_reasoning_details( self, reasoning_details: Option<Vec<Value>>, ) -> Self
Attach reasoning details for providers that support structured reasoning
Sourcepub fn with_phase(self, phase: Option<AssistantPhase>) -> Self
pub fn with_phase(self, phase: Option<AssistantPhase>) -> Self
Attach assistant phase metadata for providers that support it.
Sourcepub fn with_metadata(self, metadata: MessageMetadata) -> Self
pub fn with_metadata(self, metadata: MessageMetadata) -> Self
Attach per-message metadata.
Sourcepub fn get_tool_calls(&self) -> Option<&[ToolCall]>
pub fn get_tool_calls(&self) -> Option<&[ToolCall]>
Get the tool calls if present
Sourcepub fn is_tool_response(&self) -> bool
pub fn is_tool_response(&self) -> bool
Check if this is a tool response message
Sourcepub fn get_text_content(&self) -> Cow<'_, str>
pub fn get_text_content(&self) -> Cow<'_, str>
Get the text content of the message (for backward compatibility)
Sourcepub fn has_images(&self) -> bool
pub fn has_images(&self) -> bool
Check if this message contains images
Sourcepub fn get_images(&self) -> Vec<&ContentPart>
pub fn get_images(&self) -> Vec<&ContentPart>
Get all images in this message
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Forget for Message
impl Freeze for Messagewhere
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
PhantomData<ContentPart>: Freeze,
PhantomData<Value>: Freeze,
PhantomData<ToolCall>: Freeze,
impl Move for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Messagewhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<ContentPart>: UnsafeUnpin,
PhantomData<Value>: UnsafeUnpin,
PhantomData<ToolCall>: UnsafeUnpin,
impl UnwindSafe for Message
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.