一部のユースケースではモデルに、構造化入力を使用して API、データベース、ファイルシステムのような外部システムと直接連携することを要求します。
ツールは、エージェントがアクションを実行するために呼び出すコンポーネントです。
LangChain 1.0 alpha : コアコンポーネント – ツール
作成 : クラスキャット・セールスインフォメーション
作成日時 : 09/24/2025
バージョン : 1.0.0a6
* 本記事は docs.langchain.com の以下のページを独自に翻訳した上で、補足説明を加えてまとめ直しています。スニペットはできる限り日本語を使用しています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
LangChain 1.0 alpha : コアコンポーネント – ツール
多くの AI アプリケーションは自然言語を介してユーザとやり取りします。けれども、一部のユースケースではモデルに、構造化入力を使用して API、データベース、ファイルシステムのような外部システムと直接連携することを要求します。
ツールは、エージェント がアクションを実行するために呼び出すコンポーネントです。それらは、うまく定義された入力と出力を通して世界とやり取りすることでモデル機能を拡張させます。ツールは呼び出し可能関数とその入力スキーマをカプセル化します。これらは互換性のある チャットモデル に渡され、モデルがツールを呼び出すかどうか、またどの引数で呼び出すかを決定することを可能にします。これらのシナリオでは、ツール呼び出しは、モデルが指定された入力スキーマに準拠したリクエストを生成することを可能にします。
ツールの作成
基本的なツール定義
ツールを作成する最も簡単な方法は @tool デコレータを使用することです。デフォルトでは、関数の docstring がツールの説明になり、モデルがいつそれを使用するか理解するのに役立ちます。
from langchain_core.tools import tool
@tool
def search_database(query: str, limit: int = 10) -> str:
"""Search the customer database for records matching the query.
Args:
query: Search terms to look for
limit: Maximum number of results to return
"""
return f"Found {limit} results for '{query}'"
型ヒントはツールの入力スキーマを定義するために 必須 です。docstring は、モデルがツールの目的を理解するのに役立つように、十分に情報が多く、かつ簡潔である必要があります。
ツール・プロパティのカスタマイズ
ツール名のカスタマイズ
デフォルトでは、ツール名は関数名に由来します。より説明的な名前が必要な場合にはオーバーライドできます :
@tool("web_search") # Custom name
def search(query: str) -> str:
"""Search the web for information."""
return f"Results for: {query}"
print(search.name) # web_search
ツール説明のカスタマイズ
より明確なモデルガイダンスのために自動生成されたツール説明をオーバーライドできます :
@tool("calculator", description="Performs arithmetic calculations. Use this for any math problems.")
def calc(expression: str) -> str:
"""Evaluate mathematical expressions."""
return str(eval(expression))
高度なスキーマ定義
Pydantic モデルや JSON スキーマを使用して複雑な入力を定義できます :
Pydantic model
from pydantic import BaseModel, Field
from typing import Literal
class WeatherInput(BaseModel):
"""Input for weather queries."""
location: str = Field(description="City name or coordinates")
units: Literal["celsius", "fahrenheit"] = Field(
default="celsius",
description="Temperature unit preference"
)
include_forecast: bool = Field(
default=False,
description="Include 5-day forecast"
)
@tool(args_schema=WeatherInput)
def get_weather(location: str, units: str = "celsius", include_forecast: bool = False) -> str:
"""Get current weather and optional forecast."""
temp = 22 if units == "celsius" else 72
result = f"Current weather in {location}: {temp} degrees {units[0].upper()}"
if include_forecast:
result += "\nNext 5 days: Sunny"
return result
JSON Schema
weather_schema = {
"type": "object",
"properties": {
"location": {"type": "string"},
"units": {"type": "string"},
"include_forecast": {"type": "boolean"}
},
"required": ["location", "units", "include_forecast"]
}
@tool(args_schema=weather_schema)
def get_weather(location: str, units: str = "celsius", include_forecast: bool = False) -> str:
"""Get current weather and optional forecast."""
temp = 22 if units == "celsius" else 72
result = f"Current weather in {location}: {temp} degrees {units[0].upper()}"
if include_forecast:
result += "\nNext 5 days: Sunny"
return result
エージェントでツールを使用する
エージェントは、推論ループ、状態管理、多段階実行を追加することで、単純なツールバインディングを超えた機能を提供できます。
Note : To see examples of how to use tools with agents, see Agents.
高度なツールパターン
ToolNode
ToolNode は事前構築済みの LangGraph コンポーネントで、エージェントのワークフロー内でツール呼び出しを処理します。create_agent() とシームレスに連携し、高度なツール実行制御、組み込みの並列処理、エラー処理を提供します。
構成設定オプション
ToolNode は以下のパラメータを受け取ります :
from langchain.agents import ToolNode
tool_node = ToolNode(
tools=[...], # List of tools or callables
handle_tool_errors=True, # Error handling configuration
...
)
tools – required
このノードが実行できるツールのリスト。以下を含めることができます :
- LangChain @tool でデコレートされた関数
- 適切な型ヒントと docstring を持つ呼び出し可能オブジェクト (e.g. 関数)
handle_tool_errors
ツール実行の失敗の処理方法を制御します。Can be :
- bool
- str
- Callable[…, str]
- type[Exception]
- tuple[type[Exception], …]
Default: internal _default_handle_tool_errors
エラー処理ストラテジー
ToolNode は、handle_tool_errors プロパティを通してツール実行の組み込みエラー処理を提供します。
さまざまなエラー処理ストラテジーの使用方法の例 :
# Retry on all exception types with the default error message template string
tool_node = ToolNode(tools=[my_tool], handle_tool_errors=True)
# Retry on all exception types with a custom message string
tool_node = ToolNode(
tools=[my_tool],
handle_tool_errors="I encountered an issue. Please try rephrasing your request."
)
# Retry on ValueError with a custom message, otherwise raise
def handle_errors(e: ValueError) -> str:
return "Invalid input provided"
tool_node = ToolNode([my_tool], handle_tool_errors=handle_errors)
# Retry on ValueError and KeyError with the default error message template string, otherwise raise
tool_node = ToolNode(
tools=[my_tool],
handle_tool_errors=(ValueError, KeyError)
)
create_agent() で使用する
Note : We recommend that you familiarize yourself with create_agent() before covering this section. Read more about agents.
構成設定済みの ToolNode を create_agent() に直接渡します :
from langchain_openai import ChatOpenAI
from langchain.agents import ToolNode, create_agent
import random
@tool
def fetch_user_data(user_id: str) -> str:
"""Fetch user data from database."""
if random.random() > 0.7:
raise ConnectionError("Database connection timeout")
return f"User {user_id}: John Doe, john@example.com, Active"
@tool
def process_transaction(amount: float, user_id: str) -> str:
"""Process a financial transaction."""
if amount > 10000:
raise ValueError(f"Amount {amount} exceeds maximum limit of 10000")
return f"Processed ${amount} for user {user_id}"
def handle_errors(e: Exception) -> str:
if isinstance(e, ConnectionError):
return "The database is currently overloaded, but it is safe to retry. Please try again with the same parameters."
elif isinstance(e, ValueError):
return f"Error: {e}. Try to process the transaction in smaller amounts."
return f"Error: {e}. Please try again."
tool_node = ToolNode(
tools=[fetch_user_data, process_transaction],
handle_tool_errors=handle_errors
)
agent = create_agent(
model=ChatOpenAI(model="gpt-4o"),
tools=tool_node,
prompt="You are a financial assistant."
)
agent.invoke({
"messages": [{"role": "user", "content": "Process a payment of 15000 dollars for user123. Generate a receipt email and address it to the user."}]
})
以上