Agent SDK: 並列ツール呼び出しで不正なツール名が含まれる場合のハングを修正
原文(日本語に翻訳)
モデルが並列ツール呼び出しバッチで不正なツール名を出力した場合に発生していた、Agent SDKのハングを修正しました。
原文(英語)
Fixed Agent SDK hang when the model emits a malformed tool name in a parallel tool call batch
概要
Claude Agent SDKで複数のツールを並列に呼び出す際、モデルが不正な形式のツール名(存在しないツール名や無効な文字を含む名前など)を出力した場合に、SDKがハング(フリーズ)する問題が修正されました。今後は不正なツール名を適切にエラーとして処理し、ハングすることなく処理が継続されます。
基本的な使い方
python
# Agent SDKで並列ツール呼び出しを使用するコード例
import anthropic
client = anthropic.Anthropic()
# v2.1.126以前: 不正なツール名でハングすることがあった
# v2.1.126以降: エラーとして適切に処理される
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "天気情報を取得",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
},
{
"name": "get_time",
"description": "現在時刻を取得",
"input_schema": {
"type": "object",
"properties": {}
}
}
],
messages=[{"role": "user", "content": "東京の天気と現在時刻を教えて"}]
)
# → 並列ツール呼び出しが安定して動作する実践例
並列ツール呼び出しのエラーハンドリング
python
import anthropic
from anthropic import APIError
client = anthropic.Anthropic()
def safe_tool_call_with_parallel(prompt: str, tools: list) -> str:
try:
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=2048,
tools=tools,
messages=[{"role": "user", "content": prompt}]
)
return response.content
except APIError as e:
# v2.1.126以降: 不正なツール名はハングではなくエラーとして返される
print(f"API Error: {e}")
raise
# 複数ツールを並列実行する例
tools = [
{"name": "search_code", "description": "コードを検索", "input_schema": {...}},
{"name": "read_file", "description": "ファイルを読む", "input_schema": {...}},
{"name": "run_tests", "description": "テストを実行", "input_schema": {...}}
]
result = safe_tool_call_with_parallel(
"コードを検索してファイルを読んでテストを実行して",
tools
)マルチエージェント並列処理での安定性
python
import asyncio
import anthropic
client = anthropic.AsyncAnthropic()
async def run_agent_with_tools(task: str, tools: list):
"""並列ツール呼び出しを使うエージェントを安全に実行"""
response = await client.messages.create(
model="claude-opus-4-7",
max_tokens=4096,
tools=tools,
messages=[{"role": "user", "content": task}]
)
return response
async def main():
# 複数エージェントを並列実行
tasks = [
run_agent_with_tools("コードレビューして", code_review_tools),
run_agent_with_tools("セキュリティ分析して", security_tools),
run_agent_with_tools("パフォーマンス分析して", perf_tools),
]
# v2.1.126以降: 不正なツール名があってもハングしない
results = await asyncio.gather(*tasks, return_exceptions=True)
return results
asyncio.run(main())注意点
- この修正は並列ツール呼び出し(parallel tool calls)に特有の問題を解決します
- 不正なツール名が原因のエラーは適切なエラーメッセージで返されます
- ツール名は英数字とアンダースコアのみ使用することが推奨されます
- Agent SDKを最新バージョン(v2.1.126以降)にアップデートしてください