Skip to main content

ChatResponse

Struct ChatResponse 

Source
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: String

Opaque response identifier from the provider.

§model: String

The 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: UsageBlock

Token accounting for this request.

Implementations§

Source§

impl ChatResponse

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> ChatResponse

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 ChatResponse

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ChatResponse

Source§

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 Serialize for ChatResponse

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more