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

使用Instructor对接Groq时出现tool_use_failed错误的技术求助

Render部署FastAPI应用时Groq+Instructor的tool_use_failed错误问题

错误信息

RuntimeError: Runtime error during ai call: 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': '{"Headline": "Vibe Coding Revolutionizes Software Industry with Creative Approach", "Readability": 4.0, "Star": "★★★★☆", "Summary": "Vibe Coding is an AI-assisted approach to programming...", "Topic": "Technology"}'
}
}

问题描述

在Render上部署Python FastAPI应用,使用instructor库将Groq API返回的LLM响应解析为Pydantic模型。本地运行完全正常,但部署后触发上述错误。模型生成的响应包含所有必填字段(Headline、Readability、Star、Summary、Topic),格式看似合法,却仍触发tool_use_failed错误,且该问题仅在生产环境(Render)出现。

相关代码片段

模型调用代码

from groq import Groq
from instructor import from_groq
import os

client = from_groq(Groq(api_key=os.getenv("GROQ_API")))

completion = client.chat.completions.create(
    model="meta-llama/llama-4-maverick-17b-128e-instruct",
    messages=[
        {
            "role": "assistant",
            "content": f"""IMPORTANT: You must call the function using the exact format required by the response model. Do NOT return JSON or plain text. You must call the function with properly typed parameters...

            (更多提示文本省略)
            """
        },
        {"role": "user", "content": Input.news},
    ],
    response_model=Summary,  # 自定义Pydantic模型
    temperature=0.3
)

Summary Pydantic模型定义

from pydantic import BaseModel, Field

class Summary(BaseModel):
    Headline : str
    Topic : str
    Summary : str
    Readability : float = Field(..., ge=1.0, le=5.0)
    Star : str

已尝试的解决方法

  • 重写提示词,强调“使用正确参数调用函数,而非返回JSON”
  • 将temperature降至0.3

内容的提问来源于stack exchange,提问作者Zakii

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:47:38