@@ -134,7 +134,7 @@ class ProcessedResponse:
134
134
tools_used : list [str ] # Names of all tools used, including hosted tools
135
135
136
136
def has_tools_to_run (self ) -> bool :
137
- # Handoffs, functions and computer actions need local processing
137
+ # Handoffs, functions, and computer actions need local processing.
138
138
# Hosted tools have already run, so there's nothing to do.
139
139
return any (
140
140
[
@@ -202,9 +202,9 @@ async def execute_tools_and_side_effects(
202
202
cls ,
203
203
* ,
204
204
agent : Agent [TContext ],
205
- # The original input to the Runner
205
+ # The original input to the Runner.
206
206
original_input : str | list [TResponseInputItem ],
207
- # Everything generated by Runner since the original input, but before the current step
207
+ # Everything generated by Runner since the original input, but before the current step.
208
208
pre_step_items : list [RunItem ],
209
209
new_response : ModelResponse ,
210
210
processed_response : ProcessedResponse ,
@@ -213,13 +213,13 @@ async def execute_tools_and_side_effects(
213
213
context_wrapper : RunContextWrapper [TContext ],
214
214
run_config : RunConfig ,
215
215
) -> SingleStepResult :
216
- # Make a copy of the generated items
216
+ # Make a copy of the generated items.
217
217
pre_step_items = list (pre_step_items )
218
218
219
219
new_step_items : list [RunItem ] = []
220
220
new_step_items .extend (processed_response .new_items )
221
221
222
- # First, lets run the tool calls - function tools and computer actions
222
+ # First, let us run the tool calls: function tools and computer actions.
223
223
function_results , computer_results = await asyncio .gather (
224
224
cls .execute_function_tool_calls (
225
225
agent = agent ,
@@ -239,7 +239,7 @@ async def execute_tools_and_side_effects(
239
239
new_step_items .extend ([result .run_item for result in function_results ])
240
240
new_step_items .extend (computer_results )
241
241
242
- # Second, check if there are any handoffs
242
+ # Second, check whether there are any handoffs.
243
243
if run_handoffs := processed_response .handoffs :
244
244
return await cls .execute_handoffs (
245
245
agent = agent ,
@@ -253,7 +253,7 @@ async def execute_tools_and_side_effects(
253
253
run_config = run_config ,
254
254
)
255
255
256
- # Third, we'll check if the tool use should result in a final output
256
+ # Third, we check whether the tool use should result in a final output.
257
257
check_tool_use = await cls ._check_for_final_output_from_tools (
258
258
agent = agent ,
259
259
tool_results = function_results ,
@@ -262,7 +262,7 @@ async def execute_tools_and_side_effects(
262
262
)
263
263
264
264
if check_tool_use .is_final_output :
265
- # If the output type is str, then let's just stringify it
265
+ # If the output type is str, then just stringify it.
266
266
if not agent .output_type or agent .output_type is str :
267
267
check_tool_use .final_output = str (check_tool_use .final_output )
268
268
@@ -283,17 +283,17 @@ async def execute_tools_and_side_effects(
283
283
context_wrapper = context_wrapper ,
284
284
)
285
285
286
- # Now we can check if the model also produced a final output
286
+ # Now we can check whether the model also produced a final output.
287
287
message_items = [item for item in new_step_items if isinstance (item , MessageOutputItem )]
288
288
289
- # We'll use the last content output as the final output
289
+ # We use the last content output as the final output.
290
290
potential_final_output_text = (
291
291
ItemHelpers .extract_last_text (message_items [- 1 ].raw_item ) if message_items else None
292
292
)
293
293
294
- # There are two possibilities that lead to a final output:
295
- # 1. Structured output schema => always leads to a final output
296
- # 2. Plain text output schema => only leads to a final output if there are no tool calls
294
+ # There are two possibilities that lead to a final output.
295
+ # 1. Structured output schema always leads to a final output.
296
+ # 2. Plain text output schema leads to a final output only if there are no tool calls.
297
297
if output_schema and not output_schema .is_plain_text () and potential_final_output_text :
298
298
final_output = output_schema .validate_json (potential_final_output_text )
299
299
return await cls .execute_final_output (
@@ -320,7 +320,7 @@ async def execute_tools_and_side_effects(
320
320
context_wrapper = context_wrapper ,
321
321
)
322
322
else :
323
- # If there's no final output, we can just run again
323
+ # If there is no final output, we can run again.
324
324
return SingleStepResult (
325
325
original_input = original_input ,
326
326
model_response = new_response ,
@@ -392,21 +392,21 @@ def process_model_response(
392
392
logger .warning (f"Unexpected output type, ignoring: { type (output )} " )
393
393
continue
394
394
395
- # At this point we know it's a function tool call
395
+ # At this point we know it is a function tool call.
396
396
if not isinstance (output , ResponseFunctionToolCall ):
397
397
continue
398
398
399
399
tools_used .append (output .name )
400
400
401
- # Handoffs
401
+ # Handoffs.
402
402
if output .name in handoff_map :
403
403
items .append (HandoffCallItem (raw_item = output , agent = agent ))
404
404
handoff = ToolRunHandoff (
405
405
tool_call = output ,
406
406
handoff = handoff_map [output .name ],
407
407
)
408
408
run_handoffs .append (handoff )
409
- # Regular function tool call
409
+ # Regular function tool call.
410
410
else :
411
411
if output .name not in function_map :
412
412
_error_tracing .attach_error_to_current_span (
@@ -513,7 +513,7 @@ async def execute_computer_actions(
513
513
config : RunConfig ,
514
514
) -> list [RunItem ]:
515
515
results : list [RunItem ] = []
516
- # Need to run these serially, because each action can affect the computer state
516
+ # These must run serially because each action can affect the computer state.
517
517
for action in actions :
518
518
results .append (
519
519
await ComputerAction .execute (
@@ -541,7 +541,7 @@ async def execute_handoffs(
541
541
context_wrapper : RunContextWrapper [TContext ],
542
542
run_config : RunConfig ,
543
543
) -> SingleStepResult :
544
- # If there is more than one handoff, add tool responses that reject those handoffs
544
+ # If there is more than one handoff, add tool responses that reject those handoffs.
545
545
multiple_handoffs = len (run_handoffs ) > 1
546
546
if multiple_handoffs :
547
547
output_message = "Multiple handoffs detected, ignoring this one."
@@ -576,7 +576,7 @@ async def execute_handoffs(
576
576
)
577
577
)
578
578
579
- # Append a tool output item for the handoff
579
+ # Append a tool output item for the handoff.
580
580
new_step_items .append (
581
581
HandoffOutputItem (
582
582
agent = agent ,
@@ -589,7 +589,7 @@ async def execute_handoffs(
589
589
)
590
590
)
591
591
592
- # Execute handoff hooks
592
+ # Execute handoff hooks.
593
593
await asyncio .gather (
594
594
hooks .on_handoff (
595
595
context = context_wrapper ,
@@ -607,7 +607,7 @@ async def execute_handoffs(
607
607
),
608
608
)
609
609
610
- # If there's an input filter, filter the input for the next agent
610
+ # If there is an input filter, filter the input for the next agent.
611
611
input_filter = handoff .input_filter or (
612
612
run_config .handoff_input_filter if run_config else None
613
613
)
@@ -669,7 +669,7 @@ async def execute_final_output(
669
669
hooks : RunHooks [TContext ],
670
670
context_wrapper : RunContextWrapper [TContext ],
671
671
) -> SingleStepResult :
672
- # Run the on_end hooks
672
+ # Run the on_end hooks.
673
673
await cls .run_final_output_hooks (agent , hooks , context_wrapper , final_output )
674
674
675
675
return SingleStepResult (
@@ -862,22 +862,22 @@ async def execute(
862
862
),
863
863
)
864
864
865
- # Cache screenshots to avoid resending duplicate images.
866
- image_id , is_new = _cache_screenshot (output )
867
- if is_new :
868
- image_url = f"data:image/png;base64,{ output } "
869
- raw_output = {
870
- "type" : "computer_screenshot" ,
871
- "image_url" : image_url ,
872
- "image_id" : image_id ,
873
- }
874
- final_output = image_url
875
- else :
876
- raw_output = {
877
- "type" : "computer_screenshot_ref" ,
878
- "image_id" : image_id ,
879
- }
880
- final_output = image_id
865
+ # TODO: Cache screenshots; avoid resending duplicate images.
866
+ image_id , is_new = _cache_screenshot (output )
867
+ if is_new :
868
+ image_url = f"data:image/png;base64,{ output } "
869
+ raw_output = {
870
+ "type" : "computer_screenshot" ,
871
+ "image_url" : image_url ,
872
+ "image_id" : image_id ,
873
+ }
874
+ final_output = image_url
875
+ else :
876
+ raw_output = {
877
+ "type" : "computer_screenshot_ref" ,
878
+ "image_id" : image_id ,
879
+ }
880
+ final_output = image_id
881
881
return ToolCallOutputItem (
882
882
agent = agent ,
883
883
output = final_output ,
0 commit comments