Skip to main content

ChatMessage

Struct ChatMessage 

Source
pub struct ChatMessage {
    pub role: Role,
    pub content: Option<String>,
    pub content_parts: Option<Vec<Value>>,
    pub tool_calls: Option<Vec<ToolCall>>,
    pub tool_call_id: Option<String>,
    pub name: Option<String>,
    pub metadata: BTreeMap<String, String>,
}
Expand description

A single message in a conversation.

The field layout mirrors the OpenAI chat-completions wire format so it serializes directly with no conversion layer — which is exactly what OpenRouter and other OpenAI-compatible gateways consume.

Fields§

§role: Role

Author of the message.

§content: Option<String>

Text content. None for assistant turns that are pure tool calls, or when Self::content_parts carries multimodal content instead.

§content_parts: Option<Vec<Value>>

Multimodal content parts (e.g. {"type":"image_url", ...} alongside {"type":"text", ...}). When present, these are serialized as the wire content array (taking precedence over Self::content) — this is how images/vision input reach a vision model.

§tool_calls: Option<Vec<ToolCall>>

Tool calls requested by an assistant turn.

§tool_call_id: Option<String>

For tool messages: the id of the ToolCall this is a result for.

§name: Option<String>

Optional name (used by some providers for tool messages).

§metadata: BTreeMap<String, String>

Source-format provenance/labels that have no slot in the OpenAI wire shape (e.g. Codex phase, turn_id; Claude promptSource, isMeta, sourceToolAssistantUUID). Never serialized — kept only for fidelity and inspection so loading a session doesn’t silently discard this signal. (Never serialized — the custom Serialize impl omits it.)

Implementations§

Source§

impl ChatMessage

Source

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

Build a system message.

Source

pub fn user_with_images(text: impl Into<String>, image_urls: &[String]) -> Self

Build a user message with multimodal content — leading text plus one image_url part per URL (an https://… link or a data: URL). This is how images are passed to a vision model.

Source

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

Build a user message.

Source

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

Build an assistant message with plain text.

Source

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

Build a tool result message tied to a specific tool call.

Source

pub fn tool_result_with_image( tool_call_id: impl Into<String>, name: impl Into<String>, notice: impl Into<String>, data_url: impl Into<String>, ) -> Self

P4c (COMPOSABLE-HARNESS-DESIGN.md §1.2 core.tools.read_file multimodal / view_image): a tool result that carries an image content block alongside a short text notice — content_parts (image passthrough) rather than a plain string, so the model actually sees the image. data_url is a full data:image/...; base64,... URL (see tools::builtins::image_tool_result).

Source

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

The tool calls on this message, or an empty slice.

Source

pub fn with_meta(self, key: impl Into<String>, value: impl Into<String>) -> Self

Attach a metadata key/value, returning self (builder style).

Source

pub fn with_metas(self, pairs: &[(String, String)]) -> Self

Attach several metadata key/values, returning self.

Trait Implementations§

Source§

impl Clone for ChatMessage

Source§

fn clone(&self) -> ChatMessage

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 ChatMessage

Source§

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

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

impl<'de> Deserialize<'de> for ChatMessage

Source§

fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error>

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

impl From<&ChatMessage> for NativeTurn

Source§

fn from(msg: &ChatMessage) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for ChatMessage

Source§

fn eq(&self, other: &ChatMessage) -> 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 ChatMessage

Source§

fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error>

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

impl StructuralPartialEq for ChatMessage

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more