1212import logging
1313import time
1414import uuid
15- from typing import TYPE_CHECKING , Any , AsyncGenerator , Optional
15+ from typing import TYPE_CHECKING , Any , AsyncGenerator , Optional , Tuple
1616
1717from opentelemetry import trace as trace_api
1818
@@ -359,17 +359,20 @@ async def recurse_event_loop(agent: "Agent", invocation_state: dict[str, Any]) -
359359
360360def _prepare_tools_for_execution (
361361 message : Message ,
362- ) -> tuple [list [ToolUse ], list [ToolResult ], list [str ]]:
362+ ) -> Tuple [list [ToolUse ], list [ToolResult ], list [str ]]:
363363 """Prepare and validate tools from a message for execution.
364364
365+ Extracts tool uses from the message content, validates them, and filters out
366+ invalid ones while creating error results for invalid tools.
367+
365368 Args:
366369 message: The message from the model that may contain tool use requests.
367370
368371 Returns:
369372 A tuple containing:
370- - List of valid tool uses
371- - List of tool results for invalid tools
372- - List of invalid tool use IDs
373+ - List of valid tool uses ready for execution
374+ - List of tool results for invalid tools (to be included in response)
375+ - List of invalid tool use IDs (for filtering)
373376 """
374377 tool_uses : list [ToolUse ] = []
375378 tool_results : list [ToolResult ] = []
@@ -384,11 +387,14 @@ def _prepare_tools_for_execution(
384387def _create_tool_result_message (tool_results : list [ToolResult ]) -> Message :
385388 """Create a tool result message for conversation history.
386389
390+ Formats tool results into a user message that can be added to the conversation
391+ history and sent back to the model for continued processing.
392+
387393 Args:
388394 tool_results: List of tool results to include in the message.
389395
390396 Returns:
391- A formatted message containing the tool results.
397+ A formatted message containing the tool results in the expected format .
392398 """
393399 return {
394400 "role" : "user" ,
@@ -402,6 +408,9 @@ def _process_tool_result_message(
402408) -> None :
403409 """Process and add a tool result message to the agent's conversation history.
404410
411+ Adds the tool result message to the agent's message history and triggers
412+ any registered callbacks for message addition events.
413+
405414 Args:
406415 agent: The agent to add the message to.
407416 tool_result_message: The tool result message to process.
@@ -420,8 +429,12 @@ def _cleanup_event_loop_cycle(
420429) -> None :
421430 """Clean up tracing and metrics for an event loop cycle.
422431
432+ Handles the cleanup of tracing spans at the end of an event loop cycle.
433+ This is separated to avoid code duplication between structured output
434+ and regular tool execution paths.
435+
423436 Args:
424- cycle_span: Span object for tracing the cycle.
437+ cycle_span: Span object for tracing the cycle (may be None) .
425438 cycle_start_time: Start time of the current cycle.
426439 cycle_trace: Trace object for the current event loop cycle.
427440 agent: The agent for which the cycle is being cleaned up.
@@ -448,14 +461,18 @@ async def _handle_structured_output(
448461 cycle_start_time : float ,
449462 cycle_trace : Trace ,
450463 cycle_span : Any ,
451- ) -> AsyncGenerator [tuple [TypedEvent , bool ], None ]:
464+ ) -> AsyncGenerator [Tuple [TypedEvent , bool ], None ]:
452465 """Handle structured output processing and emit appropriate events.
453466
467+ Processes structured output from tool executions, emits the structured output
468+ event, and handles the cleanup and termination of the event loop when
469+ structured output is successfully generated.
470+
454471 Args:
455472 output_schema: The output schema to process.
456473 invocation_state: Current invocation state.
457- tool_uses: List of tool uses.
458- tool_results: List of tool results.
474+ tool_uses: List of tool uses that were executed .
475+ tool_results: List of tool results from execution .
459476 stop_reason: The reason the model stopped generating.
460477 message: The original message from the model.
461478 agent: The agent processing the structured output.
@@ -464,7 +481,8 @@ async def _handle_structured_output(
464481 cycle_span: Span object for tracing the cycle.
465482
466483 Yields:
467- A tuple of (event, should_stop) where should_stop indicates if processing should halt.
484+ A tuple of (event, should_stop) where should_stop indicates if the
485+ event loop should halt processing after this event.
468486 """
469487 from ..tools .structured_output_tool import _extract_structured_output_from_state , _cleanup_structured_outputs
470488
0 commit comments