code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must b...
错误信息
{"msg": "API返回错误:Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. (insufficient tool messages following tool_calls message)", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}"}
AI 解决方案
1. 🌐 错误翻译
原文:An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. (insufficient tool messages following tool_calls message)
译文:带有'tool_calls'的助手消息后面必须跟着响应每个'tool_call_id'的工具消息。(工具调用消息后工具消息不足)
2. 🎯 原因分析
该错误是工具调用流程的完整性问题:当助手返回包含
tool_calls(工具调用)的消息后,系统要求必须针对每个调用的工具返回对应的工具执行结果消息,但当前缺少部分或全部工具的响应消息,导致工具调用与工具响应不匹配。3. 💡 解决方案
需确保助手发出工具调用后,严格按顺序补充每个工具的响应消息,完整覆盖所有
tool_call_id。以下是正确的工具调用-响应流程示例(以Python为例,适配常见大模型工具调用框架):# 步骤1:助手发起工具调用(此时未返回工具结果)
assistant_response = {
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "tool_1", # 工具调用唯一标识
"type": "get_user_info",
"arguments": {"user_id": "123"}
}
]
}
# 步骤2:模拟工具执行后返回结果(必须紧跟工具调用,且id与工具调用一致)
tool_response = {
"role": "tool",
"tool_call_id": "tool_1", # 必须与工具调用的id对应
"content": {"name": "张三", "age": 30}
}
# 完整对话流拼接(保证工具调用-响应闭环)
full_dialog = [
{"role": "user", "content": "查询用户123的信息"},
assistant_response,
tool_response
]
4. 🛡️ 预防措施
tool_call_id补充了对应的工具响应,建议使用框架自带的工具调用校验工具(如LangChain的ToolCallingManager)验证流程完整性。tool_call_id的发起和响应状态,便于快速定位缺失的工具响应节点。