Converts a given Langchain tool into a general Tool
format.
This method verifies that the provided tool is a valid LangchainTool
, processes the tool's input and description, and returns a standardized Tool
object.
PARAMETER | DESCRIPTION |
tool | The tool to convert, expected to be an instance of LangchainTool . TYPE: Any |
**kwargs | Additional arguments, which are not supported by this method. TYPE: Any DEFAULT: {} |
RETURNS | DESCRIPTION |
Tool | A standardized Tool object converted from the Langchain tool. TYPE: Tool |
RAISES | DESCRIPTION |
ValueError | If the provided tool is not an instance of LangchainTool , or if any additional arguments are passed. |
Source code in autogen/interop/langchain/langchain_tool.py
| @classmethod
@require_optional_import("langchain_core", "interop-langchain")
def convert_tool(cls, tool: Any, **kwargs: Any) -> Tool:
"""Converts a given Langchain tool into a general `Tool` format.
This method verifies that the provided tool is a valid `LangchainTool`,
processes the tool's input and description, and returns a standardized
`Tool` object.
Args:
tool (Any): The tool to convert, expected to be an instance of `LangchainTool`.
**kwargs (Any): Additional arguments, which are not supported by this method.
Returns:
Tool: A standardized `Tool` object converted from the Langchain tool.
Raises:
ValueError: If the provided tool is not an instance of `LangchainTool`, or if
any additional arguments are passed.
"""
if not isinstance(tool, LangchainTool):
raise ValueError(f"Expected an instance of `langchain_core.tools.BaseTool`, got {type(tool)}")
if kwargs:
raise ValueError(f"The LangchainInteroperability does not support any additional arguments, got {kwargs}")
# needed for type checking
langchain_tool: LangchainTool = tool # type: ignore[no-any-unimported]
model_type = langchain_tool.get_input_schema()
def func(tool_input: model_type) -> Any: # type: ignore[valid-type]
return langchain_tool.run(tool_input.model_dump()) # type: ignore[attr-defined]
return Tool(
name=langchain_tool.name,
description=langchain_tool.description,
func_or_tool=func,
)
|