Skip to main content

MessagePart

Enum MessagePart 

Source
pub enum MessagePart {
    Text {
        text: String,
    },
    Image {
        source: ImageSource,
    },
    ToolCall {
        id: String,
        name: String,
        input: Value,
    },
    ToolResult {
        call_id: String,
        output: ToolContent,
        is_error: Option<bool>,
    },
}
Expand description

A part of message content within a Message.

Messages can contain multiple types of content interleaved in a single response: plain text, base64-encoded images, tool-call invocations by the assistant, and tool results. Each variant corresponds to a possible part type with tagged JSON serialization.

§Serialization

Uses #[serde(tag = "type")] with renamed variants to produce JSON objects like {"type": "text", "text": "..."} matching the loopctl message schema.

§Construction

Use the constructors text, tool_call, and tool_result rather than building variants directly.

§Example

use loopctl::message::{MessagePart};
let text_part = MessagePart::text("Hello");
let tool_part = MessagePart::tool_call("id1", "search", serde_json::json!({"q": "test"}));
let result_part = MessagePart::tool_result("id1", "found 3 results", false);

Variants§

§

Text

Plain text content.

Contains a UTF-8 text string generated by the model or provided by the user.

Fields

§text: String

The text content of this part.

May contain multiple lines. For streaming responses, this is the fully assembled text after all streaming deltas have been concatenated.

§

Image

A base64-encoded image part.

Images can be included in user messages for multimodal models. The ImageSource contains the MIME type and base64 data.

Serialized as {"type":"image","source":{...}}.

Note: image parts are only valid in Role::User messages; the API rejects them in assistant messages.

Fields

§source: ImageSource

The image data and metadata.

Contains the MIME type, encoding type, and base64-encoded image bytes. See ImageSource for details.

§

ToolCall

A tool-call invocation by the assistant.

When the model decides to call a tool, it emits a part with the tool’s ID, name, and JSON input. The framework then executes the tool and sends the result back as a ToolResult block.

The correlation between a ToolCall and its ToolResult is done via the id field, which must be unique within a single conversation turn.

Fields

§id: String

Unique identifier for this tool invocation.

Assigned by the API. Used to correlate the tool-call part with the corresponding ToolResult part in the next message.

§name: String

The name of the tool being invoked.

Must match one of the tool names provided in the request’s tools parameter.

§input: Value

The JSON input provided by the model for the tool.

Structure depends on the tool’s input schema. May be any JSON value (Object, Array, String, etc.).

§

ToolResult

The result of a tool-call invocation.

Sent back to the model with Role::User (per API convention) to provide the output of a tool execution. Contains the tool-call ID for correlation, the output content, and an optional error flag.

After the agent executes a tool, it wraps the output in this variant and appends it to the conversation history so the model can reason about the output.

Fields

§call_id: String

The ID of the tool-call part this result corresponds to.

Must match the id from the original ToolCall block.

§output: ToolContent

The output returned by the tool.

Can be a simple string or a multipart response with multiple parts. See ToolContent.

§is_error: Option<bool>

Whether this result represents an error.

Some(true) indicates the tool invocation failed. When None or Some(false), the result is a success.

Implementations§

Source§

impl MessagePart

Source

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

Create a text part.

Constructor for the Text variant. Accepts any type that implements Into<String>.

§Arguments
  • text — The text content.
§Returns

A MessagePart::Text variant.

§Example
use loopctl::message::{MessagePart};
let part = MessagePart::text("Hello, world!");
assert!(part.is_text());
Source

pub fn tool_call( id: impl Into<String>, name: impl Into<String>, input: Value, ) -> Self

Create a tool-call part.

Used when constructing an assistant message that invokes a tool. The id must be unique within the conversation to allow correlation with the corresponding tool result.

§Arguments
  • id — Unique identifier for this tool invocation.
  • name — The name of the tool to invoke.
  • input — The JSON input parameters for the tool.
§Returns

A MessagePart::ToolCall variant.

§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_call(
    "tool_abc",
    "read_file",
    serde_json::json!({"path": "/tmp/test.txt"}),
);
assert!(part.is_tool_call());
Source

pub fn tool_result( call_id: impl Into<String>, output: impl Into<ToolContent>, is_error: bool, ) -> Self

Create a tool-result part.

Used when sending the output of a tool execution back to the model. The call_id must match the id from the original ToolCall block.

§Arguments
  • call_id — The ID of the tool invocation this output is for.
  • output — The tool output (string or multipart).
  • is_error — Whether the tool invocation failed.
§Returns

A MessagePart::ToolResult variant.

§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_result(
    "tool_abc",
    "file contents here",
    false,
);
assert!(part.is_tool_result());
Source

pub const fn is_text(&self) -> bool

Returns true if this is a Text block.

Allows filtering or pattern matching on parts without a full match expression.

§Example
use loopctl::message::{MessagePart};
let part = MessagePart::text("hello");
assert!(part.is_text());
Source

pub const fn is_tool_call(&self) -> bool

Returns true if this is a ToolCall block.

Detects tool invocations in an assistant message to decide whether to enter the tool-execution loop.

§Example
use loopctl::message::{MessagePart};
use serde_json::Value;
let part = MessagePart::tool_call("id", "tool", Value::Null);
assert!(part.is_tool_call());
Source

pub const fn is_tool_result(&self) -> bool

Returns true if this is a ToolResult block.

Identifies tool results in a conversation history.

§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_result("id", "output", false);
assert!(part.is_tool_result());
Source

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

Get the text content if this is a Text block.

Returns Some(&str) for text parts, None for all other variants. Extracts the text from a known-text part.

§Returns

Some(text) if this is a text part, None otherwise.

§Example
use loopctl::message::{MessagePart};
use serde_json::Value;
let part = MessagePart::text("hello");
assert_eq!(part.as_text(), Some("hello"));

let tool = MessagePart::tool_call("id", "tool", Value::Null);
assert_eq!(tool.as_text(), None);

Trait Implementations§

Source§

impl Clone for MessagePart

Source§

fn clone(&self) -> MessagePart

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 MessagePart

Source§

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

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

impl<'de> Deserialize<'de> for MessagePart

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 Serialize for MessagePart

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

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