Skip to content

Latest commit

 

History

History
62 lines (43 loc) · 1.96 KB

File metadata and controls

62 lines (43 loc) · 1.96 KB

Streaming

Use this page when you need visibility into the run before the final output is ready.

What You Get

RunResultStreaming is a live run handle. It lets you:

  • read events as they happen
  • wait for the final RunResult
  • inspect last-response and replay state
  • preserve normalized and full replay input

Minimal Example

use futures::StreamExt;
use openai_agents::{Agent, run_streamed};

#[tokio::main]
async fn main() -> Result<(), openai_agents::AgentsError> {
    let agent = Agent::builder("assistant").build();
    let streamed = run_streamed(&agent, "hello").await?;

    let events = streamed.stream_events().collect::<Vec<_>>().await;
    let result = streamed.wait_for_completion().await?;

    println!("events={}", events.len());
    println!("{:?}", result.final_output);
    Ok(())
}

Runnable versions: streamed_run.rs for a minimal stream, stream_text.rs for message text, and stream_items.rs for run-item events around a tool call. For tool-call arguments, stream_function_call_args.rs shows the RunItem::ToolCall event payload. For nested agent tools, agents_as_tools_streaming.rs shows AgentAsToolOptions::on_stream.

Event Families

The stream can include:

  • agent lifecycle events
  • raw model events
  • run item events
  • tool lifecycle events
  • handoff events
  • interruption events

Replay Matters

The streamed handle is not just an event feed. It also preserves the state needed to continue or replay the run after completion.

Use:

  • wait_for_completion() for the durable result
  • to_input_list() or to_input_list_mode(...) for replay

Read Next