pub enum LlmResponse {
Start {
message_id: String,
},
Text {
chunk: String,
},
Reasoning {
chunk: String,
},
EncryptedReasoning {
id: String,
content: String,
},
ToolRequestStart {
id: String,
name: String,
},
ToolRequestArg {
id: String,
chunk: String,
},
ToolRequestComplete {
tool_call: ToolCallRequest,
},
Done {
stop_reason: Option<StopReason>,
},
Error {
message: String,
},
Usage {
tokens: TokenUsage,
},
}Expand description
A streaming response event from an LLM provider.
Providers return an LlmResponseStream that yields a sequence of these events as the model generates its response. The stream follows a defined lifecycle:
Start -> (Text | Reasoning | EncryptedReasoning | ToolRequest*)* -> Usage -> Done§Text generation
Start– Stream opened, contains themessage_id.Text– A chunk of generated text.Reasoning– A chunk of the model’s chain-of-thought reasoning (visible summary).EncryptedReasoning– Opaque encrypted reasoning content (model-specific, can be replayed to the same model in future turns).
§Tool calling
Tool calls are streamed in three phases:
ToolRequestStart– The model begins a tool call (id+name).ToolRequestArg– Argument JSON arrives in chunks (sameid).ToolRequestComplete– The fully assembledToolCallRequestis ready to execute.
Multiple tool calls can be interleaved in a single response.
§Completion
Usage– Token usage statistics, carried as aTokenUsage. Required:input_tokens,output_tokens. Optional sub-categories (each filled in by the providers that expose it):cache_read_tokens– prompt tokens served from cache (Anthropic,Bedrock,OpenAIChat/Responses/Codex,OpenAI-compat).cache_creation_tokens– prompt tokens written to cache on this turn (Anthropiccache_creation_input_tokens,Bedrockcache_write_input_tokens,OpenRoutercache_write_tokens).input_audio_tokens– input audio tokens, subset ofinput_tokens(OpenAIChat,OpenRouter).input_video_tokens– input video tokens, subset ofinput_tokens(OpenRouter).reasoning_tokens– reasoning/thinking tokens, subset ofoutput_tokens(OpenAIChat/Responses/Codex,OpenRouter).output_audio_tokens– output audio tokens, subset ofoutput_tokens(OpenAIChat,OpenRouter).accepted_prediction_tokens– subset ofoutput_tokensfor predicted-output models (OpenAIChat,OpenRouter).rejected_prediction_tokens– subset ofoutput_tokensfor predicted-output models (OpenAIChat,OpenRouter).
Done– Stream complete, with an optionalStopReason.Error– An error occurred during generation.
§Convenience constructors
Each variant has a corresponding constructor method (e.g. LlmResponse::text("chunk"), LlmResponse::done()) to simplify test fixture construction.
Variants§
Start
Text
Reasoning
EncryptedReasoning
ToolRequestStart
ToolRequestArg
ToolRequestComplete
Fields
§
tool_call: ToolCallRequestDone
Fields
§
stop_reason: Option<StopReason>Error
Usage
Fields
§
tokens: TokenUsageImplementations§
Source§impl LlmResponse
impl LlmResponse
pub fn start(message_id: &str) -> Self
pub fn text(chunk: &str) -> Self
pub fn reasoning(chunk: &str) -> Self
pub fn encrypted_reasoning(id: &str, encrypted: &str) -> Self
pub fn tool_request_start(id: &str, name: &str) -> Self
pub fn tool_request_arg(id: &str, chunk: &str) -> Self
pub fn tool_request_complete(id: &str, name: &str, arguments: &str) -> Self
pub fn usage(input_tokens: u32, output_tokens: u32) -> Self
pub fn done() -> Self
pub fn done_with_stop_reason(stop_reason: StopReason) -> Self
Trait Implementations§
Source§impl Clone for LlmResponse
impl Clone for LlmResponse
Source§fn clone(&self) -> LlmResponse
fn clone(&self) -> LlmResponse
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LlmResponse
impl Debug for LlmResponse
Source§impl<'de> Deserialize<'de> for LlmResponse
impl<'de> Deserialize<'de> for LlmResponse
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>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl PartialEq for LlmResponse
impl PartialEq for LlmResponse
Source§impl Serialize for LlmResponse
impl Serialize for LlmResponse
impl Eq for LlmResponse
impl StructuralPartialEq for LlmResponse
Auto Trait Implementations§
impl Freeze for LlmResponse
impl RefUnwindSafe for LlmResponse
impl Send for LlmResponse
impl Sync for LlmResponse
impl Unpin for LlmResponse
impl UnsafeUnpin for LlmResponse
impl UnwindSafe for LlmResponse
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
Mutably borrows from an owned value. Read more
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§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
Compare self to
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.