diff --git a/lib/crewai/src/crewai/agents/crew_agent_executor.py b/lib/crewai/src/crewai/agents/crew_agent_executor.py index 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 annotations import asyncio from collections.abc import Callable +import contextvars from concurrent.futures import ThreadPoolExecutor, as_completed import inspect import 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.py index 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 annotations import asyncio +import contextvars from collections.abc import Callable, Coroutine from concurrent.futures import ThreadPoolExecutor, as_completed from 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): idx for idx, tool_call in enumerate(runnable_tool_calls) } ordered_results: list[dict[str, Any] | None] = [None] * len(