Open
Description
In the following example, the tool call event and the tool call output event are triggered simultaneously,and I believe it's more reasonable to separate them.
test code
import logging
logging.basicConfig(
format="%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logging.getLogger(__name__).setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
from agents import Runner, Agent, ItemHelpers, function_tool
import asyncio
import random
@function_tool
def how_many_jokes() -> int:
await asyncio.sleep(3)
return random.randint(1, 2)
async def main():
agent = Agent(
name="Joker",
instructions="First call the `how_many_jokes` tool, then tell that many jokes.",
tools=[how_many_jokes],
)
result = Runner.run_streamed(
agent,
input="Hello",
)
logger.debug("=== 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":
logger.debug(f"Agent updated: {event.new_agent.name}")
elif event.type == "run_item_stream_event":
if event.item.type == "tool_call_item":
logger.debug("-- Tool was called")
elif event.item.type == "tool_call_output_item":
logger.debug(f"-- Tool output: {event.item.output}")
elif event.item.type == "message_output_item":
logger.debug(
f"-- Message output:\n {ItemHelpers.text_message_output(event.item)}"
)
logger.debug("=== Run complete ===")
if __name__ == "__main__":
asyncio.run(main())
current output
2025-06-09 17:02:38.588 - DEBUG - === Run starting ===
2025-06-09 17:02:38.588 - DEBUG - Agent updated: Joker
2025-06-09 17:02:39.770 - DEBUG - -- Tool was called
2025-06-09 17:02:39.770 - DEBUG - -- Tool output: 1
2025-06-09 17:02:40.788 - DEBUG - -- Message output:
Here's a joke for you:
Why don't skeletons fight each other?
They don't have the guts!
2025-06-09 17:02:40.788 - DEBUG - === Run complete ===
For event.item.type in "tool_call_item" and "tool_call_output_item", they are triggered simultaneously, and I believe it's more reasonable to separate them.