Skip to main content

Event

Enum Event 

Source
pub enum Event {
    Text(String),
    Reasoning(String),
    ToolCallDelta {
        id: String,
        name: String,
        arguments_fragment: String,
    },
    ToolCall(ToolCall),
    ReasoningState(ReasoningState),
    BuiltInToolResult {
        tool: String,
        result: String,
    },
    Usage(Usage),
}
Expand description

Events emitted by a language model during response generation.

This is the primary output type from [LanguageModel::respond]. Consumers should handle each event type appropriately.

§Example

use futures_lite::StreamExt;

let mut stream = model.respond(request);
while let Some(event) = stream.next().await {
    match event? {
        Event::Text(text) => print!("{}", text),
        Event::Reasoning(thought) => eprintln!("[thinking] {}", thought),
        Event::ToolCall(call) => {
            // Execute tool and continue conversation
            let result = execute_tool(&call).await;
            // ... add result to messages and continue
        }
        Event::BuiltInToolResult { tool, result } => {
            println!("[{}] {}", tool, result);
        }
        Event::Usage(usage) => {
            println!("Tokens used: {:?}", usage.total_tokens);
        }
    }
}

Variants§

§

Text(String)

Visible text chunk from the model.

These chunks should be concatenated to form the complete response.

§

Reasoning(String)

Internal reasoning or thinking from reasoning models.

Not all models emit reasoning. For models like Claude with extended thinking or OpenAI’s o1, this contains the model’s internal thought process. This is for observability only - it’s not part of the conversation.

§

ToolCallDelta

Incremental tool call assembly progress.

Emitted as the model streams a tool call’s name and arguments. Consumers can use this to show early UI feedback (e.g., tool name and partial description) before the full arguments are available.

A final Event::ToolCall is always emitted once the tool call is fully assembled; consumers that don’t need incremental progress can ignore ToolCallDelta entirely.

Fields

§id: String

Tool call identifier (available from the first delta).

§name: String

Tool name (available from the first delta for Claude; may arrive incrementally for OpenAI).

§arguments_fragment: String

Partial JSON arguments accumulated so far.

§

ToolCall(ToolCall)

Request to execute a tool.

Important: The core crate does NOT execute tool calls. This event indicates the model wants to use a tool. The consumer (typically an agent) should:

  1. Execute the tool
  2. Add the result to the conversation
  3. Continue the conversation with the model
§

ReasoningState(ReasoningState)

Opaque reasoning state that must be replayed to the provider.

Distinct from Event::Reasoning, which is display text: state carries no meaning for the reader and text carries none for the model. They are emitted independently, and a provider may emit either alone — Claude with display: "omitted" produces state with no text at all.

Consumers assembling the next request must collect these into the assistant message they build; dropping them degrades multi-turn tool use.

§

BuiltInToolResult

Result from a provider’s built-in tool.

Some providers have native tools that are executed server-side:

  • Gemini: Google Search grounding
  • OpenAI: Code interpreter, file search
  • Claude: (future built-in tools)

These are already executed - this event contains the result.

Fields

§tool: String

Name of the built-in tool that was executed.

§result: String

Result from the tool execution.

§

Usage(Usage)

Token usage and cost information.

Emitted at the end of a response stream with usage statistics. Use this to track token consumption and costs across requests.

Implementations§

Source§

impl Event

Source

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

Creates a text event.

Source

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

Creates a reasoning event.

Source

pub fn tool_call_delta( id: impl Into<String>, name: impl Into<String>, arguments_fragment: impl Into<String>, ) -> Self

Creates a tool call delta event for incremental streaming.

Source

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

Creates a tool call event.

Source

pub fn builtin_result( tool: impl Into<String>, result: impl Into<String>, ) -> Self

Creates a built-in tool result event.

Source

pub const fn usage(usage: Usage) -> Self

Creates a usage event.

Source

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

Returns the text content if this is a Text event.

Source

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

Returns the reasoning content if this is a Reasoning event.

Source

pub const fn as_tool_call(&self) -> Option<&ToolCall>

Returns the tool call if this is a ToolCall event.

Source

pub const fn is_text(&self) -> bool

Returns true if this is a text event.

Source

pub const fn is_tool_call(&self) -> bool

Returns true if this is a tool call event.

Source

pub const fn as_usage(&self) -> Option<&Usage>

Returns the usage info if this is a Usage event.

Source

pub const fn is_usage(&self) -> bool

Returns true if this is a usage event.

Trait Implementations§

Source§

impl Clone for Event

Source§

fn clone(&self) -> Event

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 Event

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Event

§

impl RefUnwindSafe for Event

§

impl Send for Event

§

impl Sync for Event

§

impl Unpin for Event

§

impl UnsafeUnpin for Event

§

impl UnwindSafe for Event

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

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

fn try_from(value: U) -> Result<T, !>

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.