Skip to main content

CompletionResponse

Struct CompletionResponse 

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

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

Stable 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: Value

The 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

Source

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.

Source

pub fn finish_reason(&self) -> Option<FinishReason>

Why the model stopped generating, when the provider reported it.

Source

pub fn identity(&self) -> ResponseIdentity

This response’s identity metadata as one ResponseIdentity carrier.

Source

pub fn with_finish_reason(self, finish_reason: FinishReason) -> Self

Attach the normalized finish reason, reconciled against the choice via FinishReason::reconcile_with_output.

Source

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

Source

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.

Source

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.

Source

pub fn with_response_id(self, response_id: impl Into<String>) -> Self

Attach the provider-assigned response-scoped ID.

Source

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.

Source

pub fn with_provider_request_id(self, request_id: impl Into<String>) -> Self

Attach the provider’s transport-level request identifier.

Source

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.

Source

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.

Source

pub fn with_optional_model(self, model: Option<impl Into<String>>) -> Self

Attach the provider-reported model identifier when the response carried one.

Source

pub fn with_raw(self, raw: impl Into<Value>) -> Self

Attach the provider’s own response, serialized — the value the model’s inherent raw method would have returned. Every provider seam calls this; see the raw field for the exact meaning of the payload (and of Value::Null).

Trait Implementations§

Source§

impl Clone for CompletionResponse

Source§

fn clone(&self) -> CompletionResponse

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 CompletionResponse

Source§

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

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

impl<'de> Deserialize<'de> for CompletionResponse

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 From<StreamingCompletionResponse> for CompletionResponse

Source§

fn from(value: StreamingCompletionResponse) -> CompletionResponse

Converts to this type from the input type.
Source§

impl Serialize for CompletionResponse

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

impl TryFrom<CompletionResponse> for CompletionResponse

Source§

type Error = CompletionError

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

fn try_from(response: CompletionResponse) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<CompletionResponse> for CompletionResponse

Source§

type Error = CompletionError

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

fn try_from(resp: CompletionResponse) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<GenerateContentResponse> for CompletionResponse

Normalize a Gemini generateContent response.

Source§

type Error = CompletionError

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

fn try_from(response: GenerateContentResponse) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Interaction> for CompletionResponse

Normalize a Gemini Interactions API payload.

Source§

type Error = CompletionError

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

fn try_from(response: Interaction) -> Result<Self, Self::Error>

Performs the conversion.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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<T> IntoToolOutput for T
where T: Serialize + 'static,

Source§

fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>

Convert this value without routing structured data through a string.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

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
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