LANG-ERR-0078 llm runtime_error ai_generated partial

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

langchain_core.exceptions.ToolException: Tool 'search_tool' called with missing required arguments. Expected: ['query'], got: [].

ID: llm/langchain-tool-call-missing-arguments

其他格式: JSON · Markdown 中文 · English
76%修复率
83%置信度
1证据数
2024-11-05首次发现

版本兼容性

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

根因分析

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

English

The LLM generates a tool call with an empty arguments dict or missing required parameters, often due to the model misinterpreting the tool schema or truncating the function call JSON during streaming.

generic

官方文档

https://python.langchain.com/docs/how_to/custom_tools/#tool-validation

解决方案

  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)`。

无效尝试

常见但无效的做法:

  1. 65% 失败

    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.

  2. 70% 失败

    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.

  3. 90% 失败

    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.