feat: Add asynchronous tool calling support #4756
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #4755
Background
This PR implements the asynchronous tool calling support proposed in #4755.
I've created a complete implementation to demonstrate the feasibility and benefits of this approach. The community is welcome to review both the proposal (#4755) and this implementation together.
If there are concerns about the approach, I'm happy to make adjustments or discuss alternatives.
Overview
This PR adds optional asynchronous tool calling support to Spring AI, enabling significant performance improvements in I/O-intensive and high-concurrency scenarios while maintaining 100% backward compatibility.
Changes
Core Components
1. New
AsyncToolCallbackInterfaceToolCallbackinterfacecallAsync(String, ToolContext)method returningMono<String>supportsAsync()flag for runtime detectionLocation:
spring-ai-model/src/main/java/org/springframework/ai/tool/AsyncToolCallback.java2. Enhanced
ToolCallingManagerexecuteToolCallsAsync(Prompt, ChatResponse)returningMono<ToolExecutionResult>executeToolCalls(Prompt, ChatResponse)unchangedAsyncToolCallback, wrapped forToolCallbackLocation:
spring-ai-model/src/main/java/org/springframework/ai/model/tool/ToolCallingManager.java3. Updated
DefaultToolCallingManagerLocation:
spring-ai-model/src/main/java/org/springframework/ai/model/tool/DefaultToolCallingManager.javaChatModel Integration
All 11 ChatModel implementations updated to support both sync and async execution:
OpenAiChatModelAnthropicChatModelOllamaChatModelAzureOpenAiChatModelBedrockChatModelBedrockConverseChatModelDeepSeekChatModelGoogleGenAiChatModelMiniMaxChatModelMistralAiChatModelZhiPuAiChatModelTesting
New Tests: 15 async-specific test cases
AsyncToolCallbackTests- Interface contract testsDefaultToolCallingManagerAsyncTests- Async execution testsTest Results:
Design Principles
1. 100% Backward Compatible ✅
No breaking changes:
ToolCallbackinterface unchangedExample - Existing code continues to work:
2. Optional Upgrade Path ✅
Progressive migration:
Example - New async tool:
3. Intelligent Fallback ✅
Automatic handling:
4. Spring Ecosystem Alignment ✅
Follows established patterns:
Performance Impact
Benchmarks
Scenario: 3 concurrent tool calls (HTTP requests, 500ms each)
Thread Utilization:
Streaming Performance:
Code Quality
Testing
Code Standards
Documentation
Usage Examples
Example 1: Existing Code (No Changes)
Example 2: New Async Tools (Performance Boost)
Example 3: Mixed Sync and Async Tools
Related Issues
Closes #4755
Contributor Checklist
git commit -s)mainbranchAdditional Notes
Why This Implementation?
Future Enhancements
This PR provides the foundation for:
Thank you for reviewing this contribution! I'm happy to make any adjustments based on feedback.