1111import logging
1212import time
1313import uuid
14+ from concurrent .futures import ThreadPoolExecutor
1415from functools import partial
1516from typing import Any , Generator , Optional
1617
2021from ..telemetry .tracer import get_tracer
2122from ..tools .executor import run_tools , validate_and_prepare_tools
2223from ..types .content import Message , Messages
23- from ..types .event_loop import ParallelToolExecutorInterface
2424from ..types .exceptions import ContextWindowOverflowException , EventLoopException , ModelThrottledException
2525from ..types .models import Model
2626from ..types .streaming import Metrics , StopReason
@@ -41,7 +41,7 @@ def event_loop_cycle(
4141 messages : Messages ,
4242 tool_config : Optional [ToolConfig ],
4343 tool_handler : Optional [ToolHandler ],
44- tool_execution_handler : Optional [ParallelToolExecutorInterface ],
44+ thread_pool : Optional [ThreadPoolExecutor ],
4545 event_loop_metrics : EventLoopMetrics ,
4646 event_loop_parent_span : Optional [trace .Span ],
4747 kwargs : dict [str , Any ],
@@ -65,7 +65,7 @@ def event_loop_cycle(
6565 messages: Conversation history messages.
6666 tool_config: Configuration for available tools.
6767 tool_handler: Handler for executing tools.
68- tool_execution_handler : Optional handler for parallel tool execution.
68+ thread_pool : Optional thread pool for parallel tool execution.
6969 event_loop_metrics: Metrics tracking object for the event loop.
7070 event_loop_parent_span: Span for the parent of this event loop.
7171 kwargs: Additional arguments including:
@@ -210,7 +210,7 @@ def event_loop_cycle(
210210 messages ,
211211 tool_config ,
212212 tool_handler ,
213- tool_execution_handler ,
213+ thread_pool ,
214214 event_loop_metrics ,
215215 event_loop_parent_span ,
216216 cycle_trace ,
@@ -256,7 +256,7 @@ def recurse_event_loop(
256256 messages : Messages ,
257257 tool_config : Optional [ToolConfig ],
258258 tool_handler : Optional [ToolHandler ],
259- tool_execution_handler : Optional [ParallelToolExecutorInterface ],
259+ thread_pool : Optional [ThreadPoolExecutor ],
260260 event_loop_metrics : EventLoopMetrics ,
261261 event_loop_parent_span : Optional [trace .Span ],
262262 kwargs : dict [str , Any ],
@@ -271,7 +271,7 @@ def recurse_event_loop(
271271 messages: Conversation history messages
272272 tool_config: Configuration for available tools
273273 tool_handler: Handler for tool execution
274- tool_execution_handler : Optional handler for parallel tool execution.
274+ thread_pool : Optional thread pool for parallel tool execution.
275275 event_loop_metrics: Metrics tracking object for the event loop.
276276 event_loop_parent_span: Span for the parent of this event loop.
277277 kwargs: Arguments to pass through event_loop_cycle
@@ -298,7 +298,7 @@ def recurse_event_loop(
298298 messages = messages ,
299299 tool_config = tool_config ,
300300 tool_handler = tool_handler ,
301- tool_execution_handler = tool_execution_handler ,
301+ thread_pool = thread_pool ,
302302 event_loop_metrics = event_loop_metrics ,
303303 event_loop_parent_span = event_loop_parent_span ,
304304 kwargs = kwargs ,
@@ -315,7 +315,7 @@ def _handle_tool_execution(
315315 messages : Messages ,
316316 tool_config : ToolConfig ,
317317 tool_handler : ToolHandler ,
318- tool_execution_handler : Optional [ParallelToolExecutorInterface ],
318+ thread_pool : Optional [ThreadPoolExecutor ],
319319 event_loop_metrics : EventLoopMetrics ,
320320 event_loop_parent_span : Optional [trace .Span ],
321321 cycle_trace : Trace ,
@@ -331,20 +331,20 @@ def _handle_tool_execution(
331331 Handles the execution of tools requested by the model during an event loop cycle.
332332
333333 Args:
334- stop_reason (StopReason) : The reason the model stopped generating.
335- message (Message) : The message from the model that may contain tool use requests.
336- model (Model) : The model provider instance.
337- system_prompt (Optional[str]) : The system prompt instructions for the model.
338- messages (Messages) : The conversation history messages.
339- tool_config (ToolConfig) : Configuration for available tools.
340- tool_handler (ToolHandler) : Handler for tool execution.
341- tool_execution_handler (Optional[ParallelToolExecutorInterface]) : Optional handler for parallel tool execution.
342- event_loop_metrics (EventLoopMetrics) : Metrics tracking object for the event loop.
343- event_loop_parent_span (Any) : Span for the parent of this event loop.
344- cycle_trace (Trace) : Trace object for the current event loop cycle.
345- cycle_span (Any) : Span object for tracing the cycle (type may vary).
346- cycle_start_time (float) : Start time of the current cycle.
347- kwargs (dict[str, Any]) : Additional keyword arguments, including request state.
334+ stop_reason: The reason the model stopped generating.
335+ message: The message from the model that may contain tool use requests.
336+ model: The model provider instance.
337+ system_prompt: The system prompt instructions for the model.
338+ messages: The conversation history messages.
339+ tool_config: Configuration for available tools.
340+ tool_handler: Handler for tool execution.
341+ thread_pool : Optional thread pool for parallel tool execution.
342+ event_loop_metrics: Metrics tracking object for the event loop.
343+ event_loop_parent_span: Span for the parent of this event loop.
344+ cycle_trace: Trace object for the current event loop cycle.
345+ cycle_span: Span object for tracing the cycle (type may vary).
346+ cycle_start_time: Start time of the current cycle.
347+ kwargs: Additional keyword arguments, including request state.
348348
349349 Yields:
350350 Tool invocation events along with events yielded from a recursive call to the event loop. The last event is a
@@ -377,7 +377,7 @@ def _handle_tool_execution(
377377 tool_results = tool_results ,
378378 cycle_trace = cycle_trace ,
379379 parent_span = cycle_span ,
380- parallel_tool_executor = tool_execution_handler ,
380+ thread_pool = thread_pool ,
381381 )
382382
383383 # Store parent cycle ID for the next cycle
@@ -406,7 +406,7 @@ def _handle_tool_execution(
406406 messages = messages ,
407407 tool_config = tool_config ,
408408 tool_handler = tool_handler ,
409- tool_execution_handler = tool_execution_handler ,
409+ thread_pool = thread_pool ,
410410 event_loop_metrics = event_loop_metrics ,
411411 event_loop_parent_span = event_loop_parent_span ,
412412 kwargs = kwargs ,
0 commit comments