Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai agents python Runner Run Streamed

From Leeroopedia

Overview

Runner.run_streamed() is a class method on the Runner class that initiates an agent run with real-time streaming of responses. It returns a RunResultStreaming object immediately while processing continues in the background.

Full Signature

class Runner:
    @classmethod
    def run_streamed(
        cls,
        starting_agent: Agent[TContext],
        input: str | list[TResponseInputItem] | RunState[TContext],
        context: TContext | None = None,
        max_turns: int = DEFAULT_MAX_TURNS,
        hooks: RunHooks[TContext] | None = None,
        run_config: RunConfig | None = None,
        previous_response_id: str | None = None,
        auto_previous_response_id: bool = False,
        conversation_id: str | None = None,
        session: Session | None = None,
        *,
        error_handlers: RunErrorHandlers[TContext] | None = None,
    ) -> RunResultStreaming:

Source

  • File: src/agents/run.py, lines 312-382
  • Import: from agents import Runner

Parameters

Parameter Type Description
starting_agent Agent[TContext] The agent to begin execution with. This is the entry point for the run.
input list[TResponseInputItem] | RunState[TContext] The user input. Can be a simple string, a list of structured input items, or a RunState to resume a previous run.
context None Optional context object passed to the agent and its tools during execution.
max_turns int Maximum number of turns (LLM calls) allowed before the run is terminated. Defaults to DEFAULT_MAX_TURNS.
hooks None Optional lifecycle hooks that are called at various points during the run (e.g., on agent start, on tool call).
run_config None Optional configuration for the run, controlling model parameters, tracing, and other settings.
previous_response_id None An explicit previous response ID for server-managed conversation continuity.
auto_previous_response_id bool When True, automatically chains response IDs across turns for server-managed conversation.
conversation_id None Optional conversation ID for server-managed conversation tracking.
session None Optional session object for persisting conversation state across runs.
error_handlers None Optional error handlers for customizing error behavior during the run. This is a keyword-only argument.

Return Value

Type: RunResultStreaming

The method returns a RunResultStreaming object immediately. This object provides:

  • stream_events() -- An async iterator that yields StreamEvent objects as the run progresses.
  • final_output -- The final output, available after the stream completes.
  • new_items -- All run items produced during execution.
  • is_complete -- Boolean indicating whether the stream has finished.
  • current_agent -- The currently active agent (may change during handoffs).

Example

from agents import Agent, Runner

agent = Agent(name="writer", instructions="Write creative stories.")
result = Runner.run_streamed(agent, "Write a short poem about coding.")

async for event in result.stream_events():
    if event.type == "raw_response_event":
        # Access raw LLM deltas for real-time text display
        if hasattr(event.data, 'delta') and hasattr(event.data.delta, 'text'):
            print(event.data.delta.text, end="", flush=True)

# After stream completes:
print(result.final_output)

Explanation

  1. An Agent is created with a name and instructions.
  2. Runner.run_streamed() is called with the agent and a user input string. It returns immediately.
  3. The async for loop consumes events from stream_events(). Each raw_response_event may contain text deltas that can be printed incrementally.
  4. After the loop completes, result.final_output contains the complete output.

Key Differences from Runner.run()

Aspect Runner.run() Runner.run_streamed()
Return behavior Blocks until completion Returns immediately
Return type RunResult RunResultStreaming
Result access Directly on return After consuming stream_events()
Real-time events Not available Available via stream_events()
Core logic Shared run loop Shared run loop

See Also

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment