# langchain_core.exceptions.ToolException: 工具 'search_tool' 调用缺少必需参数。期望：['query']，实际：[]。

- **ID:** `llm/langchain-tool-call-missing-arguments`
- **领域:** llm
- **类别:** runtime_error
- **错误码:** `LANG-ERR-0078`
- **验证级别:** ai_generated
- **修复率:** 76%

## 根因

LLM 生成一个参数字典为空或缺少必需参数的工具调用，通常是由于模型误解了工具架构，或在流式传输期间截断了函数调用 JSON。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| langchain==0.3.0 | active | — | — |
| langchain-core==0.3.5 | active | — | — |
| langchain-openai==0.2.0 | active | — | — |

## 解决方案

1. ```
   实现一个验证包装器，捕获 ToolException 并使用更正消息重新提示 LLM：`try: result = tool.run(tool_call) except ToolException: corrected_call = llm.invoke("之前的工具调用无效。请为 'search_tool' 提供有效的参数，包含 'query' 字符串。")`。这允许模型自我纠正。
   ```
2. ```
   通过使用原始类型（str、int）而不是复杂对象或枚举来简化工具架构。示例：将 `query: Optional[str]` 改为 `query: str = Field(description="...")` 使其成为必需且明确的参数。
   ```
3. ```
   使用 LangChain 的 `StructuredTool` 并设置 `handle_tool_error=True` 以自动重试并回退消息：`tool = StructuredTool.from_function(func=search, handle_tool_error=True)`。
   ```

## 无效尝试

- **** — While helpful, verbose descriptions can confuse the LLM or lead to token limit issues. The root cause is often the model's inability to consistently produce valid JSON for complex schemas. (65% 失败率)
- **** — Deterministic settings reduce variability but don't fix the underlying schema parsing issue. The model may still generate malformed calls if the prompt or tool definitions are ambiguous. (70% 失败率)
- **** — Retrying the same request with the same configuration will likely produce the same error because the model's behavior is consistent for the same input. (90% 失败率)
