Home / Function/ convert_runnable_to_tool() — langchain Function Reference

convert_runnable_to_tool() — langchain Function Reference

Architecture documentation for the convert_runnable_to_tool() function in convert.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  969898db_d03b_b08b_586c_a15e26c6f807["convert_runnable_to_tool()"]
  1f697f5b_f8d2_0b74_7ca1_23dcfcfa02e2["convert.py"]
  969898db_d03b_b08b_586c_a15e26c6f807 -->|defined in| 1f697f5b_f8d2_0b74_7ca1_23dcfcfa02e2
  180c4272_eb5c_a626_92ef_8ef991f0afa9["_get_description_from_runnable()"]
  969898db_d03b_b08b_586c_a15e26c6f807 -->|calls| 180c4272_eb5c_a626_92ef_8ef991f0afa9
  f3770c8f_f079_5d31_25c1_4df27d4c87fa["_get_schema_from_runnable_and_arg_types()"]
  969898db_d03b_b08b_586c_a15e26c6f807 -->|calls| f3770c8f_f079_5d31_25c1_4df27d4c87fa
  style 969898db_d03b_b08b_586c_a15e26c6f807 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/tools/convert.py lines 419–476

def convert_runnable_to_tool(
    runnable: Runnable,
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None,
) -> BaseTool:
    """Convert a `Runnable` into a `BaseTool`.

    Args:
        runnable: The `Runnable` to convert.
        args_schema: The schema for the tool's input arguments.
        name: The name of the tool.
        description: The description of the tool.
        arg_types: The types of the arguments.

    Returns:
        The tool.
    """
    if args_schema:
        runnable = runnable.with_types(input_type=args_schema)
    description = description or _get_description_from_runnable(runnable)
    name = name or runnable.get_name()

    schema = runnable.input_schema.model_json_schema()
    if schema.get("type") == "string":
        return Tool(
            name=name,
            func=runnable.invoke,
            coroutine=runnable.ainvoke,
            description=description,
        )

    async def ainvoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
        return await runnable.ainvoke(kwargs, config={"callbacks": callbacks})

    def invoke_wrapper(callbacks: Callbacks | None = None, **kwargs: Any) -> Any:
        return runnable.invoke(kwargs, config={"callbacks": callbacks})

    if (
        arg_types is None
        and schema.get("type") == "object"
        and schema.get("properties")
    ):
        args_schema = runnable.input_schema
    else:
        args_schema = _get_schema_from_runnable_and_arg_types(
            runnable, name, arg_types=arg_types
        )

    return StructuredTool.from_function(
        name=name,
        func=invoke_wrapper,
        coroutine=ainvoke_wrapper,
        description=description,
        args_schema=args_schema,
    )

Subdomains

Frequently Asked Questions

What does convert_runnable_to_tool() do?
convert_runnable_to_tool() is a function in the langchain codebase, defined in libs/core/langchain_core/tools/convert.py.
Where is convert_runnable_to_tool() defined?
convert_runnable_to_tool() is defined in libs/core/langchain_core/tools/convert.py at line 419.
What does convert_runnable_to_tool() call?
convert_runnable_to_tool() calls 2 function(s): _get_description_from_runnable, _get_schema_from_runnable_and_arg_types.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free