Error for message's assistant tool calls' id object

Hello everyone.
I’m trying to make a call through with function calling history.
I’m using openai==1.12.0 and python 3.11.4.

The body of the message is like this:

[{'role': 'system',
  'content': 'you are an assistant-like content'},
{'role': 'user',
  'content': 'Question'},
 {'role': 'assistant',
  'tool_calls': {'id': 'call_id',
   'function': {'arguments': '{"args":"args"}',
    'name': 'get_function'},
   'type': 'function'}},
 {'tool_call_id': 'call_id',
  'role': 'tool',
  'content': "content"}]

and chat completion call it’s like the following:

response = client.chat.completions.create(
    model="gpt-3.5-turbo-0125",
    messages=messages,
    tools=tools,
    tool_choice = "auto",
    response_format={"type": "json_object"},
)

I obtain a Bad Request Error:
BadRequestError: Error code: 400 - {'error': {'message': "'id' is not of type 'object' - 'messages.2.tool_calls.0'", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Anybody any idea? from the API documentation the body of the message and the objects should be correctly called.

I wrote out a much longer tutorial, showing the building of tools as schemas, progressing to the inclusion of tools in a request that gets tool_call emitted to you, then through including the two messages that must be returned in order to continue.

1 Like

Hey @_j , thanks for the demo link, it is useful. But it is still not clear the reason for this error? Can you explain why we see this specific error and what is the way to solve this?

The basic fault is that you must write your code to anticipate and handle parallel tool calls and the tool IDs that accompany them.

The error is index 2, the replay of the assistant tool call.

I made a GPT for programming API, so lets give it the linked post, your post, and have it give a solution.


Let’s fix the code in question by addressing the parameter and structure issues for making tool calls within the OpenAI API framework.

Initial Review and Identification of Issues

The error described:

BadRequestError: Error code: 400 - {'error': {'message': "'id' is not of type 'object' - 'messages.2.tool_calls.0'", 'type': 'invalid_request_error', 'param': None, 'code': None}}

This suggests that there are one or more issues in how the tool_calls are structured in the messages object.

Primary Issue

The error message indicates that 'id' is not expected to be of type ‘object’. However, the error might actually be misleading as it’s not about the type of 'id' but rather how the tool_calls object is structured.

Problems in Your Provided Code

  1. The tool_calls field should be an array of objects, but it seems to be structured incorrectly.
  2. The field names and structures need to match exactly with what the API expects, especially in terms of nesting and object types.

Revised Code

Here is how you can structure your API call correctly:

from openai import OpenAI
client = OpenAI()

messages = [
    {'role': 'system', 'content': 'you are an assistant-like content'},
    {'role': 'user', 'content': 'Question'},
    {'role': 'assistant',
     'tool_calls': [
         {'id': 'call_id',
          'function': {
            'name': 'get_function',
            'arguments': '{"args":"args"}'
          },
          'type': 'function'}
     ]},
    {'tool_call_id': 'call_id', 'role': 'tool', 'content': "content"}
]

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_function",
            "description": "A function to get something.",
            "parameters": {
                "type": "object",
                "properties": {
                    "args": {"type": "string"}
                },
                "required": ["args"]
            },
        }
    }
]

response = client.chat.completions.create(
    model="gpt-3.5-turbo-0125",
    messages=messages,
    tools=tools,
    tool_choice="auto",
    response_format={"type": "json_object"}
)

# Handle and print the response or error
try:
    print(response)
except Exception as e:
    print(f"Error: {e}")

Explanation

  • Correcting Structure: tool_calls should be a list where each tool call is an object containing the id, type, and function fields.
  • Function Definition: Ensure that each tool used (here “get_function”) is defined in the tools parameter being passed to the API call. This was missing in your snippet, and could lead to errors if not properly defined.

This corrected structure should now properly make the API call without encountering the BadRequestError you saw previously.


I think the AI did pretty good. Here’s my links depiction of code, showing what would be sent manually, as a template of what’s right, but you must create list items yourself dynamically.

xparams['messages'].extend(
[
  {
    "role": "assistant",
    "content": "Let me look up the weather in those cities for you...",
    "tool_calls": [
         {
          "id": "call_rygjilssMBx8JQGUgEo7QqeY",
          "type": "function",
          "function": {
            "name": "get_weather_forecast",
            "arguments": "{\"location\": \"Seattle\", \"format\": \"fahrenheit\", \"time_period\": 1}"
          }
        },
        {
          "id": "call_pI6vxWtSMU5puVBHNm5nJhw3",
          "type": "function",
          "function": {
            "name": "get_weather_forecast",
            "arguments": "{\"location\": \"Miami\", \"format\": \"fahrenheit\", \"time_period\": 1}"
        },
    }
    ]
  }
]
)