
openai
#10034
invalid_parameter_error
Invalid parameter: messages with role "tool" must follow a preceding message with "tool_calls".
This error has been identified and solved.
Reason
The 400 status error in the OpenAI API with the message "Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls'" indicates that the API request is not properly structured according to the API's requirements.
Specifically, the error suggests that the API expects any message with the role "tool" to be a response to a previous message that contains a "tool_calls" directive. If this preceding message is missing or incorrectly formatted, the API cannot process the request, leading to a "Bad Request" error.
This issue is likely due to the API's strict requirements for the sequence and content of messages in the request, particularly when involving tool calls and responses.
Solution
To fix the "Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls'" error in the OpenAI API, you need to ensure that your request adheres to the API's messaging structure. Here are the key steps to correct this:
Ensure each
ToolMessage
is preceded by anAIMessage
that contains the correspondingtool_calls
.Match each
ToolMessage
with the correcttool_call_id
from the precedingAIMessage
.Avoid passing
ToolMessages
that are not responses to precedingtool_calls
.
Here are some concise actions to take:
Verify the sequence of messages in your request.
Check that each
ToolMessage
has a validtool_call_id
.Ensure no
ToolMessage
is sent without a precedingAIMessage
withtool_calls
.