#[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,
) -> CompletionResponse
pub fn new( choice: Vec<AssistantContent>, usage: Usage, origin: Origin, raw: Value, ) -> CompletionResponse
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<P>(
&self,
) -> Option<Result<<P as ProviderExtension>::Extras, Error>>where
P: ProviderExtension,
pub fn extras<P>(
&self,
) -> Option<Result<<P as ProviderExtension>::Extras, Error>>where
P: ProviderExtension,
Sourcepub fn extras_lossy<P>(&self) -> <P as ProviderExtension>::Extras
pub fn extras_lossy<P>(&self) -> <P as ProviderExtension>::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) -> CompletionResponse
pub fn accept_unknown_finish_reasons(self, accept: bool) -> CompletionResponse
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,
) -> CompletionResponse
pub fn with_finish_reason( self, finish_reason: FinishReason, ) -> CompletionResponse
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>,
) -> CompletionResponse
pub fn with_optional_finish_reason( self, finish_reason: Option<FinishReason>, ) -> CompletionResponse
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§fn clone(&self) -> CompletionResponse
fn clone(&self) -> CompletionResponse
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more