#BadRequestError: 400 An assistant message with 'tool_calls' must be followed by tool messages
1 messages · Page 1 of 1 (latest)
Hi @glass echo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the information from the OpenAI community forum, it appears that a tool message must follow immediately after an assistant message with 'tool_calls'. The error message you're seeing suggests that the system is expecting a tool message to respond to each 'tool_call_id' immediately after the assistant message.
Here is an example of how to construct a response to a tool call:
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # extend conversation with function response
And here's how to construct the assistant's tool call from scratch in Python:
function_response = {
"name": "ask_user_clarifying_question",
"arguments": json.dumps({
"question": clarification.clarification_question.question,
"choices": [choice.name for choice in clarification.clarification_question.choices]
})
}
assistant_message = {
"role": "assistant",
"tool_calls": [{"id": clarification.clarification_question.tool_call_id, "function": function_response, "type": "function"}]
}
messages.append(assistant_message)
Please note that the 'tool_call_id' in the tool message should match the 'id' in the assistant's tool call.
Sources:
- OpenAI Community Forum: Formatting Assistant Messages After Tool Function Calls in GPT Conversations (https://community.openai.com/t/535360)
- OpenAI Community Forum: OpenAI 400 Error about JSON and and String (https://community.openai.com/t/597830)