as_tool() — langchain Function Reference
Architecture documentation for the as_tool() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 0001ba2c_b7d4_c9bf_b96a_109df35261e6["as_tool()"] 4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"] 0001ba2c_b7d4_c9bf_b96a_109df35261e6 -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6 style 0001ba2c_b7d4_c9bf_b96a_109df35261e6 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/runnables/base.py lines 2467–2583
def as_tool(
self,
args_schema: type[BaseModel] | None = None,
*,
name: str | None = None,
description: str | None = None,
arg_types: dict[str, type] | None = None,
) -> BaseTool:
"""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.
Args:
args_schema: The schema for the tool.
name: The name of the tool.
description: The description of the tool.
arg_types: A dictionary of argument names to types.
Returns:
A `BaseTool` instance.
!!! example "`TypedDict` input"
```python
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]})
```
!!! example "`dict` input, specifying schema via `args_schema`"
```python
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]})
```
!!! example "`dict` input, specifying schema via `arg_types`"
```python
from typing import Any
from langchain_core.runnables import RunnableLambda
def f(x: dict[str, Any]) -> str:
return str(x["a"] * max(x["b"]))
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does as_tool() do?
as_tool() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is as_tool() defined?
as_tool() is defined in libs/core/langchain_core/runnables/base.py at line 2467.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free