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: Any)

Bases: BaseToolWrapper

Wrapper providing sync and async entry points for a LangChain-style tool.

Attributes:

Name Type Description
tool Any

The wrapped object.

name str

Normalized tool name.

params list[str]

Parameter names.

Initialize the wrapper.

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required

call_async async

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

Run the tool asynchronously.

Uses the tool's async runner when available; otherwise the sync runner runs in a worker thread so the event loop is not blocked.

Parameters:

Name Type Description Default
args Any

Positional arguments.

()
kwargs Any

Keyword arguments.

{}

Returns:

Type Description
Any

The tool's return value.

Raises:

Type Description
Exception

Whatever the tool raises.

call_sync

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

Run the tool synchronously.

Parameters:

Name Type Description Default
args Any

Positional arguments.

()
kwargs Any

Keyword arguments.

{}

Returns:

Type Description
Any

The tool's return value.

Raises:

Type Description
TypeError

If the tool exposes no sync runner.

Exception

Whatever the tool raises.

LangChainTool

Wrapper class for LangChain tools that preserves original function metadata.

toolregistry.integrations.langchain.integration.LangChainTool dataclass

LangChainTool(*, name: str, description: str, parameters: dict[str, Any], callable: Callable[..., Any], metadata: ToolMetadata | None = None, parameters_model: Any | None = None, namespace: str | None = None, method_name: str | None = None, is_async: bool | None = None)

Bases: Tool

Wrapper class for LangChain-style tools that preserves tool metadata.

from_langchain_tool classmethod

from_langchain_tool(tool: Any, namespace: str | None = None, sep: NameSeparator = '-') -> LangChainTool

Create a LangChainTool from a LangChain-style object.

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace str | None

Optional namespace to prefix the tool name.

None
sep NameSeparator

Separator between namespace and tool name. Pass the registry's name_sep so the stored key matches.

'-'

Returns:

Name Type Description
LangChainTool LangChainTool

A new instance.

Raises:

Type Description
TypeError

If the tool exposes no name or no usable schema.

LangChainIntegration

Handles integration with LangChain tools for registration.

toolregistry.integrations.langchain.integration.LangChainIntegration

LangChainIntegration(registry: ToolRegistry)

Handles registration of LangChain-style tools.

Attributes:

Name Type Description
registry ToolRegistry

Tool registry instance.

register_langchain_tools

register_langchain_tools(tool: Any, namespace: bool | str = False, include: str | list[str] | None = None, exclude: str | list[str] | None = None, allow_replace: bool = False) -> None

Register a LangChain-style tool (synchronous entry point).

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace Union[bool, str]

Whether to prefix tool names with a namespace. True uses the default "langchain tool" namespace; a string is used verbatim.

False
include str | list[str] | None

A name, glob pattern, or list of them to keep.

None
exclude str | list[str] | None

A name, glob pattern, or list of them to drop.

None
allow_replace bool

Acknowledge replacing an already-registered tool under the same name without warning.

False

Raises:

Type Description
ValueError

If both include and exclude are given.

TypeError

If the tool exposes no name or no usable schema.

register_langchain_tools_async async

register_langchain_tools_async(tool: Any, namespace: bool | str = False, include: str | list[str] | None = None, exclude: str | list[str] | None = None, allow_replace: bool = False) -> None

Register a LangChain-style tool from an async caller.

Construction is fast and CPU-bound, so it runs inline rather than borrowing the event loop's default executor. (Other integrations still use run_in_executor(None, ...); see #311 for that class of leak.)

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace Union[bool, str]

Whether to prefix tool names with a namespace. True uses the default "langchain tool" namespace; a string is used verbatim.

False
include str | list[str] | None

A name, glob pattern, or list of them to keep. Matched against the raw tool name, the post-namespace name, and the snake_case form of each.

None
exclude str | list[str] | None

A name, glob pattern, or list of them to drop. Mutually exclusive with include.

None
allow_replace bool

Acknowledge replacing an already-registered tool under the same name without warning.

False

Raises:

Type Description
ValueError

If both include and exclude are given.

TypeError

If the tool exposes no name or no usable schema.

Module Overview

LangChain Module

The main LangChain integration module.

toolregistry.integrations.langchain

LangChainIntegration

LangChainIntegration(registry: ToolRegistry)

Handles registration of LangChain-style tools.

Attributes:

Name Type Description
registry ToolRegistry

Tool registry instance.

register_langchain_tools

register_langchain_tools(tool: Any, namespace: bool | str = False, include: str | list[str] | None = None, exclude: str | list[str] | None = None, allow_replace: bool = False) -> None

Register a LangChain-style tool (synchronous entry point).

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace Union[bool, str]

Whether to prefix tool names with a namespace. True uses the default "langchain tool" namespace; a string is used verbatim.

False
include str | list[str] | None

A name, glob pattern, or list of them to keep.

None
exclude str | list[str] | None

A name, glob pattern, or list of them to drop.

None
allow_replace bool

Acknowledge replacing an already-registered tool under the same name without warning.

False

Raises:

Type Description
ValueError

If both include and exclude are given.

TypeError

If the tool exposes no name or no usable schema.

register_langchain_tools_async async

register_langchain_tools_async(tool: Any, namespace: bool | str = False, include: str | list[str] | None = None, exclude: str | list[str] | None = None, allow_replace: bool = False) -> None

Register a LangChain-style tool from an async caller.

Construction is fast and CPU-bound, so it runs inline rather than borrowing the event loop's default executor. (Other integrations still use run_in_executor(None, ...); see #311 for that class of leak.)

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace Union[bool, str]

Whether to prefix tool names with a namespace. True uses the default "langchain tool" namespace; a string is used verbatim.

False
include str | list[str] | None

A name, glob pattern, or list of them to keep. Matched against the raw tool name, the post-namespace name, and the snake_case form of each.

None
exclude str | list[str] | None

A name, glob pattern, or list of them to drop. Mutually exclusive with include.

None
allow_replace bool

Acknowledge replacing an already-registered tool under the same name without warning.

False

Raises:

Type Description
ValueError

If both include and exclude are given.

TypeError

If the tool exposes no name or no usable schema.

LangChainTool dataclass

LangChainTool(*, name: str, description: str, parameters: dict[str, Any], callable: Callable[..., Any], metadata: ToolMetadata | None = None, parameters_model: Any | None = None, namespace: str | None = None, method_name: str | None = None, is_async: bool | None = None)

Bases: Tool

Wrapper class for LangChain-style tools that preserves tool metadata.

from_langchain_tool classmethod

from_langchain_tool(tool: Any, namespace: str | None = None, sep: NameSeparator = '-') -> LangChainTool

Create a LangChainTool from a LangChain-style object.

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required
namespace str | None

Optional namespace to prefix the tool name.

None
sep NameSeparator

Separator between namespace and tool name. Pass the registry's name_sep so the stored key matches.

'-'

Returns:

Name Type Description
LangChainTool LangChainTool

A new instance.

Raises:

Type Description
TypeError

If the tool exposes no name or no usable schema.

LangChainToolWrapper

LangChainToolWrapper(tool: Any)

Bases: BaseToolWrapper

Wrapper providing sync and async entry points for a LangChain-style tool.

Attributes:

Name Type Description
tool Any

The wrapped object.

name str

Normalized tool name.

params list[str]

Parameter names.

Initialize the wrapper.

Parameters:

Name Type Description Default
tool Any

Object exposing name, description and a schema source.

required

call_async async

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

Run the tool asynchronously.

Uses the tool's async runner when available; otherwise the sync runner runs in a worker thread so the event loop is not blocked.

Parameters:

Name Type Description Default
args Any

Positional arguments.

()
kwargs Any

Keyword arguments.

{}

Returns:

Type Description
Any

The tool's return value.

Raises:

Type Description
Exception

Whatever the tool raises.

call_sync

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

Run the tool synchronously.

Parameters:

Name Type Description Default
args Any

Positional arguments.

()
kwargs Any

Keyword arguments.

{}

Returns:

Type Description
Any

The tool's return value.

Raises:

Type Description
TypeError

If the tool exposes no sync runner.

Exception

Whatever the tool raises.