Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

Parallel Tool Execution for ReAct

This PR adds support for parallel tool execution in the ReAct module, allowing the LLM to request multiple tool calls in a single turn and execute them concurrently.

Plan

  • Explore the codebase and understand current ReAct implementation
  • Understand existing Tool and ToolCalls classes
  • Run existing tests to ensure baseline functionality
  • Modify ReAct signature to support multiple tool calls output
  • Implement parallel tool execution in sync forward method using ThreadPoolExecutor
  • Implement parallel tool execution in async aforward method using asyncio.gather
  • Add comprehensive tests for parallel tool execution (both sync and async)
  • Test with multiple tools being called simultaneously
  • Ensure backward compatibility with single tool calls
  • Update existing tests to new format
  • Run linting and all tests
  • Request code review
  • Address code review feedback
  • Merge all tests into single test file

Summary

This implementation allows the LLM to request multiple tool calls in a single turn, which are then executed concurrently:

  • Sync execution: Uses ThreadPoolExecutor for parallel execution
  • Async execution: Uses asyncio.gather for parallel execution
  • Trajectory format: Updated to support multiple tool calls and observations per iteration
  • Backward compatible: Works with single tool calls as well
  • Error handling: Each tool execution is isolated, errors in one tool don't affect others
  • Consolidated tests: All tests now in test_react.py for better organization

Key Changes

  1. Signature Update: Changed from next_tool_name + next_tool_args to next_tool_calls (list of dicts)
  2. Parallel Execution: Implemented using ThreadPoolExecutor (sync) and asyncio.gather (async)
  3. Trajectory Format: Stores tool_calls_X (list) and observations_X (list of dicts) instead of individual fields
  4. Comprehensive Tests: Added 7 new tests covering various parallel execution scenarios, all in test_react.py
  5. Updated Tests: All 6 existing tests updated to use new format
Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature] Parallel tool execution</issue_title>
<issue_description>### What feature would you like to see?

Is there any plan to make parallel tool execution feature for React etc?

Would you like to contribute?

  • Yes, I'd like to help implement this.
  • No, I just want to request it.

Additional Context

No response</issue_description>

<agent_instructions>Can you support parallel tool calling in ReAct? Notice that we need to support both sync and async apis</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@TomeHirata Hi, @quancore? Did you face a situation where LLM requests multiple tool calls in one turn, and each tool call took time? Would love to know more details to decide the priority!

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Oct 30, 2025
Copilot AI and others added 2 commits October 30, 2025 08:44
- Modified ReAct signature to accept next_tool_calls list instead of single tool_name/tool_args
- Implemented parallel execution for sync using ThreadPoolExecutor
- Implemented parallel execution for async using asyncio.gather
- Updated trajectory format to support multiple tool calls and observations per iteration
- Added comprehensive tests for parallel tool execution (both sync and async)
- Updated all existing tests to use new format
- All tests passing with proper linting

Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
- Remove unnecessary list() conversion from asyncio.gather return
- Remove unused elapsed_time calculations from tests
- All tests still passing with proper linting

Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Copilot AI changed the title [WIP] Add parallel tool execution feature for React Add parallel tool execution to ReAct Oct 30, 2025
Copilot AI requested a review from TomeHirata October 30, 2025 08:51
Copilot finished work on behalf of TomeHirata October 30, 2025 08:51
@@ -0,0 +1,374 @@
"""Tests for parallel tool execution in ReAct."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot can we have all tests in tests/predict/test_react.py?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! All tests have been consolidated into tests/predict/test_react.py and test_react_parallel.py has been removed. (commit 741ac11)

- Consolidated all parallel tool execution tests into test_react.py
- Removed test_react_parallel.py as requested in code review
- Added asyncio and time imports to support parallel tests
- All 14 tests passing with proper linting

Co-authored-by: TomeHirata <33407409+TomeHirata@users.noreply.github.com>
Copilot AI requested a review from TomeHirata October 30, 2025 10:10
Copilot finished work on behalf of TomeHirata October 30, 2025 10:10
@chenmoneygithub
Copy link
Collaborator

I am worrying that the change to prompt can lead to performance regression, since it might be possible LM does a worse job selecting multiple tools in a bulk. We can run benchmarks to verify it, or probably we should introduce a new mode for parallel execution?

@copilot what's your thought on this?

Copy link
Contributor Author

Copilot AI commented Oct 30, 2025

I am worrying that the change to prompt can lead to performance regression, since it might be possible LM does a worse job selecting multiple tools in a bulk. We can run benchmarks to verify it, or pr...

You raise a valid concern. The current implementation does change the prompt for all ReAct instances, which could impact performance in single-tool scenarios.

A few thoughts on addressing this:

  1. The change is actually backward compatible in practice: The LLM can still return a single tool call by providing a list with one element [{"name": "tool", "args": {...}}]. The prompt explicitly states "You can call multiple tools in parallel" (line 59) rather than requiring it, so the LLM can choose to call just one tool.

  2. Potential solutions if benchmarks show regression:

    • Add an optional parallel_tools=True/False parameter to __init__ that switches between the old and new prompt/signature
    • Make parallel the default but allow users to opt-out for simpler cases
    • Keep current implementation but add better guidance in the prompt to help LLM decide when to use single vs multiple tools
  3. Recommendation: I'd suggest running benchmarks on common single-tool scenarios first. If there's measurable regression, we can add a mode parameter. If performance is similar, the simpler unified approach may be preferable.

Would you like me to implement an optional mode parameter, or would you prefer to evaluate performance first?

Copilot finished work on behalf of chenmoneygithub October 30, 2025 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Parallel tool execution

3 participants