Skip to content

Stream Items

Source code in OpAgentsOlympus/practice/stream_items.py
OpAgentsOlympus/practice/stream_items.py
import asyncio
import random

from agents import Agent, ItemHelpers, Runner, function_tool
from config import config


@function_tool
def random_number() -> int:
    print("I was called")
    return random.randint(1, 4)


async def main():
    agent = Agent(
        name="Joker",
        instructions="First call the `random_number` tool which will give you a number, then tell that many jokes.",
        tools=[random_number],
    )

    result = Runner.run_streamed(agent, input="tell me jokes", run_config=config)
    print("=== Run starting ===")
    async for event in result.stream_events():
        # We'll ignore the raw responses event deltas
        if event.type == "raw_response_event":
            continue
        elif event.type == "agent_updated_stream_event":
            print(f"Agent updated: {event.new_agent.name}")
            continue
        elif event.type == "run_item_stream_event":
            if event.item.type == "tool_call_item":
                print("-- Tool was called")
            elif event.item.type == "tool_call_output_item":
                print(f"-- Tool output: {event.item.output}")
            elif event.item.type == "message_output_item":
                print(
                    f"-- Message output:\n {ItemHelpers.text_message_output(event.item)}"
                )
            else:
                pass  # Ignore other event types

    print("=== Run complete ===")


if __name__ == "__main__":
    asyncio.run(main())

    # === Run starting ===
    # Agent updated: Joker
    # -- Tool was called
    # -- Tool output: 4
    # -- Message output:
    #  Sure, here are four jokes for you:

    # 1. **Why don't skeletons fight each other?**
    #    They don't have the guts!

    # 2. **What do you call fake spaghetti?**
    #    An impasta!

    # 3. **Why did the scarecrow win an award?**
    #    Because he was outstanding in his field!

    # 4. **Why did the bicycle fall over?**
    #    Because it was two-tired!
    # === Run complete ===