Skip to main content

Module stream

Module stream 

Source
Expand description

Streaming response types.

When a provider streams its response, it yields a sequence of StreamEvents through a ChatStream. Events arrive incrementally — text deltas, tool-call fragments, and finally a Done event with the stop reason.

§Collecting a stream

use futures::StreamExt;
use llm_stack::{ChatStream, StreamEvent};

async fn print_stream(mut stream: ChatStream) {
    while let Some(event) = stream.next().await {
        match event {
            Ok(StreamEvent::TextDelta(text)) => print!("{text}"),
            Ok(StreamEvent::Done { stop_reason }) => {
                println!("\n[done: {stop_reason:?}]");
            }
            Err(e) => eprintln!("stream error: {e}"),
            _ => {} // handle other events as needed
        }
    }
}

§Tool-call reassembly

Tool calls arrive in three phases:

  1. ToolCallStart — announces the call’s id and name.
  2. ToolCallDelta — one or more JSON argument fragments, streamed as they’re generated.
  3. ToolCallComplete — the fully assembled ToolCall with parsed arguments.

The index field on each event identifies which call it belongs to when the model invokes multiple tools in parallel.

Enums§

StreamEvent
An incremental event emitted during a streaming response.

Type Aliases§

ChatStream
A pinned, boxed, Send stream of StreamEvent results.