33
44import pytest
55
6- from agents import Agent , Runner , function_tool
6+ from agents import Agent , Runner , function_tool , HandoffCallItem , HandoffOutputItem
77
88from .fake_model import FakeModel
9- from .test_responses import get_function_tool_call , get_text_message
9+ from .test_responses import get_function_tool_call , get_text_message , get_handoff_tool_call
1010
1111
1212@function_tool
@@ -52,3 +52,76 @@ async def test_stream_events_main():
5252 assert tool_call_start_time > 0 , "tool_call_item was not observed"
5353 assert tool_call_end_time > 0 , "tool_call_output_item was not observed"
5454 assert tool_call_start_time < tool_call_end_time , "Tool call ended before or equals it started?"
55+
56+ @pytest .mark .asyncio
57+ async def test_stream_events_main_with_handoff ():
58+ @function_tool
59+ async def foo (args : str ) -> str :
60+ return f"foo_result_{ args } "
61+
62+ spanish_agent = Agent (
63+ name = "SpanishAgent" ,
64+ instructions = "You only speak Spanish." ,
65+ model = FakeModel (),
66+ )
67+
68+ english_agent = Agent (
69+ name = "EnglishAgent" ,
70+ instructions = "You only speak English." ,
71+ model = FakeModel (),
72+ )
73+
74+ model = FakeModel ()
75+ model .add_multiple_turn_outputs (
76+ [
77+ [
78+ get_text_message ("Hello" ),
79+ get_function_tool_call ("foo" , '{"args": "arg1"}' ),
80+ get_handoff_tool_call (spanish_agent ),
81+ ],
82+ [get_text_message ("Done" )],
83+ ]
84+ )
85+
86+ triage_agent = Agent (
87+ name = "TriageAgent" ,
88+ instructions = "Handoff to the appropriate agent based on the language of the request." ,
89+ handoffs = [spanish_agent , english_agent ],
90+ tools = [foo ],
91+ model = model ,
92+ )
93+
94+ result = Runner .run_streamed (
95+ triage_agent ,
96+ input = "Start" ,
97+ )
98+
99+ tool_call_start_time = - 1
100+ tool_call_end_time = - 1
101+ handoff_requested_seen = False
102+ handoff_occured_seen = False
103+ agent_switched_to_spanish = False
104+
105+ async for event in result .stream_events ():
106+ if event .type == "run_item_stream_event" :
107+ if isinstance (event .item , HandoffCallItem ):
108+ handoff_requested_seen = True
109+ elif isinstance (event .item , HandoffOutputItem ):
110+ handoff_occured_seen = True
111+ elif event .item .type == "tool_call_item" :
112+ tool_call_start_time = time .time_ns ()
113+ elif event .item .type == "tool_call_output_item" :
114+ tool_call_end_time = time .time_ns ()
115+ elif event .type == "agent_updated_stream_event" :
116+ if hasattr (event , 'new_agent' ) and event .new_agent .name == "SpanishAgent" :
117+ agent_switched_to_spanish = True
118+
119+
120+ assert tool_call_start_time > 0 , "tool_call_item was not observed"
121+ assert tool_call_end_time > 0 , "tool_call_output_item was not observed"
122+ assert tool_call_start_time < tool_call_end_time , "Tool call ended before or equals it started?"
123+
124+ assert handoff_requested_seen , "handoff_requested event not observed"
125+ assert handoff_occured_seen , "handoff_occured event not observed"
126+
127+ assert agent_switched_to_spanish , "Agent did not switch to SpanishAgent"
0 commit comments