Cross-Framework LLM Tool Integration with AG2
TL;DR AG2 lets you bring in Tools from different frameworks like LangChain and PydanticAI.
- LangChain Tools: Useful for tasks like API querying and web scraping.
- PydanticAI Tools: Adds context-driven tools and structured data processing.
With AG2, you can combine these tools and enhance your agents' capabilities.
In this post, we’ll walk through how to integrate tools from various frameworks—like LangChain Tools and PydanticAI Tools—into AG2.
Because, really, the magic happens when you combine them all. This allows you to use tools from different frameworks within AG2, giving your agents more power and flexibility. This blog builds upon the concepts covered in the Tool Integration notebook.
In this post, you will understand how to configure agents, adapt these tools for use in AG2, and validate the integration through practical examples.
LangChain is a popular framework with lots of tools for working with LLMs. It's got a range of tools that can be easily integrated into AG2. If you want to see the full list, check out the LangChain Community Tools. You can quickly add things like API queries, web scraping, and text generation to your AG2 setup.
Installation#
To get LangChain tools working with AG2, you’ll need to install a couple of dependencies:
autogen or ag2, all you need to do is upgrade it using:
autogen and ag2 are aliases for the same PyPI package. Also, we’ll use LangChain’s Wikipedia Tool, which needs the wikipedia package. Install it like this:
Imports#
Now, let’s import the necessary modules and tools.
- WikipediaQueryRun and WikipediaAPIWrapper are the tools for querying Wikipedia.
AssistantAgentandUserProxyAgentare the agents for interaction within AG2.Interoperabilityis what helps connect LangChain tools with AG2.
import os
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.interop import Interoperability
Agent Configuration#
Let’s set up the agents for interaction. - config_list is where you define the LLM configuration, like the model and API key. - UserProxyAgent simulates user inputs without requiring actual human interaction (set to NEVER). - AssistantAgent represents the AI agent, configured with the LLM settings.
llm_config = LLMConfig(config_list={"api_type": "openai", "model": "gpt-5", "api_key": os.environ["OPENAI_API_KEY"]})
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
)
chatbot = AssistantAgent(name="chatbot", llm_config=llm_config)
Tool Integration#
Here’s where we connect everything.
- First, we set up WikipediaAPIWrapper, which fetches the top Wikipedia result (with a character limit).
- Then, we use WikipediaQueryRun to perform Wikipedia queries.
Interoperabilityhelps convert the LangChain tool to AG2’s format.- Finally, we register the tool for use with both the
user_proxyandchatbot.
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)
langchain_tool = WikipediaQueryRun(api_wrapper=api_wrapper)
interop = Interoperability()
ag2_tool = interop.convert_tool(tool=langchain_tool, type="langchain")
ag2_tool.register_for_execution(user_proxy)
ag2_tool.register_for_llm(chatbot)
Initiating the Chat#
Once everything’s set up, we can send a message to the chatbot, and it’ll use the Wikipedia tool to fetch the relevant information.
message = "Tell me about the history of the United States"
user_proxy.initiate_chat(recipient=chatbot, message=message, max_turns=2)
Output#
When the chat is initiated, here’s the output you’ll see:
User (to chatbot):
Tell me about the history of the United States
--------------------------------------------------------------------------------
chatbot (to User):
***** Suggested tool call (call_hhy2G43ymytUFmJlDsK9J0tk): wikipedia *****
Arguments:
{"tool_input":{"query":"history of the United States"}}
**************************************************************************
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING FUNCTION wikipedia...
User (to chatbot):
***** Response from calling tool (call_hhy2G43ymytUFmJlDsK9J0tk) *****
Page: History of the United States
Summary: The history of the lands that became the United States began with the arrival of the first people in the Americas around 15,000 BC. After European colonization of North America began in the late 15th century, wars and epidemics decimated Indigenous societies. By the 1760s, the thirteen British colonies were established. The Southern Colonies built an agricultural system on slave labor, enslaving millions from Africa. After defeating France, the British Parliament imposed a series of taxes; resistance to these taxes, especially the Boston Tea Party in 1773, led to Parliament issuing the Intolerable Acts designed to end self-government.
In 1776, the United States declared its independence. Led by General George Washington, it won the Revolutionary War in 1783. The Constitution was adopted in 1789, and a Bill of Rights was added in 1791 to guarantee inalienable rights. Washington, the first president, and his adviser Alexander Hamilton created a
**********************************************************************
--------------------------------------------------------------------------------
chatbot (to User):
The history of the United States begins with the arrival of the first peoples in the Americas around 15,000 BC. This pre-Columbian era was followed by European colonization, beginning in the late 15th century, which dramatically altered the indigenous societies through wars and epidemics.
By the 1760s, thirteen British colonies were established along the Atlantic seaboard. In the Southern Colonies, an agricultural economy heavily reliant on enslaved labor from Africa was developed. The British victory over France in the Seven Years' War led Parliament to impose various taxes on the colonies. Resistance to these taxes, exemplified by the Boston Tea Party in 1773, prompted the Parliament to enact the Intolerable Acts, seeking to curtail colonial self-governance.
The United States declared independence in 1776. Under the leadership of General George Washington, the American Revolutionary War concluded successfully in 1783. Subsequently, the U.S. Constitution was adopted in 1789, with the Bill of Rights added in 1791 to ensure inalienable rights. During this early period, President George Washington and his advisor Alexander Hamilton played significant roles in forming the young nation's governmental and economic foundations.
This overview covers the early formation and foundational moments of what became the United States, setting the stage for the country's subsequent expansion and development. TERMINATE
--------------------------------------------------------------------------------
PydanticAI is a newer framework that brings powerful features for working with LLMs. Although it doesn't yet have a collection of pre-built tools like other frameworks, it offers useful capabilities such as dependency injection. This feature allows you to inject a "Context" into tools, which can help pass parameters or manage state without relying on LLMs. Though it's still evolving, you can easily integrate PydanticAI tools into AG2 to boost agent capabilities, particularly for tasks that involve structured data and context-driven logic.
Installation#
To get PydanticAI tools working with AG2, install the necessary dependencies:
Imports#
Import necessary modules and tools.
- BaseModel: Used to define data structures for tool inputs and outputs.
- RunContext: Provides context during the execution of tools.
- PydanticAITool: Represents a tool in the PydanticAI framework.
AssistantAgentandUserProxyAgent: Agents that facilitate communication in the AG2 framework.Interoperability: This module acts as a bridge, making it easier to integrate PydanticAI tools with AG2’s architecture.
import os
from typing import Optional
from pydantic import BaseModel
from pydantic_ai import RunContext
from pydantic_ai.tools import Tool as PydanticAITool
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.interop import Interoperability
Agent Configuration#
Configure the agents for the interaction. - config_list defines the LLM configurations, including the model and API key. - UserProxyAgent simulates user inputs without requiring actual human interaction (set to NEVER). - AssistantAgent represents the AI agent, configured with the LLM settings.
llm_config = LLMConfig(config_list={"api_type": "openai", "model": "gpt-5", "api_key": os.environ["OPENAI_API_KEY"]})
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
)
chatbot = AssistantAgent(
name="chatbot",
llm_config=llm_config,
)
Tool Integration#
To integrate a PydanticAI tool into AG2:
- First, define a
Playermodel using BaseModel to structure the input data. - Use RunContext to inject dependencies (like the
Playerinstance) securely into the tool. - The
get_playerfunction defines the tool’s functionality, retrieving injected data throughctx.deps. - Then, convert the tool into an AG2-compatible format with
Interoperability. - Register the tool for execution and interaction with both the
user_proxyandchatbot.
class Player(BaseModel):
name: str
age: int
def get_player(ctx: RunContext[Player], additional_info: Optional[str] = None) -> str: # type: ignore[valid-type]
"""Get the player's name.
Args:
additional_info: Additional information which can be used.
"""
return f"Name: {ctx.deps.name}, Age: {ctx.deps.age}, Additional info: {additional_info}" # type: ignore[attr-defined]
interop = Interoperability()
pydantic_ai_tool = PydanticAITool(get_player, takes_ctx=True)
# player will be injected as a dependency
player = Player(name="Luka", age=25)
ag2_tool = interop.convert_tool(tool=pydantic_ai_tool, type="pydanticai", deps=player)
ag2_tool.register_for_execution(user_proxy)
ag2_tool.register_for_llm(chatbot)
Initiating the chat#
Now that everything is set up, you can initiate a chat between the UserProxyAgent and the AssistantAgent:
- The
user_proxysends a message to thechatbot. - The user requests player information, and includes "goal keeper" as additional context.
- The
Playerdata is securely injected into the tool, and the chatbot can access and use it during the chat.
user_proxy.initiate_chat(
recipient=chatbot, message="Get player, for additional information use 'goal keeper'", max_turns=3
)
Output#
User (to chatbot):
Get player, for additional information use 'goal keeper'
--------------------------------------------------------------------------------
chatbot (to User):
***** Suggested tool call (call_lPXIohFiJfnjmgwDnNFPQCzc): get_player *****
Arguments:
{"additional_info":"goal keeper"}
***************************************************************************
--------------------------------------------------------------------------------
>>>>>>>> EXECUTING FUNCTION get_player...
User (to chatbot):
***** Response from calling tool (call_lPXIohFiJfnjmgwDnNFPQCzc) *****
Name: Luka, Age: 25, Additional info: goal keeper
**********************************************************************
--------------------------------------------------------------------------------
chatbot (to User):
The player's name is Luka, who is a 25-year-old goalkeeper. TERMINATE
LangChain Tools Integration#
PydanticAI Tools Integration#
Summary#
In this post, we've explored how to integrate tools from multiple frameworks (LangChain and PydanticAI) into the AG2 framework, enabling cross-framework interoperability. By integrating these tools, you can enhance your agents with a variety of capabilities, such as API querying, web scraping, and structured data processing.
- LangChain offers a wide range of pre-built tools for working with APIs and web scraping, making it easy to extend AG2's functionality.
- PydanticAI introduces dependency injection and context-driven logic, enabling efficient data handling without relying on LLMs.
With AG2's flexible architecture and the power of these frameworks, developers can create agents that are more capable and adaptable. By following the integration steps for each framework, you can enhance your agents' performance, expand their capabilities, and create more dynamic interactions.
Now you should have a better understanding of how to integrate tools from different frameworks into AG2, and how to use these tools effectively within your own projects.