pub struct ChatResponse {
pub id: String,
pub model: String,
pub choices: Vec<ChatChoice>,
pub usage: UsageBlock,
}Expand description
The top-level response from POST /v1/chat/completions.
Why: Wraps the choices array and usage block so callers receive a single
typed value from LlmClient::chat.
What: id is the response ID; choices contains the generated turns;
usage carries token accounting. model (#1475 bug 2) is the RESOLVED
model slug the provider actually served the request with — OpenRouter
(and the underlying OpenAI-compatible API) echoes this at the top level
of every response, which can differ from the request’s model field
when routing/fallback resolves to a concrete backing model (e.g. an
:auto or multi-model routing slug). #[serde(default)] so fixtures
and mocks that omit it still deserialise — see
Self::resolved_model for the fallback this enables.
Test: chat_response_deserialises_fixture,
resolved_model_prefers_response_model_over_requested,
resolved_model_falls_back_to_requested_when_absent.
Fields§
§id: StringOpaque response identifier from the provider.
model: StringThe resolved model slug the provider actually used (#1475 bug 2).
Empty when the response omits it (older fixtures, some mocks) —
callers should use Self::resolved_model rather than this field
directly.
choices: Vec<ChatChoice>Generated choices (one per n; we always request n = 1).
usage: UsageBlockToken accounting for this request.
Implementations§
Source§impl ChatResponse
impl ChatResponse
Sourcepub fn first_text(&self) -> Option<String>
pub fn first_text(&self) -> Option<String>
Extract the first choice’s text content, if any.
Why: The vast majority of calls want the assistant’s text response; this
convenience method avoids repetitive indexing at every call site.
What: Returns choices[0].message.content.clone() or None when the
response has no choices or the content is absent.
Test: chat_response_first_text_content.
Sourcepub fn first_tool_calls(&self) -> &[ToolCall]
pub fn first_tool_calls(&self) -> &[ToolCall]
Extract the first choice’s tool calls.
Why: Tool-call workflows need quick access to the emitted calls without
indexing into nested fields each time.
What: Returns a reference to choices[0].message.tool_calls, or an empty
slice when there are no choices.
Test: chat_response_first_tool_calls.
Sourcepub fn finish_reason(&self) -> Option<&str>
pub fn finish_reason(&self) -> Option<&str>
Extract the finish reason from the first choice.
Why: Callers need the stop condition to decide whether to continue the
tool-call loop or surface the response.
What: Returns choices[0].finish_reason.as_deref() or None.
Test: chat_response_deserialises_fixture.
Sourcepub fn token_usage(self) -> TokenUsage
pub fn token_usage(self) -> TokenUsage
Convert the response’s usage block into TokenUsage.
Why: PerfCollector::record_phase accepts &TokenUsage; this method
keeps the conversion at one place.
What: Delegates to UsageBlock::into_token_usage.
Test: chat_response_usage_into_token_usage.
Sourcepub fn resolved_model<'a>(&'a self, requested: &'a str) -> &'a str
pub fn resolved_model<'a>(&'a self, requested: &'a str) -> &'a str
The model slug to attribute this turn to for recording/pricing
(#1475 bug 2): the RESOLVED model the provider reports, falling back
to the originally requested slug when the response didn’t carry
one.
Why: run_task::recorder::RecordingLlmClient and any future
transcript recorder must record what the provider actually ran, not
merely what was asked for — a routing/fallback slug (:auto, a
multi-model alias) can resolve to a different concrete model.
What: Returns &self.model when non-empty, else requested.
Test: resolved_model_prefers_response_model_over_requested,
resolved_model_falls_back_to_requested_when_absent.
Trait Implementations§
Source§impl Clone for ChatResponse
impl Clone for ChatResponse
Source§fn clone(&self) -> ChatResponse
fn clone(&self) -> ChatResponse
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ChatResponse
impl Debug for ChatResponse
Source§impl<'de> Deserialize<'de> for ChatResponse
impl<'de> Deserialize<'de> for ChatResponse
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>,
Auto Trait Implementations§
impl Freeze for ChatResponse
impl RefUnwindSafe for ChatResponse
impl Send for ChatResponse
impl Sync for ChatResponse
impl Unpin for ChatResponse
impl UnsafeUnpin for ChatResponse
impl UnwindSafe for ChatResponse
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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>
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>
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 more