Skip to main content

Message

Enum Message 

Source
pub enum Message {
    User {
        content: String,
        attachments: Vec<Attachment>,
    },
    Assistant {
        content: String,
        tool_calls: Vec<ToolCall>,
        reasoning: Vec<ReasoningState>,
    },
    System {
        content: String,
    },
    Tool {
        content: String,
        tool_call_id: String,
    },
}
Expand description

A message in a conversation.

Different message types have different fields:

  • User/System: content with optional attachments
  • Assistant: content with optional tool calls
  • Tool: content with required tool_call_id

Variants§

§

User

User message with content and optional attachments.

Fields

§content: String

Text content of the message.

§attachments: Vec<Attachment>

Attachment URLs (images, documents, etc.)

§

Assistant

Assistant message with content and optional tool calls.

Fields

§content: String

Text content of the message.

§tool_calls: Vec<ToolCall>

Tool calls made by the assistant.

§reasoning: Vec<ReasoningState>

Opaque reasoning state produced with this turn.

Replayed to the provider on the next request. Order is significant — Anthropic rejects a turn whose thinking blocks were reordered or partially dropped — so append rather than rebuild.

§

System

System message with instructions/context.

Fields

§content: String

Text content of the message.

§

Tool

Tool result message.

Fields

§content: String

Result content from the tool.

§tool_call_id: String

ID of the tool call this is responding to.

Implementations§

Source§

impl Message

Source

pub const fn role(&self) -> Role

Returns the message sender role.

Source

pub fn content(&self) -> &str

Returns the text content of the message.

Source

pub fn attachments(&self) -> &[Attachment]

Returns the typed attachments (only for User messages).

Source

pub fn tool_calls(&self) -> &[ToolCall]

Returns tool calls made by the assistant (only for Assistant messages).

Source

pub fn tool_call_id(&self) -> Option<&str>

Returns the tool call ID (only for Tool messages).

Source

pub fn user(content: impl Into<String>) -> Self

Creates a new user message.

Source

pub fn assistant(content: impl Into<String>) -> Self

Creates a new assistant message.

Source

pub fn assistant_with_tool_calls( content: impl Into<String>, tool_calls: Vec<ToolCall>, ) -> Self

Creates an assistant message with tool calls.

Source

pub fn assistant_with_reasoning( content: impl Into<String>, tool_calls: Vec<ToolCall>, reasoning: Vec<ReasoningState>, ) -> Self

Creates an assistant message carrying the reasoning state of its turn.

Use this when rebuilding a conversation for another request: without the reasoning, providers that verify their own thinking see a turn that has lost the reasoning behind its tool calls.

Source

pub fn reasoning(&self) -> &[ReasoningState]

Reasoning state recorded on this message, if any.

Only assistant turns carry reasoning; every other role returns empty.

Source

pub fn system(content: impl Into<String>) -> Self

Creates a new system message.

Source

pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self

Creates a new tool result message.

Source

pub fn with_attachment(self, attachment: Attachment) -> Self

Adds a typed attachment to the message (only works for User messages).

Source

pub fn with_attachments( self, values: impl IntoIterator<Item = Attachment>, ) -> Self

Adds multiple typed attachments to the message.

Source

pub fn with_tool_calls(self, calls: Vec<ToolCall>) -> Self

Adds tool calls to the message (only works for Assistant messages).

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Message

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Message

Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Message

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Message

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.