Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/agents/run_internal/tool_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,13 +1335,24 @@ async def execute(

def _create_tool_task(self, tool_run: ToolRunFunction, order: int) -> None:
task_state = _FunctionToolTaskState(tool_run=tool_run, order=order)
task = asyncio.create_task(
self._run_single_tool(

# Under asyncio.eager_task_factory, the coroutine starts executing inside
# create_task() before it returns. This creates a race: the task could
# complete and be processed by _partition_pending_tasks before
# self.task_states[task] is set on the next line, causing KeyError.
#
# Fix: wrap in an inner async function so eager execution runs the
# wrapper's synchronous preamble (nothing) and yields at the first
# await, returning control to this method to finish registration.
# See: https://github.com/openai/openai-agents-python/issues/2729
async def _run() -> Any:
return await self._run_single_tool(
task_state=task_state,
func_tool=tool_run.function_tool,
tool_call=tool_run.tool_call,
)
)

task = asyncio.create_task(_run())
self.task_states[task] = task_state
self.pending_tasks.add(task)

Expand Down