":{"message":"Invalid param: Message format error, the role 'tool' of index [*] ...
🚨 错误信息
HttpException: HTTP 400: {"error":{"message":"Invalid param: Message format error, the role 'tool' of index [*] must be a response to a preceeding assistant message with 'tool_calls'.","type":"rix_api_error","param":"8b7649d5-4187-40f3-9794-4d196200b092","code":"param_error"}}
🤖 AI解决方案
代码糕手 - 专业报错分析助理
错分析报告
错误翻译
原文:Invalid param: Message format error, the role 'tool' of index [*] must be a response to a preceeding assistant message with 'tool_calls'
译文:参数无效:消息格式错误,索引[*]处的角色'tool'必须是对前一个包含'tool_calls'的助手消息的响应
2
这是一个API调用格式错误,发生在使用工具调用功能时。根本原因是:
role: 'tool'的消息没有正确跟随在role: 'assistant'且包含tool_calls的消息之后3
修复消息格式,确保正确的消息顺序:
javascript
// 正确的消息格式示例
const messages = [
{
role: 'user',
content: '查询北京的天气'
},
{
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_abc123',
type: 'function',
function: {
name: 'get_weather',
arguments: JSON.stringify({ city: '北京' })
}
}
]
},
{
role: 'tool',
tool_call_id: 'call_abc123', // 必须匹配前一个assistant消息中的tool_call_id
name: 'get_weather',
content: JSON.stringify({ temperature: 25, condition: '晴朗' })
}
];// 错误示例 - 不要这样做
const wrongMessages = [
{
role: 'user',
content: '查询天气'
},
{
role: 'tool', // ❌ 错误:tool消息没有对应的assistant tool_calls
content: '{"temperature": 25}'
}
];
️ 预防措施
---
请检查您的对话历史数组,确保每个tool角色消息都有对应的前一个assistant消息包含tool_calls,并且tool_call_id匹配正确。