1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
25 small wins to finish your pathNext lesson →
Watching a run as it happens
A run with two model calls and a tool takes a few seconds. Streaming turns that from a blank screen into something a person will wait for.
One word changes
result = Runner.run_streamed(agent, "Where is order A17?")No await on that line. run_streamed hands back a result object immediately and the run happens as you read from it.
async for event in result.stream_events():
if event.type == "run_item_stream_event":
print("item ->", event.item.type)result = Runner.run_streamed(agent, "Where is order A17?")
async for event in result.stream_events():
if event.type == "run_item_stream_event":
print("item ->", event.item.type)
elif event.type == "agent_updated_stream_event":
print("agent ->", event.new_agent.name)
print()
print("answer:", result.final_output)Those are the same three items from lesson 4, arriving one at a time instead of all at the end. The agent event fires first, before any work, which is how an interface knows whose name to put at the top.
The three kinds of event
| Type | What it is | Use it for |
|---|---|---|
raw_response_event | the model's output, as it is produced | typing the answer out live |
run_item_stream_event | a finished item: a tool call, a tool answer, a message | a progress log |
agent_updated_stream_event | a different agent took over | showing a handoff as it happens |
Our stand-in produces its answer all at once, so there is one raw event rather than dozens. A real model sends a few characters at a time and that first row is where the typing effect comes from.
Read the final answer last
final_output is only there once the loop over events has finished. Read it before that and the run has not got to the end yet.Try it yourself
- Print every event type, including the raw ones.
- Filter to
message_output_itemonly and print the text. - Stream the handoff from lesson 13 and watch the agent event fire twice.
Little by little, you're building something great.