#BadRequestError: 400 An assistant message with 'tool_calls' must be followed by tool messages

1 messages · Page 1 of 1 (latest)

glass echo
#

Does the tool message need to follow immediately after? Or can I insert messages inbetween the assistant tool call and the tool response?

late salmonBOT
#

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:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: