Implementation:Openai Openai agents python Runner Run Streamed
Appearance
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 yieldsStreamEventobjects 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
- An
Agentis created with a name and instructions. Runner.run_streamed()is called with the agent and a user input string. It returns immediately.- The
async forloop consumes events fromstream_events(). Eachraw_response_eventmay contain text deltas that can be printed incrementally. - After the loop completes,
result.final_outputcontains 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
- Streamed Run Invocation Principle -- The theory behind streamed run invocation.
- Stream Events Implementation -- The event types yielded during streaming.
- RunResultStreaming Implementation -- The streaming result object.
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment