Skip to content

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

  1. LangChainToolWrapper: A wrapper class that bridges LangChain tools with ToolRegistry's unified interface

  2. Provides both synchronous (_run) and asynchronous (_arun) execution methods

  3. Manages parameter mapping between LangChain and ToolRegistry formats
  4. Handles error propagation and logging

  5. LangChainTool: A tool class that wraps LangChain BaseTool instances

  6. Preserves original tool metadata and descriptions

  7. Converts LangChain input schemas to ToolRegistry format
  8. Maintains namespace support for tool organization

  9. LangChainIntegration: The main integration class that orchestrates the bridging process

  10. Manages the conversion from LangChain tools to ToolRegistry tools
  11. Supports both individual tool and batch registration patterns
  12. 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 BaseTool instances
  • 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

LangChainToolWrapper(tool: BaseTool)

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

call_async(*args: Any, **kwargs: Any) -> Any

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

call_sync(*args: Any, **kwargs: Any) -> Any

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

from_langchain_tool(tool: BaseTool, namespace: str | None = None) -> LangChainTool

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

LangChainIntegration(registry: ToolRegistry)

Handles integration with LangChain tools for registration.

Attributes:

Name Type Description
registry ToolRegistry

Tool registry instance.

register_langchain_tools

register_langchain_tools(tool: BaseTool, namespace: bool | str = False) -> None

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, no namespace is used. - If True, the namespace is derived from the tool's metadata if available. - If a string is provided, it is used as the namespace. Defaults to False.

False

register_langchain_tools_async async

register_langchain_tools_async(tool: BaseTool, namespace: bool | str = False) -> None

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, no namespace is used. - If True, the namespace is derived from the tool's metadata if available. - If a string is provided, it is used as the namespace. Defaults to False.

False

Raises:

Type Description
ValueError

If tools argument is invalid.

Module Overview

LangChain Module

The main LangChain integration module.

toolregistry.integrations.langchain

LangChainIntegration

LangChainIntegration(registry: ToolRegistry)

Handles integration with LangChain tools for registration.

Attributes:

Name Type Description
registry ToolRegistry

Tool registry instance.

register_langchain_tools

register_langchain_tools(tool: BaseTool, namespace: bool | str = False) -> None

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, no namespace is used. - If True, the namespace is derived from the tool's metadata if available. - If a string is provided, it is used as the namespace. Defaults to False.

False

register_langchain_tools_async async

register_langchain_tools_async(tool: BaseTool, namespace: bool | str = False) -> None

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, no namespace is used. - If True, the namespace is derived from the tool's metadata if available. - If a string is provided, it is used as the namespace. Defaults to False.

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

from_langchain_tool(tool: BaseTool, namespace: str | None = None) -> LangChainTool

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

LangChainToolWrapper(tool: BaseTool)

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

call_async(*args: Any, **kwargs: Any) -> Any

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

call_sync(*args: Any, **kwargs: Any) -> Any

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.