@@ -358,7 +358,6 @@ async def run_agent_auto_send(
358358 },
359359 ) as span :
360360 heartbeat_if_in_workflow ("run agent auto send" )
361-
362361 async with mcp_server_context (mcp_server_params , mcp_timeout_seconds ) as servers :
363362 tools = [tool .to_oai_function_tool () for tool in tools ] if tools else []
364363 handoffs = [Agent (** handoff .model_dump ()) for handoff in handoffs ] if handoffs else []
@@ -396,12 +395,9 @@ async def run_agent_auto_send(
396395 result = await Runner .run (
397396 starting_agent = agent , input = input_list , previous_response_id = previous_response_id
398397 )
399- else :
400- result = await Runner .run (starting_agent = agent , input = input_list )
401-
402- if span :
403- span .output = {
404- "new_items" : [
398+ item .raw_item .model_dump ()
399+ if isinstance (item .raw_item , BaseModel )
400+ else item .raw_item
405401 item .raw_item .model_dump () if isinstance (item .raw_item , BaseModel ) else item .raw_item
406402 for item in result .new_items
407403 ],
@@ -431,7 +427,6 @@ async def run_agent_auto_send(
431427
432428 elif item .type == "tool_call_item" :
433429 tool_call_item = item .raw_item
434-
435430 # Extract tool call information using the helper method
436431 call_id , tool_name , tool_arguments = self ._extract_tool_call_info (tool_call_item )
437432 tool_call_map [call_id ] = tool_call_item
@@ -557,9 +552,15 @@ async def run_agent_streamed(
557552 ) as span :
558553 heartbeat_if_in_workflow ("run agent streamed" )
559554
560- async with mcp_server_context (mcp_server_params , mcp_timeout_seconds ) as servers :
555+ async with mcp_server_context (
556+ mcp_server_params , mcp_timeout_seconds
557+ ) as servers :
561558 tools = [tool .to_oai_function_tool () for tool in tools ] if tools else []
562- handoffs = [Agent (** handoff .model_dump ()) for handoff in handoffs ] if handoffs else []
559+ handoffs = (
560+ [Agent (** handoff .model_dump ()) for handoff in handoffs ]
561+ if handoffs
562+ else []
563+ )
563564 agent_kwargs = {
564565 "name" : agent_name ,
565566 "instructions" : agent_instructions ,
@@ -572,7 +573,9 @@ async def run_agent_streamed(
572573 "tool_use_behavior" : tool_use_behavior ,
573574 }
574575 if model_settings is not None :
575- agent_kwargs ["model_settings" ] = model_settings .to_oai_model_settings ()
576+ agent_kwargs ["model_settings" ] = (
577+ model_settings .to_oai_model_settings ()
578+ )
576579 if input_guardrails is not None :
577580 agent_kwargs ["input_guardrails" ] = input_guardrails
578581 if output_guardrails is not None :
@@ -600,7 +603,9 @@ async def run_agent_streamed(
600603 if span :
601604 span .output = {
602605 "new_items" : [
603- item .raw_item .model_dump () if isinstance (item .raw_item , BaseModel ) else item .raw_item
606+ item .raw_item .model_dump ()
607+ if isinstance (item .raw_item , BaseModel )
608+ else item .raw_item
604609 for item in result .new_items
605610 ],
606611 "final_output" : result .final_output ,
@@ -733,7 +738,6 @@ async def run_agent_streamed_auto_send(
733738 if event .type == "run_item_stream_event" :
734739 if event .item .type == "tool_call_item" :
735740 tool_call_item = event .item .raw_item
736-
737741 # Extract tool call information using the helper method
738742 call_id , tool_name , tool_arguments = self ._extract_tool_call_info (tool_call_item )
739743 tool_call_map [call_id ] = tool_call_item
@@ -746,10 +750,12 @@ async def run_agent_streamed_auto_send(
746750 )
747751
748752 # Create tool request using streaming context (immediate completion)
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 :
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+ ):
753759 # The message has already been persisted, but we still need to send an upda
754760 await streaming_context .stream_update (
755761 update = StreamTaskMessageFull (
@@ -775,9 +781,12 @@ async def run_agent_streamed_auto_send(
775781 )
776782
777783 # Create tool response using streaming context (immediate completion)
778- async with self .streaming_service .streaming_task_message_context (
779- task_id = task_id , initial_content = tool_response_content
780- ) as streaming_context :
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+ ):
781790 # The message has already been persisted, but we still need to send an update
782791 await streaming_context .stream_update (
783792 update = StreamTaskMessageFull (
@@ -803,10 +812,14 @@ async def run_agent_streamed_auto_send(
803812 ),
804813 )
805814 # Open the streaming context
806- item_id_to_streaming_context [item_id ] = await streaming_context .open ()
815+ item_id_to_streaming_context [
816+ item_id
817+ ] = await streaming_context .open ()
807818 unclosed_item_ids .add (item_id )
808819 else :
809- streaming_context = item_id_to_streaming_context [item_id ]
820+ streaming_context = item_id_to_streaming_context [
821+ item_id
822+ ]
810823
811824 # Stream the delta through the streaming service
812825 await streaming_context .stream_update (
@@ -836,10 +849,14 @@ async def run_agent_streamed_auto_send(
836849 ),
837850 )
838851 # Open the streaming context
839- item_id_to_streaming_context [item_id ] = await streaming_context .open ()
852+ item_id_to_streaming_context [
853+ item_id
854+ ] = await streaming_context .open ()
840855 unclosed_item_ids .add (item_id )
841856 else :
842- streaming_context = item_id_to_streaming_context [item_id ]
857+ streaming_context = item_id_to_streaming_context [
858+ item_id
859+ ]
843860
844861 # Stream the summary delta through the streaming service
845862 await streaming_context .stream_update (
@@ -873,10 +890,14 @@ async def run_agent_streamed_auto_send(
873890 ),
874891 )
875892 # Open the streaming context
876- item_id_to_streaming_context [item_id ] = await streaming_context .open ()
893+ item_id_to_streaming_context [
894+ item_id
895+ ] = await streaming_context .open ()
877896 unclosed_item_ids .add (item_id )
878897 else :
879- streaming_context = item_id_to_streaming_context [item_id ]
898+ streaming_context = item_id_to_streaming_context [
899+ item_id
900+ ]
880901
881902 # Stream the content delta through the streaming service
882903 await streaming_context .stream_update (
@@ -904,7 +925,6 @@ async def run_agent_streamed_auto_send(
904925 # to close the streaming context, but they do!!!
905926 # They output both a ResponseReasoningSummaryTextDoneEvent and a ResponseReasoningSummaryPartDoneEvent
906927 # I have no idea why they do this.
907-
908928 elif isinstance (event .data , ResponseReasoningTextDoneEvent ):
909929 # Handle reasoning content text completion
910930 item_id = event .data .item_id
@@ -920,7 +940,9 @@ async def run_agent_streamed_auto_send(
920940
921941 # Finish the streaming context (sends DONE event and updates message)
922942 if item_id in item_id_to_streaming_context :
923- streaming_context = item_id_to_streaming_context [item_id ]
943+ streaming_context = item_id_to_streaming_context [
944+ item_id
945+ ]
924946 await streaming_context .close ()
925947 if item_id in unclosed_item_ids :
926948 unclosed_item_ids .remove (item_id )
@@ -930,17 +952,17 @@ async def run_agent_streamed_auto_send(
930952 # Create a copy to avoid modifying set during iteration
931953 remaining_items = list (unclosed_item_ids )
932954 for item_id in remaining_items :
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 ]
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+ ]
937960 await streaming_context .close ()
938961 unclosed_item_ids .discard (item_id )
939962
940963 except InputGuardrailTripwireTriggered as e :
941964 # Handle guardrail trigger by sending a rejection message
942965 rejection_message = "I'm sorry, but I cannot process this request due to a guardrail. Please try a different question."
943-
944966 # Try to extract rejection message from the guardrail result
945967 if hasattr (e , "guardrail_result" ) and hasattr (e .guardrail_result , "output" ):
946968 output_info = getattr (e .guardrail_result .output , "output_info" , {})
@@ -971,7 +993,6 @@ async def run_agent_streamed_auto_send(
971993 type = "full" ,
972994 ),
973995 )
974-
975996 # Re-raise to let the activity handle it
976997 raise
977998
@@ -1009,7 +1030,6 @@ async def run_agent_streamed_auto_send(
10091030 type = "full" ,
10101031 ),
10111032 )
1012-
10131033 # Re-raise to let the activity handle it
10141034 raise
10151035
0 commit comments