pub struct CompletionResponse {
pub choice: Vec<AssistantContent>,
pub usage: Usage,
pub message_id: Option<String>,
pub response_id: Option<String>,
pub provider_request_id: Option<String>,
pub provider: String,
pub model: Option<String>,
pub raw: Value,
/* private fields */
}Expand description
General completion response struct: the completion choice plus normalized response metadata. The completion choice contains one or more assistant content items.
This type is concrete — it carries no provider-typed payload. Callers who
hold a concrete model and need a provider’s own wire response typed call
that model’s inherent raw_completion method, which performs the same
request and returns the provider’s native type. Callers who do not hold the
concrete model — an agent erases it at construction — read the same value,
serialized, from CompletionResponse::raw, which every provider seam
populates.
Fields§
§choice: Vec<AssistantContent>The completion choice (represented by one or more assistant message content) returned by the completion model provider
usage: UsageTokens used during prompting and responding
message_id: Option<String>The identifier the provider assigned to the assistant message itself,
when it issued one — an OpenAI Responses output-message msg_ ID or an
Anthropic msg_ ID. Only IDs the provider would recognize on a replayed
assistant message belong here; identifiers that name the whole response
(an OpenAI chat chatcmpl- ID, a Gemini responseId) go in
CompletionResponse::response_id instead.
The Responses API path uses it to pair reasoning input items with their
output items across turns, and it is what agent history promotes into
Message::Assistant’s id.
response_id: Option<String>The identifier the provider assigned to the response as a whole, when it
reported one — an OpenAI chat chatcmpl- ID, a Gemini responseId, a
Cohere generation ID. Response-scoped: useful for logging, telemetry
(gen_ai.response.id), and support requests, but never replayed to a
provider as a message ID.
provider_request_id: Option<String>The provider’s transport-level request identifier, taken from the HTTP
response headers (Anthropic request-id, OpenAI/xAI x-request-id) or
the provider SDK’s response metadata (Bedrock) — the id provider
support asks for when investigating a request. Never the body’s
message.id/response id; those are Self::message_id and
Self::response_id. None means the provider did not report one —
that is a documented outcome (e.g. Gemini sends no id header), never an
error.
provider: StringStable descriptor name of the provider that produced this response, for
example "openai". Always populated, including for responses derived
from a stream that ended before its terminal record.
model: Option<String>Provider-reported model identifier for the response.
This is the model named by the wire response, not the model that was
requested; it is None when the provider reports no identifier.
raw: ValueThe provider’s own response for this call: the value the model’s
inherent raw_completion would have returned, serialized. It is the
response as rig’s wire type parsed it — fields that type does not model
are not here. Every provider seam populates it, unconditionally — the
same parity the pre-normalization raw_response: T had.
An escape hatch for provider-specific data rig does not normalize — it
never replaces a normalized field, and every normalized field means the
same thing whatever this holds. Value::Null means the value was built
without a provider behind it — CompletionResponse::new without
with_raw (test doubles, hand-built responses), or a response
persisted before the field existed — never that the provider sent
nothing: no provider seam produces Null.
Typed access is recoverable: provider raw types are Deserialize, so
provider::CompletionResponse::deserialize(&raw) returns the
provider’s own type, and NormalizeCompletionResponse converts
forward.
Implementations§
Source§impl CompletionResponse
impl CompletionResponse
Sourcepub fn new(
choice: Vec<AssistantContent>,
usage: Usage,
provider: impl Into<String>,
) -> Self
pub fn new( choice: Vec<AssistantContent>, usage: Usage, provider: impl Into<String>, ) -> Self
Create a response from its required parts; optional metadata starts
unset and is filled in with the with_* helpers.
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 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 with_optional_finish_reason(
self,
finish_reason: Option<FinishReason>,
) -> Self
pub fn with_optional_finish_reason( self, finish_reason: Option<FinishReason>, ) -> Self
Attach the normalized finish reason when the provider reported one.
This is the Option form of CompletionResponse::with_finish_reason
and applies the same reconciliation. Provider conversions that hold an
Option<FinishReason> use this rather than assigning the field, so the
tool-call upgrade is never skipped.
Source§impl CompletionResponse
impl CompletionResponse
Sourcepub fn with_message_id(self, message_id: impl Into<String>) -> Self
pub fn with_message_id(self, message_id: impl Into<String>) -> Self
Attach the provider-assigned message ID.
An empty string is treated as absent: gateways that echo ""
for fields they don’t populate must not produce a Some("")
that differs between the buffered and streaming paths. All
identifier and model setters share this rule so the invariant
lives here rather than at every provider call site.
Sourcepub fn with_optional_message_id(
self,
message_id: Option<impl Into<String>>,
) -> Self
pub fn with_optional_message_id( self, message_id: Option<impl Into<String>>, ) -> Self
Attach the provider-assigned message ID when the provider reported one.
Sourcepub fn with_response_id(self, response_id: impl Into<String>) -> Self
pub fn with_response_id(self, response_id: impl Into<String>) -> Self
Attach the provider-assigned response-scoped ID.
Sourcepub fn with_optional_response_id(
self,
response_id: Option<impl Into<String>>,
) -> Self
pub fn with_optional_response_id( self, response_id: Option<impl Into<String>>, ) -> Self
Attach the provider-assigned response-scoped ID when the provider reported one.
Sourcepub fn with_provider_request_id(self, request_id: impl Into<String>) -> Self
pub fn with_provider_request_id(self, request_id: impl Into<String>) -> Self
Attach the provider’s transport-level request identifier.
Sourcepub fn with_optional_provider_request_id(
self,
request_id: Option<impl Into<String>>,
) -> Self
pub fn with_optional_provider_request_id( self, request_id: Option<impl Into<String>>, ) -> Self
Attach the provider’s transport-level request identifier when the provider reported one.
Sourcepub fn with_model(self, model: impl Into<String>) -> Self
pub fn with_model(self, model: impl Into<String>) -> Self
Attach the provider-reported model identifier.
An empty string is treated as absent, matching the identifier setters.
Sourcepub fn with_optional_model(self, model: Option<impl Into<String>>) -> Self
pub fn with_optional_model(self, model: Option<impl Into<String>>) -> Self
Attach the provider-reported model identifier when the response carried one.
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 moreSource§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 From<StreamingCompletionResponse> for CompletionResponse
impl From<StreamingCompletionResponse> for CompletionResponse
Source§fn from(value: StreamingCompletionResponse) -> CompletionResponse
fn from(value: StreamingCompletionResponse) -> CompletionResponse
Source§impl Serialize for CompletionResponse
impl Serialize for CompletionResponse
Source§impl TryFrom<CompletionResponse> for CompletionResponse
impl TryFrom<CompletionResponse> for CompletionResponse
Source§type Error = CompletionError
type Error = CompletionError
Source§impl TryFrom<CompletionResponse> for CompletionResponse
impl TryFrom<CompletionResponse> for CompletionResponse
Source§type Error = CompletionError
type Error = CompletionError
Source§impl TryFrom<GenerateContentResponse> for CompletionResponse
Normalize a Gemini generateContent response.
impl TryFrom<GenerateContentResponse> for CompletionResponse
Normalize a Gemini generateContent response.
Source§type Error = CompletionError
type Error = CompletionError
Source§impl TryFrom<Interaction> for CompletionResponse
Normalize a Gemini Interactions API payload.
impl TryFrom<Interaction> for CompletionResponse
Normalize a Gemini Interactions API payload.
Source§type Error = CompletionError
type Error = CompletionError
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> 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