pub struct ChatMessage {
pub role: ChatRole,
pub content: Vec<ContentBlock>,
}Expand description
A single message in a conversation.
Use the convenience constructors for common cases:
use llm_stack::ChatMessage;
let user = ChatMessage::user("What is 2+2?");
let asst = ChatMessage::assistant("4");
let sys = ChatMessage::system("You are a math tutor.");
let tool = ChatMessage::tool_result("call_123", "4");For multi-block content (images, multiple tool results), construct
the content vec directly.
Fields§
§role: ChatRoleWho sent this message.
content: Vec<ContentBlock>The content blocks that make up the message body.
Implementations§
Source§impl ChatMessage
impl ChatMessage
Sourcepub fn text(role: ChatRole, text: impl Into<String>) -> Self
pub fn text(role: ChatRole, text: impl Into<String>) -> Self
Creates a message with a single ContentBlock::Text block.
Sourcepub fn user(text: impl Into<String>) -> Self
pub fn user(text: impl Into<String>) -> Self
Shorthand for a ChatRole::User text message.
Sourcepub fn assistant(text: impl Into<String>) -> Self
pub fn assistant(text: impl Into<String>) -> Self
Shorthand for a ChatRole::Assistant text message.
Sourcepub fn system(text: impl Into<String>) -> Self
pub fn system(text: impl Into<String>) -> Self
Shorthand for a ChatRole::System text message.
Sourcepub fn tool_result(
tool_call_id: impl Into<String>,
content: impl Into<String>,
) -> Self
pub fn tool_result( tool_call_id: impl Into<String>, content: impl Into<String>, ) -> Self
Creates a ChatRole::Tool message with a single successful
ToolResult.
Use ChatMessage::tool_error when the tool invocation failed.
Sourcepub fn tool_error(
tool_call_id: impl Into<String>,
content: impl Into<String>,
) -> Self
pub fn tool_error( tool_call_id: impl Into<String>, content: impl Into<String>, ) -> Self
Creates a ChatRole::Tool message indicating the tool call
failed. The content should describe the error.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the message has zero content blocks.
This checks the block count only — a message containing a single
ContentBlock::Text("") returns false. Use this to detect
structurally empty messages (no blocks at all).
Sourcepub fn to_json(&self) -> Result<Value, Error>
pub fn to_json(&self) -> Result<Value, Error>
Serializes the message to a stable JSON format.
This format is documented and versioned for persistence use cases (conversation history, checkpointing, etc.). The format is guaranteed to be backward compatible within the same major version.
§Stability Guarantees
- Field names are stable within major versions
- New optional fields may be added in minor versions
- The output is deterministic for the same input
§Example
use llm_stack::ChatMessage;
let msg = ChatMessage::user("Hello, world!");
let json = msg.to_json().expect("serialization should succeed");
assert!(json["content"][0]["text"].as_str() == Some("Hello, world!"));Sourcepub fn from_json(value: &Value) -> Result<Self, Error>
pub fn from_json(value: &Value) -> Result<Self, Error>
Deserializes a message from the stable JSON format.
Accepts JSON produced by to_json. This method is
the inverse of to_json and maintains roundtrip fidelity.
§Example
use llm_stack::ChatMessage;
use serde_json::json;
let json = json!({
"role": "User",
"content": [{"text": "Hello!"}]
});
let msg = ChatMessage::from_json(&json).expect("valid message");
assert_eq!(msg.role, llm_stack::ChatRole::User);Sourcepub fn from_json_owned(value: Value) -> Result<Self, Error>
pub fn from_json_owned(value: Value) -> Result<Self, Error>
Deserializes a message from the stable JSON format, consuming the value.
This is more efficient than from_json when you have
an owned Value that you don’t need afterward, as it avoids cloning.
§Example
use llm_stack::ChatMessage;
use serde_json::json;
let json = json!({
"role": "User",
"content": [{"text": "Hello!"}]
});
let msg = ChatMessage::from_json_owned(json).expect("valid message");
assert_eq!(msg.role, llm_stack::ChatRole::User);Source§impl ChatMessage
impl ChatMessage
Sourcepub fn tool_result_full(result: ToolResult) -> Self
pub fn tool_result_full(result: ToolResult) -> Self
Creates a tool result message from a ToolResult.
Trait Implementations§
Source§impl Clone for ChatMessage
impl Clone for ChatMessage
Source§fn clone(&self) -> ChatMessage
fn clone(&self) -> ChatMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more