LangChain Tools API
@tool Decorator
| Parameter | Type | Default Value | Description |
|---|---|---|---|
| args_schema | BaseModel or None | None | Parameter validation model. If not provided, it is automatically generated from the function signature. |
| return_direct | bool | False | Whether to return directly (skip model rethinking) |
| name | str or None | Function name | Tool name |
| description | str or None | Function docstring | Tool description |
BaseTool Key Attributes and Methods
| Attribute/Method | Description |
|---|---|
| name | Tool name (string) |
| description | Tool description (string) |
| args_schema | Parameter Pydantic model |
| return_direct | Whether to return directly (bool) |
| invoke(input) | Call the tool; input is the parameter dictionary |
| ainvoke(input) | Asynchronously call the tool |
Dependency Injection Markers
| Marker | Purpose | Usage |
|---|---|---|
| InjectedState | Inject Agent state | Annotated[dict, InjectedState] |
| InjectedStore | Inject cross-session store | Annotated[BaseStore, InjectedStore()] |
| InjectedToolCallId | Inject tool call ID | Annotated[str, InjectedToolCallId] |
| InjectedToolArg | Generic injection marker | Annotated[T, InjectedToolArg] |
Common Usage Examples
Examples
from langchain.tools import tool, InjectedState, InjectedStore, ToolException
from typing import Annotated
from langgraph.store.base import BaseStore
# Basic tool
@tool
def my_tool(param: str) -> str:
"""Tool description"""
return f"Result: {param}"
# With parameter validation
from pydantic import BaseModel, Field
class MyInput(BaseModel):
param: str = Field(description="Parameter description", min_length=1)
@tool(args_schema=MyInput)
def validated_tool(param: str) -> str:
return param
# Direct return
@tool(return_direct=True)
def query_tool(query: str) -> str:
return f"Result: {query}"
# Inject state
@tool
def stateful_tool(
param: str,
state: Annotated[dict, InjectedState],
) -> str:
return f"Message count: {len(state.get('messages', []))}"
# Inject Store
@tool
def store_tool(
key: str,
store: Annotated[BaseStore, InjectedStore()],
) -> str:
item = store.get(("ns",), key)
return str(item.value if item else "None")
# Exception handling
@tool
def safe_tool(param: int) -> str:
if param < 0:
raise ToolException(f"Parameter must be a positive number: {param}")
return f"OK: {param}"
from typing import Annotated
from langgraph.store.base import BaseStore
# Basic tool
@tool
def my_tool(param: str) -> str:
"""Tool description"""
return f"Result: {param}"
# With parameter validation
from pydantic import BaseModel, Field
class MyInput(BaseModel):
param: str = Field(description="Parameter description", min_length=1)
@tool(args_schema=MyInput)
def validated_tool(param: str) -> str:
return param
# Direct return
@tool(return_direct=True)
def query_tool(query: str) -> str:
return f"Result: {query}"
# Inject state
@tool
def stateful_tool(
param: str,
state: Annotated[dict, InjectedState],
) -> str:
return f"Message count: {len(state.get('messages', []))}"
# Inject Store
@tool
def store_tool(
key: str,
store: Annotated[BaseStore, InjectedStore()],
) -> str:
item = store.get(("ns",), key)
return str(item.value if item else "None")
# Exception handling
@tool
def safe_tool(param: int) -> str:
if param < 0:
raise ToolException(f"Parameter must be a positive number: {param}")
return f"OK: {param}"