You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groq+LangChain调用自定义search_pdf工具触发tool_use_failed错误

问题:LangGraph + Groq llama-3.3-70b-versatile 调用自定义工具时格式错误

环境与配置

  • 基于LangChain(最新版)、LangGraph和Groq开发Streamlit应用
  • 使用模型:llama-3.3-70b-versatile
  • 通过LangGraph的create_agent() API构建智能体,配置两个工具:
    • search_pdf:基于Chroma检索器的自定义工具(工具装饰器命名为searchdatasheet)
    • web_search:DuckDuckGo官方工具

核心问题

智能体可根据查询正确选择工具,但调用searchdatasheet时Groq返回400格式错误:

groq.BadRequestError: Error code: 400 - {'error': {'message': "Failed to call a function. Please adjust your prompt. See 'failed_generation' for more details.", 'type': 'invalid_request_error', 'code': 'tool_use_failed', 'failed_generation': '<function=searchdatasheet {"search_query": "I2C slave address"} </function>'}}

模型输出的工具调用格式不符合结构化要求,示例输出为:

<function=search_pdf{"query": "I2C slave address"}</function>

特殊现象

  • web_search工具可正常调用,无格式问题
  • 仅search_pdf工具触发该格式错误
  • 切换至llama-3.1-8b-instant模型可避免错误,但智能体优先使用web_search,极少调用search_pdf

相关代码片段

自定义工具定义

#Input Schema
class SearchInput(BaseModel):
    search_query: str = Field(description="The exact technical term or specification to look up.")

@tool("searchdatasheet", args_schema=SearchInput)
def searchdatasheet(search_query: str) -> str:
    """Use this tool FIRST for ANY technical question about the currently loaded datasheet. This includes SPI modes, electrical characteristics, register maps, pin configuration, timing diagrams, operating conditions, and any specification related queries. Only use web_search if the answer is NOT found in the datasheet."""
    if "retriever" in st.session_state and st.session_state.retriever is not None:
        try:

LLM初始化代码

llm = ChatGroq(
    model="llama-3.3-70b-versatile",
    temperature=0
)

智能体创建代码

agent = create_agent(
    llm,
    agent_tools,
    system_prompt=system_prompt
)

内容的提问来源于stack exchange,提问作者Ravikiran Arasur T S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 12:12:37