pub struct ChatResponse {
pub content: Vec<ContentBlock>,
pub usage: Usage,
pub stop_reason: StopReason,
pub model: String,
pub metadata: HashMap<String, Value>,
}Expand description
A complete response from a model.
Returned by Provider::generate. For
streaming responses, accumulate StreamEvents
instead.
Fields§
§content: Vec<ContentBlock>The content blocks produced by the model.
usage: UsageToken counts for this request/response pair.
stop_reason: StopReasonWhy the model stopped generating.
model: StringThe model identifier that actually served the request (may differ from the requested model if the provider performed routing).
metadata: HashMap<String, Value>Provider-specific metadata (e.g., request IDs, cache info).
Contents are provider-specific. See each provider crate’s docs for
the keys it populates. Common keys include "request_id".
Implementations§
Source§impl ChatResponse
impl ChatResponse
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an empty response with no content.
Useful for timeout or error cases where no LLM response was received.
Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Returns the text of the first ContentBlock::Text block, if any.
This is a convenience for the common case where you only care
about the model’s text output. For responses that may contain
multiple text blocks, iterate content directly.
Sourcepub fn tool_calls(&self) -> Vec<&ToolCall>
pub fn tool_calls(&self) -> Vec<&ToolCall>
Returns references to all ToolCalls in the response.
Returns an empty Vec when the response contains no tool calls.
This is the primary accessor for implementing tool-use loops.
For allocation-free iteration, use tool_calls_iter.
Sourcepub fn tool_calls_iter(&self) -> impl Iterator<Item = &ToolCall>
pub fn tool_calls_iter(&self) -> impl Iterator<Item = &ToolCall>
Returns an iterator over all ToolCalls in the response.
Prefer this over tool_calls to avoid allocation.
Sourcepub fn into_tool_calls(self) -> Vec<ToolCall>
pub fn into_tool_calls(self) -> Vec<ToolCall>
Consumes the response and returns all ToolCalls.
This is more efficient than tool_calls when you
need owned ToolCall values and won’t use the response afterward.
Non-tool-call content blocks are discarded.
§Example
let response = ChatResponse {
content: vec![
ContentBlock::Text("Let me help".into()),
ContentBlock::ToolCall(ToolCall {
id: "call_1".into(),
name: "search".into(),
arguments: json!({"query": "rust"}),
}),
],
stop_reason: StopReason::ToolUse,
usage: Usage::default(),
model: "test".into(),
metadata: Default::default(),
};
let calls = response.into_tool_calls();
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].name, "search");Sourcepub fn partition_content(self) -> (Vec<ToolCall>, Vec<ContentBlock>)
pub fn partition_content(self) -> (Vec<ToolCall>, Vec<ContentBlock>)
Consumes the response content and partitions it into tool calls and other blocks.
Returns (tool_calls, other_content) where other_content contains all
non-tool-call blocks (Text, Image, ToolResult, etc.) suitable for building
an assistant message in a tool loop.
This is more efficient than calling both tool_calls
and filtering content separately, as it processes the content in a single pass.
Trait Implementations§
Source§impl Clone for ChatResponse
impl Clone for ChatResponse
Source§fn clone(&self) -> ChatResponse
fn clone(&self) -> ChatResponse
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more