Get a bad request error in M3_UGL_1 ungraded lab. seems to be an issue with the AISUITE tools_calls

can someone help resolve the below error:

BadRequestError: Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_cxdspVAEEwly7nZdvZ8cSP7F", 'type': 'invalid_request_error', 'param': 'messages.[3].role', 'code': None}}

During handling of the above exception, another exception occurred:

LLMError                                  Traceback (most recent call last)
Cell In[15], line 19
     14 messages.append({
     15     "role": "tool", "tool_call_id": tool_call.id, "content": str(tool_result)
     16 })
     18 # Send the list of messages with the newly appended results back to the LLM
---> 19 response2 = client.chat.completions.create(
     20     model="openai:gpt-4o",
     21     messages=messages,
     22     tools=tools,
     23 )
     25 print(response2.choices[0].message.content)

Hi vkbalasub,

The error message is produced by the openai api, as explained here:

If I outcomment the following snippet of code:

messages.append({
    "role": "tool", "tool_call_id": tool_call.id, "content": str(tool_result)
})

I get the following LLMError:

LLMError: An error occurred: Error code: 400 - {‘error’: {‘message’: “An assistant message with ‘tool_calls’ must be followed by tool messages responding to each ‘tool_call_id’. The following tool_call_ids did not have response messages: call_Ym3BSPjUp4QiznqkVvv3HazC”, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages’, ‘code’: None}}

If I then remove the outcommenting, I need to run the previous cells to reset the messages variable. Then the code runs without error.

You can see what is going on in your case by including print(messages) statements, as in

# Run the tool locally
tool_result = get_current_time()

# Append the result to the messages list

print(messages, "\n")

messages.append(response.choices[0].message)

print(messages, "\n")

messages.append({
    "role": "tool", "tool_call_id": tool_call.id, "content": str(tool_result)
})

print(messages)