LangChain Integration¶
This section documents the LangChain integration capabilities of the ToolRegistry library.
Architecture Overview¶
The LangChain integration enables seamless interoperability between LangChain tools and the ToolRegistry ecosystem. This integration allows LangChain's extensive tool ecosystem to be used within the ToolRegistry framework:
Core Components¶
-
LangChainToolWrapper: A wrapper class that bridges LangChain tools with ToolRegistry's unified interface
-
Provides both synchronous (
_run) and asynchronous (_arun) execution methods - Manages parameter mapping between LangChain and ToolRegistry formats
-
Handles error propagation and logging
-
LangChainTool: A tool class that wraps LangChain BaseTool instances
-
Preserves original tool metadata and descriptions
- Converts LangChain input schemas to ToolRegistry format
-
Maintains namespace support for tool organization
-
LangChainIntegration: The main integration class that orchestrates the bridging process
- Manages the conversion from LangChain tools to ToolRegistry tools
- Supports both individual tool and batch registration patterns
- Handles schema transformation and normalization
Design Philosophy¶
- Non-invasive Integration: Preserves original LangChain tool behavior
- Schema Compatibility: Automatic conversion between LangChain and ToolRegistry schemas
- Error Transparency: Original LangChain exceptions are preserved and enhanced with context
- Async Support: Full compatibility with LangChain's async execution model
Key Features¶
- Direct integration with LangChain's
BaseToolinstances - Automatic schema transformation from LangChain to ToolRegistry format
- Support for both synchronous and asynchronous execution modes
- Namespace support for organizing LangChain tools
- Preserved error handling and logging from original LangChain tools
- Minimal overhead - no additional dependencies or transformations
Usage Patterns¶
- Single Tool Integration: Register individual LangChain tools
- Tool Collections: Integrate multiple LangChain tools from collections
- Namespace Organization: Group LangChain tools under common namespaces
- Error Handling: Maintain LangChain's original exception behavior with enhanced context
API Reference¶
LangChainToolWrapper¶
Wrapper class providing both async and sync versions of LangChain tool calls.
toolregistry.integrations.langchain.integration.LangChainToolWrapper ¶
Bases: BaseToolWrapper
Wrapper class providing both async and sync versions of LangChain tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool |
BaseTool
|
The LangChain tool instance. |
name |
str
|
Name of the tool. |
description |
str
|
Description of the tool. |
params |
List[str]
|
List of parameter names. |
Initialize LangChain tool wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
call_async
async
¶
Async implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
call_sync ¶
Synchronous implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
LangChainTool¶
Wrapper class for LangChain tools that preserves original function metadata.
toolregistry.integrations.langchain.integration.LangChainTool ¶
Bases: Tool
Wrapper class for LangChain tools that preserves original function metadata.
from_langchain_tool
classmethod
¶
Create a LangChainTool instance from a LangChain LCBaseTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
namespace
|
Optional[str]
|
An optional namespace to prefix the tool name. If provided, the tool name will be formatted as "{namespace}.{name}". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
LangChainTool |
LangChainTool
|
A new instance of LangChainTool. |
LangChainIntegration¶
Handles integration with LangChain tools for registration.
toolregistry.integrations.langchain.integration.LangChainIntegration ¶
Handles integration with LangChain tools for registration.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
ToolRegistry
|
Tool registry instance. |
register_langchain_tools ¶
Register LangChain tool (synchronous entry point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
register_langchain_tools_async
async
¶
Async implementation to register LangChain tool using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If tools argument is invalid. |
Module Overview¶
LangChain Module¶
The main LangChain integration module.
toolregistry.integrations.langchain ¶
LangChainIntegration ¶
Handles integration with LangChain tools for registration.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
ToolRegistry
|
Tool registry instance. |
register_langchain_tools ¶
Register LangChain tool (synchronous entry point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
register_langchain_tools_async
async
¶
Async implementation to register LangChain tool using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If tools argument is invalid. |
LangChainTool ¶
Bases: Tool
Wrapper class for LangChain tools that preserves original function metadata.
from_langchain_tool
classmethod
¶
Create a LangChainTool instance from a LangChain LCBaseTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
namespace
|
Optional[str]
|
An optional namespace to prefix the tool name. If provided, the tool name will be formatted as "{namespace}.{name}". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
LangChainTool |
LangChainTool
|
A new instance of LangChainTool. |
LangChainToolWrapper ¶
Bases: BaseToolWrapper
Wrapper class providing both async and sync versions of LangChain tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool |
BaseTool
|
The LangChain tool instance. |
name |
str
|
Name of the tool. |
description |
str
|
Description of the tool. |
params |
List[str]
|
List of parameter names. |
Initialize LangChain tool wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
call_async
async
¶
Async implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
call_sync ¶
Synchronous implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |