400 {"error":{"message":"INVALID_PARAMETER_VALUE: Missing 'id' in the tool_calls...
🚨 错误信息
API Error: 400 {"error":{"message":"INVALID_PARAMETER_VALUE: Missing 'id' in the tool_calls specification in the assistant message. (request id:
20260110224130756707147Amkfwu0g) (request id: 20260110224130680035461mHTgqC6p) (request id: 202601102241293018610313zojPHP3) (request id:
20260110224129180073334Fat6XKKz)","type":"<nil>"},"type":"error"}
🤖 AI解决方案
代码糕手 - 专业报错分析助理 错误翻译
原文:INVALID_PARAMETER_VALUE: Missing 'id' in the tool_calls specification in the assistant message.
译文:参数值无效:助手消息中的 tool_calls 规范缺少 'id' 字段。
##
这是在使用大型语言模型(LLM)API时出现的参数验证错误。当您使用带有函数调用/工具调用的 API 时(例如 OpenAI 的 Assistants API 或其他兼容接口),Assistant 消息中每个 tool_calls 数组内的工具调用对象都必须包含一个唯一的 id 字段。系统检测到您提交的 Assistant 消息中的某个或多个工具调用缺少这个必需的标识符。
核心原因:API 请求体中的数据结构不符合规范,缺少了 tool_calls[i].id 字段。
##您需要在构造 Assistant 消息时,确保每个 tool_calls 对象都包含一个唯一的 id 字符串。
以下是修复后的代码示例,假设您在使用类似于 OpenAI API 的结构:
json
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_unique_id_001", // ⬅️ 必须添加的唯一ID
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"Beijing\"}"
}
},
{
"id": "call_unique_id_002", // ⬅️ 另一个工具调用也需要ID
"type": "function",
"function": {
"name": "calculate",
"arguments": "{\"expression\": \"2+2\"}"
}
}
]
}
具体步骤:
assistant 角色消息的地方。tool_calls 对象创建一个唯一的字符串标识符(例如:使用时间戳、UUID 或递增计数器)。tool_calls 数组内的每个对象都包含了 id 字段。️ 预防措施
openai Python/Node.js SDK),它们通常会自动生成并填充 tool_calls.id 字段,避免手动构造时的遗漏。assistant 消息中的 tool_calls 是否都包含必需的字段(id, type, function)。python
def validate_tool_calls(tool_calls):
for tc in tool_calls:
if 'id' not in tc:
raise ValueError(f"Missing 'id' in tool call: {tc}")
# ... 其他字段检查
工具推荐:使用 Postman 或类似的 API 调试工具,可以在发送请求前清晰查看和编辑 JSON 结构,确保格式正确。