Skip to main content

normalize_stream

Function normalize_stream 

Source
pub fn normalize_stream<R, F>(
    stream: Pin<Box<dyn Stream<Item = Result<RawStreamingChoice<R>, CompletionError>> + Send>>,
    map: F,
) -> Pin<Box<dyn Stream<Item = Result<RawStreamingChoice, CompletionError>> + Send>>
where R: Serialize + 'static, F: FnMut(R) -> Result<StreamFinal, CompletionError> + WasmCompatSend + 'static,
Expand description

Normalize the terminal record of a provider-native stream.

Every incremental event passes through untouched; only RawStreamingChoice::FinalResponse is converted, by map. On the way through, the stream remembers whether it emitted any tool call and applies FinishReason::reconcile_with_output to the mapped record — the streaming counterpart of what CompletionResponse::with_finish_reason does on the unary path, so both paths agree about a stop that was really a tool call.

The provider-native terminal R is also serialized onto StreamFinal::raw before map consumes it — this is the one streaming seam every provider routes through, so it is the streaming counterpart of the capture each provider’s unary completion performs before normalize. That is why R is bounded Serialize: every in-tree terminal type already is, and a terminal that could not be serialized could not be surfaced to callers who no longer hold the typed model.