Skip to content

fix: support async LangChain tools in convert_tool()#2618

Open
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Ricardo-M-L:fix/langchain-async-tool-support
Open

fix: support async LangChain tools in convert_tool()#2618
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Ricardo-M-L:fix/langchain-async-tool-support

Conversation

@Ricardo-M-L
Copy link
Copy Markdown
Contributor

Summary

Fixes #1402

  • Problem: LangChainInteroperability.convert_tool() always wraps LangChain tools with a synchronous function that calls langchain_tool.run(), even when the tool provides native async support via _arun. This blocks the event loop when used inside a_initiate_chat() and other async execution paths.
  • Solution: Added async detection via MRO inspection. When a LangChain tool overrides _arun (i.e., provides a real async implementation beyond BaseTool's default NotImplementedError stub), the converted tool now uses an async def function that calls ainvoke(). Tools without async support continue to use the synchronous run() method, preserving full backward compatibility.
  • How it works with the framework: AG2's a_execute_function() already checks is_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:

  • Added _langchain_tool_has_async_implementation() helper that walks the MRO to detect if the tool's _arun is overridden from BaseTool's default
  • Modified convert_tool() to create an async wrapper using ainvoke() when async support is detected
  • Sync path remains unchanged for backward compatibility

test/interop/langchain/test_langchain_async.py (new):

  • Tests that sync-only tools remain sync after conversion
  • Tests that async tools produce async converted functions
  • Tests async tool execution via await
  • Tests concurrent execution (async tool doesn't block event loop)
  • Tests metadata preservation, error handling
  • Tests for the _langchain_tool_has_async_implementation detection helper

Test plan

  • pytest test/interop/langchain/test_langchain_async.py — new async tests pass
  • pytest test/interop/langchain/test_langchain.py — existing sync tests still pass (regression)
  • Verify async detection correctly identifies tools with/without _arun override

🤖 Generated with Claude Code

Ricardo-M-L and others added 2 commits April 12, 2026 00:19
…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
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

❌ Patch coverage is 6.66667% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
autogen/interop/langchain/langchain_tool.py 6.66% 14 Missing ⚠️
Files with missing lines Coverage Δ
autogen/interop/langchain/langchain_tool.py 44.44% <6.66%> (-18.89%) ⬇️

... and 122 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

[Bug]: Synchronous Call of Asynchronous Langchain Tool in Autogen Interoperability System

1 participant