Skip to content

fix(agents): await native async tools on caller loop#6616

Open
Sakshamm-Goyal wants to merge 1 commit into
crewAIInc:mainfrom
Sakshamm-Goyal:fix/6611-native-async-tools
Open

fix(agents): await native async tools on caller loop#6616
Sakshamm-Goyal wants to merge 1 commit into
crewAIInc:mainfrom
Sakshamm-Goyal:fix/6611-native-async-tools

Conversation

@Sakshamm-Goyal

Copy link
Copy Markdown

Summary

  • dispatch awaitable native-tool results back to the active flow event loop instead of creating a per-call worker loop
  • preserve synchronous native-tool execution, argument validation, usage limits, hooks, caching, and ordering
  • cover async BaseTool._run, wrapped async functions, parallel calls, and flow-route registration

Closes #6611

Validation

  • pytest lib/crewai/tests/agents/test_agent_executor.py -q -n 0 --timeout=60 --block-network --import-mode=importlib -k "not anthropic_provider_has_image_block_converter and not kickoff_response_format_with_planning_and_tools" (98 passed, 2 deselected because optional Anthropic/Exa dependencies are unavailable)
  • pytest lib/crewai/tests/agents/test_agent_executor.py::TestNativeToolExecution -q -n 0 --timeout=60 --block-network --import-mode=importlib (8 passed)
  • mypy lib/crewai/src/crewai/experimental/agent_executor.py
  • ruff check and ruff format --check on changed files

@Sakshamm-Goyal

Copy link
Copy Markdown
Author

This PR was authored with AI assistance. The repository contribution policy requires the llm-generated label, but the contributor account does not have permission to apply it. Could a maintainer add that required label? Thank you.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a945ef34-4d5f-4663-8ad0-da567adb5a7f

📥 Commits

Reviewing files that changed from the base of the PR and between 3bb8753 and 0facc23.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/experimental/agent_executor.py
  • lib/crewai/tests/agents/test_agent_executor.py

📝 Walkthrough

Walkthrough

The experimental AgentExecutor now routes native tool calls through an async entry point, captures the active event loop, and executes awaitable BaseTool results on that loop. Tests cover flow wiring, thread affinity, parallel calls, and recorded tool outputs.

Changes

Async native tool execution

Layer / File(s) Summary
Async dispatch and flow wiring
lib/crewai/src/crewai/experimental/agent_executor.py
Native tool flow transitions use execute_native_tool_async, which captures the running loop, delegates synchronous orchestration to a worker thread, and restores the context variable afterward.
Awaitable tool execution and regression coverage
lib/crewai/src/crewai/experimental/agent_executor.py, lib/crewai/tests/agents/test_agent_executor.py
BaseTool calls use _run_native_tool to complete awaitable results on the captured loop or a new loop, with tests covering wiring, caller-thread execution, parallel calls, and executor state messages.

Sequence Diagram(s)

sequenceDiagram
  participant Flow
  participant AgentExecutor
  participant WorkerThread
  participant BaseTool
  participant EventLoop
  Flow->>AgentExecutor: execute_native_tool_async()
  AgentExecutor->>EventLoop: capture running loop
  AgentExecutor->>WorkerThread: execute_native_tool()
  WorkerThread->>BaseTool: _run_native_tool()
  BaseTool-->>EventLoop: schedule awaitable result
  EventLoop-->>AgentExecutor: return tool result
  AgentExecutor-->>Flow: native tool completion
Loading

Suggested reviewers: lucasgomide

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: awaiting native async tools on the caller loop.
Description check ✅ Passed The description matches the code changes and test coverage, and stays on-topic.
Linked Issues check ✅ Passed The changes implement #6611 by awaiting async native tools on the active loop while preserving synchronous paths and flow routing.
Out of Scope Changes check ✅ Passed The diff is limited to the executor fix and targeted regression tests, with no unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant