Skip to main content

StreamFinal

Struct StreamFinal 

Source
pub struct StreamFinal {
    pub kind: StreamFinalKind,
    pub usage: Usage,
    pub finish_reason: Option<FinishReason>,
    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,
}
Expand description

The provider’s terminal stream record, normalized.

This replaces the provider-typed final payload that streams used to carry: usage is a plain field rather than a trait method, and the finish reason is normalized exactly as on the unary CompletionResponse.

Providers that want their own terminal type keep it behind RawStreamingResult and map it once with normalize_stream.

§Emission contract

A terminal record is emitted only when the provider signaled genuine completion — its own end-of-response event (an Anthropic message_delta with a stop reason, an OpenAI [DONE] / response.completed, a Gemini chunk carrying finishReason, and so on). Three failure shapes reach a consumer, and they are distinct:

ShapeErr itemStream continuesTerminal record
Transport error (connection lost, HTTP failure)yesnonever
Malformed frame (recoverable parse error)yesyesif a genuine terminal later arrives
Truncation (EOF without the provider’s end event)nonever

On a terminal error (a transport failure or the provider’s own failure event), tool calls that were fully delivered before the failure are yielded before the terminal Err; nothing follows the error — the stream then ends without a terminal record.

Consequently an Err item is not by itself terminal: a malformed frame is surfaced and the stream keeps consuming, so a later genuine terminal still completes it. Consumers must drain the stream to None rather than stop at the first Err, and must treat the absence of a terminal record as truncation, never as a successful zero-usage completion.

Fields§

§kind: StreamFinalKind

Discriminating field; always StreamFinalKind::Final.

§usage: Usage

Token usage reported by the provider for this streamed completion. Zero-valued usage is the documented sentinel for missing metrics.

§finish_reason: Option<FinishReason>

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

normalize_stream applies FinishReason::reconcile_with_output to this value using the tool calls actually seen on the stream, so a provider mapper does not need to (and cannot — it has no view of the preceding events).

§message_id: Option<String>

Provider-assigned assistant message ID, when available — only IDs the provider would recognize on a replayed assistant message. Response-scoped identifiers belong in StreamFinal::response_id.

§response_id: Option<String>

Provider-assigned response-scoped ID, when available — e.g. an OpenAI chat chatcmpl- ID. Never replayed to a provider as a message ID.

§provider_request_id: Option<String>

The provider’s transport-level request identifier, taken from the SSE connection’s HTTP response headers (Anthropic request-id, OpenAI/xAI x-request-id). When the source reconnected, this is the connection that delivered this terminal record. Never the body’s message/response id. None means the provider did not report one — a documented outcome, never an error.

§provider: String

Stable descriptor name of the provider that produced this stream.

§model: Option<String>

Provider-reported model identifier, when available.

§raw: Value

The provider’s own terminal record for this stream: the value the model’s inherent raw_stream would have yielded as its FinalResponse, serialized. It is the terminal record as rig’s wire type parsed it — fields that type does not model are not here — and it is the terminal record only, not the stream’s frames; see the module docs for why frames are a separate mechanism. normalize_stream populates it unconditionally — the same parity the pre-normalization Final(R) 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 record was built without a provider behind it — StreamFinal::new without with_raw (a provider’s mapper before normalize_stream attaches the terminal, test doubles, hand-built records), or a record persisted before the field existed — never that the provider sent nothing: no stream that reached its terminal yields Null here.

Typed access is recoverable: provider terminal types are Deserialize, so provider::StreamingCompletionResponse::deserialize(&raw) returns the provider’s own type.

Implementations§

Source§

impl StreamFinal

Source

pub fn new(provider: impl Into<String>, usage: Usage) -> StreamFinal

Create a terminal record for provider with usage; optional metadata starts unset and is filled in with the with_* helpers.

Source

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

Attach the normalized finish reason.

Source

pub fn with_optional_finish_reason( self, finish_reason: Option<FinishReason>, ) -> StreamFinal

Attach the normalized finish reason when the provider reported one.

Source

pub fn identity(&self) -> ResponseIdentity

This terminal record’s identity metadata as one crate::completion::ResponseIdentity carrier.

Source§

impl StreamFinal

Source

pub fn with_message_id(self, message_id: impl Into<String>) -> StreamFinal

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>>, ) -> StreamFinal

Attach the provider-assigned message ID when the provider reported one.

Source

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

Attach the provider-assigned response-scoped ID.

Source

pub fn with_optional_response_id( self, response_id: Option<impl Into<String>>, ) -> StreamFinal

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>, ) -> StreamFinal

Attach the provider’s transport-level request identifier.

Source

pub fn with_optional_provider_request_id( self, request_id: Option<impl Into<String>>, ) -> StreamFinal

Attach the provider’s transport-level request identifier when the provider reported one.

Source

pub fn with_model(self, model: impl Into<String>) -> StreamFinal

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>>, ) -> StreamFinal

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

Source

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

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 StreamFinal

Source§

fn clone(&self) -> StreamFinal

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 StreamFinal

Source§

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

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

impl<'de> Deserialize<'de> for StreamFinal

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<StreamFinal, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<(&str, CopilotStreamingResponse)> for StreamFinal

Source§

fn from(_: (&str, CopilotStreamingResponse)) -> StreamFinal

Converts to this type from the input type.
Source§

impl From<(&str, StreamingCompletionResponse)> for StreamFinal

Normalize an Anthropic terminal stream record.

The provider descriptor name is an input rather than a constant: the Anthropic Messages stream format is shared by every Anthropic-compatible provider, so baking in "anthropic" here would mislabel all of them.

Source§

fn from(_: (&str, StreamingCompletionResponse)) -> StreamFinal

Converts to this type from the input type.
Source§

impl From<(&str, StreamingCompletionResponse)> for StreamFinal

Normalize the Responses API’s terminal stream record.

The provider descriptor name is an input for the same reason it is on the unary conversion: ChatGPT and Copilot stream this exact wire shape, so a baked-in "openai" would mislabel them.

The finish reason is left exactly as the provider reported it; crate::streaming::normalize_stream applies the tool-call reconciliation afterwards, using the calls the stream actually emitted.

Source§

fn from(_: (&str, StreamingCompletionResponse)) -> StreamFinal

Converts to this type from the input type.
Source§

impl<U> From<(&str, StreamingCompletionResponse<U>)> for StreamFinal
where U: Into<Usage>,

Normalize an OpenAI-compatible streaming terminal record.

As on the unary path, the provider descriptor name is an input rather than a constant: this terminal record is shared by every OpenAI-compatible provider, so baking in "openai" here would mislabel Groq, Together, DeepSeek and the rest.

Source§

fn from(_: (&str, StreamingCompletionResponse<U>)) -> StreamFinal

Converts to this type from the input type.
Source§

impl From<StreamFinalRepr> for StreamFinal

Source§

fn from(repr: StreamFinalRepr) -> StreamFinal

Converts to this type from the input type.
Source§

impl From<StreamingCompletionResponse> for StreamFinal

Source§

fn from(response: StreamingCompletionResponse) -> StreamFinal

Converts to this type from the input type.
Source§

impl From<StreamingCompletionResponse> for StreamFinal

Source§

fn from(response: StreamingCompletionResponse) -> StreamFinal

Converts to this type from the input type.
Source§

impl PartialEq for StreamFinal

Source§

fn eq(&self, other: &StreamFinal) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for StreamFinal

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for StreamFinal

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> CloneDebuggableStorage for T

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> CloneableStorage for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

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