pub struct Message {
pub role: Role,
pub content: Vec<ContentPart>,
}Expand description
A single turn in the conversation.
content is always a list of ContentParts. Codecs that accept a string
shorthand are responsible for collapsing a single ContentPart::Text to a
bare string at encode time.
Message is an open data carrier: codec/runnable internals
pattern-match exhaustively against the IR, so the type stays
constructable via struct-literal syntax. New IR signals land as
additional ContentPart variants (which ContentPart’s
#[non_exhaustive] covers) or as new helpers on Message.
Fields§
§role: RoleWho authored this message.
content: Vec<ContentPart>One or more content parts. Empty content is permitted (some providers emit empty assistant messages alongside tool calls).
Implementations§
Source§impl Message
impl Message
Sourcepub fn new(role: Role, content: Vec<ContentPart>) -> Self
pub fn new(role: Role, content: Vec<ContentPart>) -> Self
Construct a message with a typed role + content list. Use the
role-specific helpers (user / assistant / system / tool_*)
for the common single-text-part cases; reach for new when
assembling multi-part content (multimodal, tool-use blocks, etc.).
Sourcepub fn user(text: impl Into<String>) -> Self
pub fn user(text: impl Into<String>) -> Self
Convenience: user message with a single text part.
Sourcepub fn assistant(text: impl Into<String>) -> Self
pub fn assistant(text: impl Into<String>) -> Self
Convenience: assistant message with a single text part.
Sourcepub fn system(text: impl Into<String>) -> Self
pub fn system(text: impl Into<String>) -> Self
Convenience: system message with a single text part.
Sourcepub fn tool_result(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: impl Into<String>,
) -> Self
pub fn tool_result( tool_use_id: impl Into<String>, name: impl Into<String>, output: impl Into<String>, ) -> Self
Convenience: tool message wrapping a tool’s reply to a
prior ContentPart::ToolUse. Mirrors LangChain’s
ToolMessage(content=…, tool_call_id=…, name=…) shape so
the RAG / agent loop reads as a one-line append after each
tool call instead of hand-constructing a Message { role: Role::Tool, content: vec![ContentPart::ToolResult { … }] }.
Both tool_use_id and name are required: Anthropic /
OpenAI / Bedrock correlate by id, but Gemini’s
functionResponse keys by name. Carrying both keeps
the IR provider-neutral so a single agent harness works
across all four codecs without per-vendor adaptation.
output accepts any string-like — for structured payloads,
use Self::tool_result_json and the codec will emit native
JSON (or stringify with a LossyEncode warning if the
provider lacks structured tool-result support).
Sourcepub fn tool_result_json(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: Value,
) -> Self
pub fn tool_result_json( tool_use_id: impl Into<String>, name: impl Into<String>, output: Value, ) -> Self
Same as Self::tool_result but carries a structured JSON
payload. Use when the tool returns objects/arrays the model
should reason over without re-parsing a stringified blob.
Sourcepub fn tool_error(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: impl Into<String>,
) -> Self
pub fn tool_error( tool_use_id: impl Into<String>, name: impl Into<String>, output: impl Into<String>, ) -> Self
Same as Self::tool_result but flagged as an error reply.
Anthropic and Bedrock surface the is_error flag natively;
other codecs prefix the text or emit a LossyEncode warning.
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 Eq for Message
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.