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
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:
- Execute the tool
- Add the result to the conversation
- 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
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
impl Event
Sourcepub fn tool_call_delta(
id: impl Into<String>,
name: impl Into<String>,
arguments_fragment: impl Into<String>,
) -> Self
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.
Sourcepub fn tool_call(
id: impl Into<String>,
name: impl Into<String>,
arguments: Value,
) -> Self
pub fn tool_call( id: impl Into<String>, name: impl Into<String>, arguments: Value, ) -> Self
Creates a tool call event.
Sourcepub fn builtin_result(
tool: impl Into<String>,
result: impl Into<String>,
) -> Self
pub fn builtin_result( tool: impl Into<String>, result: impl Into<String>, ) -> Self
Creates a built-in tool result event.
Sourcepub fn as_reasoning(&self) -> Option<&str>
pub fn as_reasoning(&self) -> Option<&str>
Returns the reasoning content if this is a Reasoning event.
Sourcepub const fn as_tool_call(&self) -> Option<&ToolCall>
pub const fn as_tool_call(&self) -> Option<&ToolCall>
Returns the tool call if this is a ToolCall event.
Sourcepub const fn is_tool_call(&self) -> bool
pub const fn is_tool_call(&self) -> bool
Returns true if this is a tool call event.