#[non_exhaustive]pub struct CompletionResponse {
pub choice: Vec<AssistantContent>,
pub usage: Usage,
pub origin: Origin,
pub error: Option<String>,
pub aborted: Option<String>,
pub provider_request_id: Option<String>,
pub raw: Value,
/* private fields */
}Expand description
Assistant content and normalized completion metadata. The choice may be
empty, including for truncated or filtered turns. Provider-specific data is
available through Self::raw without retaining a concrete model type.
A response goes straight back into the conversation as the assistant
turn: history.extend(response.message()).
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.choice: Vec<AssistantContent>Assistant content returned by the provider, one block per provider output item, in provider order. Possibly empty.
usage: UsageTokens used during prompting and responding
origin: OriginThe wire, provider and requested model that produced the response, with the model and response id the provider reported.
error: Option<String>The provider’s report that the turn failed, such as a refusal’s
explanation. The turn’s message then ends in StopReason::Error
and is never replayed.
aborted: Option<String>Why the reply stopped before the provider ended it, when the caller
stopped reading: the turn’s message then ends in
StopReason::Aborted and is never replayed.
provider_request_id: Option<String>Request identifier from HTTP headers or SDK metadata, not the body’s
response ID. None when the provider reports none.
raw: ValueProvider response document for typed inspection through deserialization. Parsed wire types may omit unmodeled fields. This data does not override normalized fields; callers constructing responses must supply it.
Implementations§
Source§impl CompletionResponse
impl CompletionResponse
Sourcepub fn new(
choice: Vec<AssistantContent>,
usage: Usage,
origin: Origin,
raw: Value,
) -> Self
pub fn new( choice: Vec<AssistantContent>, usage: Usage, origin: Origin, raw: Value, ) -> Self
Create a response from its required parts; optional metadata starts
unset. raw is the provider’s own document for this response,
serialized; see Self::raw.
Sourcepub fn extras_lossy<P: ProviderExtension>(&self) -> P::Extras
pub fn extras_lossy<P: ProviderExtension>(&self) -> P::Extras
P’s typed view of Self::raw, or the empty view (every field
None) when it cannot be read: when another provider produced the
reply, or when raw does not hold P’s extras. A decode failure is
logged at debug; use Self::extras to tell the cases apart.
Sourcepub fn response_id(&self) -> Option<&str>
pub fn response_id(&self) -> Option<&str>
The provider’s response id, when it sent one.
Sourcepub fn finish_reason(&self) -> Option<FinishReason>
pub fn finish_reason(&self) -> Option<FinishReason>
Why the model stopped generating, when the provider reported it.
Sourcepub fn accepts_unknown_finish_reasons(&self) -> bool
pub fn accepts_unknown_finish_reasons(&self) -> bool
Whether a finish reason outside the normalized vocabulary ends the
turn as a normal stop, as the request asked
(CompletionRequest::accept_unknown_finish_reasons).
Sourcepub fn accept_unknown_finish_reasons(self, accept: bool) -> Self
pub fn accept_unknown_finish_reasons(self, accept: bool) -> Self
Accept, or refuse, FinishReason::Other as a normal stop in
Self::stop. Filtered content and a reported failure still fail.
Sourcepub fn stop(&self) -> StopReason
pub fn stop(&self) -> StopReason
How the turn ended, for history. It fails closed: only a natural
stop, the token limit and a stop to call tools are successes. A
reported failure, filtered content and any finish reason outside
that set are StopReason::Error, unless the response accepts
unknown reasons (Self::accepts_unknown_finish_reasons), which then
stop like FinishReason::Stop. A reply the caller stopped reading
is StopReason::Aborted.
Sourcepub fn identity(&self) -> ResponseIdentity
pub fn identity(&self) -> ResponseIdentity
This response’s identity metadata as one ResponseIdentity carrier.
Sourcepub fn with_finish_reason(self, finish_reason: FinishReason) -> Self
pub fn with_finish_reason(self, finish_reason: FinishReason) -> Self
Attach the normalized finish reason, reconciled against the choice via
FinishReason::reconcile_with_output.
Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
The text parts of Self::choice, concatenated in order.
Sourcepub fn reasoning(&self) -> String
pub fn reasoning(&self) -> String
The reasoning text of Self::choice, concatenated in order.
Redacted reasoning has no text.
Sourcepub fn message(&self) -> Option<Message>
pub fn message(&self) -> Option<Message>
The assistant turn to append to the conversation: Self::choice in
order with its origin and stop, or None for an
empty choice.
Sourcepub fn continued(&self, content: Vec<AssistantContent>) -> AssistantMessage
pub fn continued(&self, content: Vec<AssistantContent>) -> AssistantMessage
The turn this response began, holding content, for a runtime that
cut the reply short and answers its calls itself (an agent rolling a
turn back over an invalid call). It keeps the origin, and stops to
call tools, since the runtime answers them. A block keeps its provider
item only when this response holds the same block with it. A response
the provider did not end, or one the consumer has not wholly taken,
holds no provider item, so its turn replays canonically.
Sourcepub fn head(&self) -> AssistantMessage
pub fn head(&self) -> AssistantMessage
The turn’s origin and stop with no content, for a runtime that carries the content separately.
Sourcepub fn tool_calls(&self) -> impl Iterator<Item = &ToolCall>
pub fn tool_calls(&self) -> impl Iterator<Item = &ToolCall>
The tool calls in Self::choice, in order.
Sourcepub fn with_optional_finish_reason(
self,
finish_reason: Option<FinishReason>,
) -> Self
pub fn with_optional_finish_reason( self, finish_reason: Option<FinishReason>, ) -> Self
Sets or clears the finish reason, reconciling a present reason with the choice.
Trait Implementations§
Source§impl Clone for CompletionResponse
impl Clone for CompletionResponse
Source§impl Debug for CompletionResponse
impl Debug for CompletionResponse
Source§impl<'de> Deserialize<'de> for CompletionResponse
impl<'de> Deserialize<'de> for CompletionResponse
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>,
Source§impl PartialEq for CompletionResponse
impl PartialEq for CompletionResponse
Source§impl Serialize for CompletionResponse
impl Serialize for CompletionResponse
impl StructuralPartialEq for CompletionResponse
Auto Trait Implementations§
impl Freeze for CompletionResponse
impl RefUnwindSafe for CompletionResponse
impl Send for CompletionResponse
impl Sync for CompletionResponse
impl Unpin for CompletionResponse
impl UnsafeUnpin for CompletionResponse
impl UnwindSafe for CompletionResponse
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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