LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corerunnablesbaseRunnableas_tool
    Method●Since v0.2

    as_tool

    Copy
    as_tool(
      self,
      args_schema: type[BaseModel] | None = None,
      *,
      name: str
    View source on GitHub
    |
    None
    =
    None
    ,
    description
    :
    str
    |
    None
    =
    None
    ,
    arg_types
    :
    dict
    [
    str
    ,
    type
    ]
    |
    None
    =
    None
    )
    ->
    BaseTool

    Parameters

    NameTypeDescription
    args_schematype[BaseModel] | None
    Default:None

    The schema for the tool.

    namestr | None
    Default:None

    The name of the tool.

    descriptionstr | None
    Default:None
    arg_typesdict[str, type] | None
    Default:None

    Create a BaseTool from a Runnable.

    as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema.

    Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema.

    You can also pass arg_types to just specify the required arguments and their types.

    TypedDict input
    from typing_extensions import TypedDict
    from langchain_core.runnables import RunnableLambda
    
    class Args(TypedDict):
        a: int
        b: list[int]
    
    def f(x: Args) -> str:
        return str(x["a"] * max(x["b"]))
    
    runnable = RunnableLambda(f)
    as_tool = runnable.as_tool()
    as_tool.invoke({"a": 3, "b": [1, 2]})
    dict input, specifying schema via args_schema
    from typing import Any
    from pydantic import BaseModel, Field
    from langchain_core.runnables import RunnableLambda
    
    def f(x: dict[str, Any]) -> str:
        return str(x["a"] * max(x["b"]))
    
    class FSchema(BaseModel):
        """Apply a function to an integer and list of integers."""
    
        a: int = Field(..., description="Integer")
        b: list[int] = Field(..., description="List of ints")
    
    runnable = RunnableLambda(f)
    as_tool = runnable.as_tool(FSchema)
    as_tool.invoke({"a": 3, "b": [1, 2]})
    dict input, specifying schema via arg_types
    from typing import Any
    from langchain_core.runnables import RunnableLambda
    
    def f(x: dict[str, Any]) -> str:
        return str(x["a"] * max(x["b"]))
    
    runnable = RunnableLambda(f)
    as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
    as_tool.invoke({"a": 3, "b": [1, 2]})
    str input
    from langchain_core.runnables import RunnableLambda
    
    def f(x: str) -> str:
        return x + "a"
    
    def g(x: str) -> str:
        return x + "z"
    
    runnable = RunnableLambda(f) | g
    as_tool = runnable.as_tool()
    as_tool.invoke("b")

    The description of the tool.

    A dictionary of argument names to types.