fix: support async LangChain tools in convert_tool()#2618
Open
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Open
fix: support async LangChain tools in convert_tool()#2618Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Conversation
…rt_tool() When a LangChain tool provides a native async implementation (overrides _arun), the converted AG2 Tool now uses an async function that calls ainvoke() instead of the synchronous run(). This allows the tool to be properly awaited inside a_initiate_chat() and other async execution paths, preventing event loop blocking. Tools without async support continue to use the synchronous run() method, preserving full backward compatibility. Fixes ag2ai#1402
The mypy strict mode flags langchain types as Any due to no type stubs. Add type ignores following the existing pattern in langchain_tool.py and replace LangchainBaseTool annotations in test fixtures with Any. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
... and 122 files with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #1402
LangChainInteroperability.convert_tool()always wraps LangChain tools with a synchronous function that callslangchain_tool.run(), even when the tool provides native async support via_arun. This blocks the event loop when used insidea_initiate_chat()and other async execution paths._arun(i.e., provides a real async implementation beyond BaseTool's defaultNotImplementedErrorstub), the converted tool now uses anasync deffunction that callsainvoke(). Tools without async support continue to use the synchronousrun()method, preserving full backward compatibility.a_execute_function()already checksis_coroutine_callable(func)and properly awaits async functions. By providing an async function when the LangChain tool supports it, the tool integrates naturally with the existing async execution path.Changes
autogen/interop/langchain/langchain_tool.py:_langchain_tool_has_async_implementation()helper that walks the MRO to detect if the tool's_arunis overridden fromBaseTool's defaultconvert_tool()to create an async wrapper usingainvoke()when async support is detectedtest/interop/langchain/test_langchain_async.py(new):await_langchain_tool_has_async_implementationdetection helperTest plan
pytest test/interop/langchain/test_langchain_async.py— new async tests passpytest test/interop/langchain/test_langchain.py— existing sync tests still pass (regression)_arunoverride🤖 Generated with Claude Code