Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix(executor): propagate contextvars context to parallel tool call threads
Tiago Freire059cb93aeb01
diff --git a/lib/crewai/src/crewai/agents/crew_agent_executor.py b/lib/crewai/src/crewai/agents/crew_agent_executor.pyindex ff40489d9..ac1cccbeb 100644--- a/lib/crewai/src/crewai/agents/crew_agent_executor.py+++ b/lib/crewai/src/crewai/agents/crew_agent_executor.py@@ -8,6 +8,7 @@ from __future__ import annotationsimport asynciofrom collections.abc import Callable+import contextvarsfrom concurrent.futures import ThreadPoolExecutor, as_completedimport inspectimport logging@@ -755,6 +756,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):with ThreadPoolExecutor(max_workers=max_workers) as pool:futures = {pool.submit(+ contextvars.copy_context().run,self._execute_single_native_tool_call,call_id=call_id,func_name=func_name,diff --git a/lib/crewai/src/crewai/experimental/agent_executor.py b/lib/crewai/src/crewai/experimental/agent_executor.pyindex 4f2a92681..b0662f6c6 100644--- a/lib/crewai/src/crewai/experimental/agent_executor.py+++ b/lib/crewai/src/crewai/experimental/agent_executor.py@@ -1,6 +1,7 @@from __future__ import annotationsimport asyncio+import contextvarsfrom collections.abc import Callable, Coroutinefrom concurrent.futures import ThreadPoolExecutor, as_completedfrom datetime import datetime@@ -728,7 +729,7 @@ class AgentExecutor(Flow[AgentReActState], CrewAgentExecutorMixin):max_workers = min(8, len(runnable_tool_calls))with ThreadPoolExecutor(max_workers=max_workers) as pool:future_to_idx = {- pool.submit(self._execute_single_native_tool_call, tool_call): idx+ pool.submit(contextvars.copy_context().run, self._execute_single_native_tool_call, tool_call): idxfor idx, tool_call in enumerate(runnable_tool_calls)}ordered_results: list[dict[str, Any] | None] = [None] * len(