Streaming
app.stream(..) runs the graph and yields events as they happen. You pick one or more stream modes per run.
The 8 modes
Section titled “The 8 modes”| Mode | StreamEvent variant |
What you get |
|---|---|---|
StreamMode::Values |
Values { ns, data } |
Full state after each superstep |
StreamMode::Updates |
Updates { ns, data } |
{node: update} after each task commits |
StreamMode::Checkpoints |
Checkpoints { ns, data } |
Checkpoint snapshot at the start of each tick |
StreamMode::Tasks |
Tasks { ns, data } |
Task start and task result |
StreamMode::Debug |
Debug { ns, data } |
{step, timestamp, type, payload} wrapping checkpoints and tasks |
StreamMode::Messages |
Messages { ns, message, metadata } |
Messages and token chunks with task metadata |
StreamMode::Custom |
Custom { ns, data } |
Anything a node writes through ctx.writer() |
StreamMode::Tools |
Tools { ns, data } |
tool-started, tool-output-delta, tool-finished, tool-error |
Mode names parse with serde and FromStr as values, updates, checkpoints, tasks, debug, messages, custom and tools.
Stream a run
Section titled “Stream a run”From the chatbot example:
use futures::StreamExt;
let cfg = RunConfig::thread("demo");let mut events = app.stream( json!({"messages": [{"role": "user", "content": "What is 2 + 40?"}]}), cfg.clone(), &[StreamMode::Updates, StreamMode::Messages], StreamOpts::default(),);while let Some(ev) = events.next().await { match ev? { StreamEvent::Updates { data, .. } => println!("update : {data}"), StreamEvent::Messages { message, .. } => println!("token : {}", message["content"]), _ => {} }}stream returns a BoxStream. Dropping the stream cancels the run.
Tokens: messages
Section titled “Tokens: messages”A node emits a chunk with ctx.stream_message(..):
for word in reply["content"].as_str().unwrap_or("").split_inclusive(' ') { ctx.stream_message(json!({"role": "assistant", "content": word, "id": "live", "chunk": true}));}ctx.wants_stream(StreamMode::Messages) tells a node whether anyone listens. The prebuilt ReAct agent uses it: with messages requested it calls ChatModel::stream and emits chunks, otherwise it calls generate.
Custom events
Section titled “Custom events”ctx.writer().write(json!({"tick": 1}));Tool events
Section titled “Tool events”ctx.stream_tool_event(..) emits on the tools mode. The payload builders are ToolEvent::started, output_delta, finished and error. The prebuilt ToolNode emits them for you. Inside a tool body, warpra_prebuilt::emit_output_delta(..) sends a tool-output-delta.
Subgraphs
Section titled “Subgraphs”Set StreamOpts { subgraphs: true, ..Default::default() } to also receive events from subgraphs. Each event carries its namespace in ns.
Cooperative cancel
Section titled “Cooperative cancel”StreamOpts::cancel takes a tokio_util::sync::CancellationToken. Once cancelled, the run stops at the next superstep boundary with GraphError::Cancelled. The last checkpoint records the pending next nodes, so the thread can resume later. Running tasks are not interrupted. Dropping the stream still aborts at once.
On the server
Section titled “On the server”warpra-server exposes these modes over SSE, plus messages-tuple. events and lifecycle return 422. See Warpra server.
Warpra is open source under Apache-2.0. Made by Liceron.