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: RoleAuthor 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
impl ChatMessage
Sourcepub fn user_with_images(text: impl Into<String>, image_urls: &[String]) -> Self
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.
Sourcepub fn assistant(content: impl Into<String>) -> Self
pub fn assistant(content: impl Into<String>) -> Self
Build an assistant message with plain text.
Sourcepub fn tool_result(
tool_call_id: impl Into<String>,
name: impl Into<String>,
content: impl Into<String>,
) -> Self
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.
Sourcepub 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
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).
Sourcepub fn tool_calls(&self) -> &[ToolCall]
pub fn tool_calls(&self) -> &[ToolCall]
The tool calls on this message, or an empty slice.
Sourcepub fn with_meta(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn with_meta(self, key: impl Into<String>, value: impl Into<String>) -> Self
Attach a metadata key/value, returning self (builder style).
Sourcepub fn with_metas(self, pairs: &[(String, String)]) -> Self
pub fn with_metas(self, pairs: &[(String, String)]) -> Self
Attach several metadata key/values, returning self.
Trait Implementations§
Source§impl Clone for ChatMessage
impl Clone for ChatMessage
Source§fn clone(&self) -> ChatMessage
fn clone(&self) -> ChatMessage
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more