@@ -358,6 +358,7 @@ async def run_agent_auto_send(
358358 },
359359 ) as span :
360360 heartbeat_if_in_workflow ("run agent auto send" )
361+
361362 async with mcp_server_context (mcp_server_params , mcp_timeout_seconds ) as servers :
362363 tools = [tool .to_oai_function_tool () for tool in tools ] if tools else []
363364 handoffs = [Agent (** handoff .model_dump ()) for handoff in handoffs ] if handoffs else []
@@ -395,9 +396,12 @@ async def run_agent_auto_send(
395396 result = await Runner .run (
396397 starting_agent = agent , input = input_list , previous_response_id = previous_response_id
397398 )
398- item .raw_item .model_dump ()
399- if isinstance (item .raw_item , BaseModel )
400- else item .raw_item
399+ else :
400+ result = await Runner .run (starting_agent = agent , input = input_list )
401+
402+ if span :
403+ span .output = {
404+ "new_items" : [
401405 item .raw_item .model_dump () if isinstance (item .raw_item , BaseModel ) else item .raw_item
402406 for item in result .new_items
403407 ],
@@ -427,6 +431,7 @@ async def run_agent_auto_send(
427431
428432 elif item .type == "tool_call_item" :
429433 tool_call_item = item .raw_item
434+
430435 # Extract tool call information using the helper method
431436 call_id , tool_name , tool_arguments = self ._extract_tool_call_info (tool_call_item )
432437 tool_call_map [call_id ] = tool_call_item
@@ -552,15 +557,9 @@ async def run_agent_streamed(
552557 ) as span :
553558 heartbeat_if_in_workflow ("run agent streamed" )
554559
555- async with mcp_server_context (
556- mcp_server_params , mcp_timeout_seconds
557- ) as servers :
560+ async with mcp_server_context (mcp_server_params , mcp_timeout_seconds ) as servers :
558561 tools = [tool .to_oai_function_tool () for tool in tools ] if tools else []
559- handoffs = (
560- [Agent (** handoff .model_dump ()) for handoff in handoffs ]
561- if handoffs
562- else []
563- )
562+ handoffs = [Agent (** handoff .model_dump ()) for handoff in handoffs ] if handoffs else []
564563 agent_kwargs = {
565564 "name" : agent_name ,
566565 "instructions" : agent_instructions ,
@@ -573,9 +572,7 @@ async def run_agent_streamed(
573572 "tool_use_behavior" : tool_use_behavior ,
574573 }
575574 if model_settings is not None :
576- agent_kwargs ["model_settings" ] = (
577- model_settings .to_oai_model_settings ()
578- )
575+ agent_kwargs ["model_settings" ] = model_settings .to_oai_model_settings ()
579576 if input_guardrails is not None :
580577 agent_kwargs ["input_guardrails" ] = input_guardrails
581578 if output_guardrails is not None :
@@ -603,9 +600,7 @@ async def run_agent_streamed(
603600 if span :
604601 span .output = {
605602 "new_items" : [
606- item .raw_item .model_dump ()
607- if isinstance (item .raw_item , BaseModel )
608- else item .raw_item
603+ item .raw_item .model_dump () if isinstance (item .raw_item , BaseModel ) else item .raw_item
609604 for item in result .new_items
610605 ],
611606 "final_output" : result .final_output ,
@@ -738,6 +733,7 @@ async def run_agent_streamed_auto_send(
738733 if event .type == "run_item_stream_event" :
739734 if event .item .type == "tool_call_item" :
740735 tool_call_item = event .item .raw_item
736+
741737 # Extract tool call information using the helper method
742738 call_id , tool_name , tool_arguments = self ._extract_tool_call_info (tool_call_item )
743739 tool_call_map [call_id ] = tool_call_item
@@ -750,12 +746,10 @@ async def run_agent_streamed_auto_send(
750746 )
751747
752748 # Create tool request using streaming context (immediate completion)
753- async with (
754- self .streaming_service .streaming_task_message_context (
755- task_id = task_id ,
756- initial_content = tool_request_content ,
757- ) as streaming_context
758- ):
749+ async with self .streaming_service .streaming_task_message_context (
750+ task_id = task_id ,
751+ initial_content = tool_request_content ,
752+ ) as streaming_context :
759753 # The message has already been persisted, but we still need to send an upda
760754 await streaming_context .stream_update (
761755 update = StreamTaskMessageFull (
@@ -781,12 +775,9 @@ async def run_agent_streamed_auto_send(
781775 )
782776
783777 # Create tool response using streaming context (immediate completion)
784- async with (
785- self .streaming_service .streaming_task_message_context (
786- task_id = task_id ,
787- initial_content = tool_response_content
788- ) as streaming_context
789- ):
778+ async with self .streaming_service .streaming_task_message_context (
779+ task_id = task_id , initial_content = tool_response_content
780+ ) as streaming_context :
790781 # The message has already been persisted, but we still need to send an update
791782 await streaming_context .stream_update (
792783 update = StreamTaskMessageFull (
@@ -812,14 +803,10 @@ async def run_agent_streamed_auto_send(
812803 ),
813804 )
814805 # Open the streaming context
815- item_id_to_streaming_context [
816- item_id
817- ] = await streaming_context .open ()
806+ item_id_to_streaming_context [item_id ] = await streaming_context .open ()
818807 unclosed_item_ids .add (item_id )
819808 else :
820- streaming_context = item_id_to_streaming_context [
821- item_id
822- ]
809+ streaming_context = item_id_to_streaming_context [item_id ]
823810
824811 # Stream the delta through the streaming service
825812 await streaming_context .stream_update (
@@ -849,14 +836,10 @@ async def run_agent_streamed_auto_send(
849836 ),
850837 )
851838 # Open the streaming context
852- item_id_to_streaming_context [
853- item_id
854- ] = await streaming_context .open ()
839+ item_id_to_streaming_context [item_id ] = await streaming_context .open ()
855840 unclosed_item_ids .add (item_id )
856841 else :
857- streaming_context = item_id_to_streaming_context [
858- item_id
859- ]
842+ streaming_context = item_id_to_streaming_context [item_id ]
860843
861844 # Stream the summary delta through the streaming service
862845 await streaming_context .stream_update (
@@ -890,14 +873,10 @@ async def run_agent_streamed_auto_send(
890873 ),
891874 )
892875 # Open the streaming context
893- item_id_to_streaming_context [
894- item_id
895- ] = await streaming_context .open ()
876+ item_id_to_streaming_context [item_id ] = await streaming_context .open ()
896877 unclosed_item_ids .add (item_id )
897878 else :
898- streaming_context = item_id_to_streaming_context [
899- item_id
900- ]
879+ streaming_context = item_id_to_streaming_context [item_id ]
901880
902881 # Stream the content delta through the streaming service
903882 await streaming_context .stream_update (
@@ -925,6 +904,7 @@ async def run_agent_streamed_auto_send(
925904 # to close the streaming context, but they do!!!
926905 # They output both a ResponseReasoningSummaryTextDoneEvent and a ResponseReasoningSummaryPartDoneEvent
927906 # I have no idea why they do this.
907+
928908 elif isinstance (event .data , ResponseReasoningTextDoneEvent ):
929909 # Handle reasoning content text completion
930910 item_id = event .data .item_id
@@ -940,9 +920,7 @@ async def run_agent_streamed_auto_send(
940920
941921 # Finish the streaming context (sends DONE event and updates message)
942922 if item_id in item_id_to_streaming_context :
943- streaming_context = item_id_to_streaming_context [
944- item_id
945- ]
923+ streaming_context = item_id_to_streaming_context [item_id ]
946924 await streaming_context .close ()
947925 if item_id in unclosed_item_ids :
948926 unclosed_item_ids .remove (item_id )
@@ -952,17 +930,17 @@ async def run_agent_streamed_auto_send(
952930 # Create a copy to avoid modifying set during iteration
953931 remaining_items = list (unclosed_item_ids )
954932 for item_id in remaining_items :
955- if (item_id in unclosed_item_ids and
956- item_id in item_id_to_streaming_context ): # Check if still unclosed
957- streaming_context = item_id_to_streaming_context [
958- item_id
959- ]
933+ if (
934+ item_id in unclosed_item_ids and item_id in item_id_to_streaming_context
935+ ): # Check if still unclosed
936+ streaming_context = item_id_to_streaming_context [item_id ]
960937 await streaming_context .close ()
961938 unclosed_item_ids .discard (item_id )
962939
963940 except InputGuardrailTripwireTriggered as e :
964941 # Handle guardrail trigger by sending a rejection message
965942 rejection_message = "I'm sorry, but I cannot process this request due to a guardrail. Please try a different question."
943+
966944 # Try to extract rejection message from the guardrail result
967945 if hasattr (e , "guardrail_result" ) and hasattr (e .guardrail_result , "output" ):
968946 output_info = getattr (e .guardrail_result .output , "output_info" , {})
@@ -993,6 +971,7 @@ async def run_agent_streamed_auto_send(
993971 type = "full" ,
994972 ),
995973 )
974+
996975 # Re-raise to let the activity handle it
997976 raise
998977
@@ -1030,6 +1009,7 @@ async def run_agent_streamed_auto_send(
10301009 type = "full" ,
10311010 ),
10321011 )
1012+
10331013 # Re-raise to let the activity handle it
10341014 raise
10351015
0 commit comments