openai-python: None is not of type ‘object’

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

When calling functions with no input arguments it gives this error

response = openai_client.chat.completions.create(timeout=10,
  File "/usr/local/lib/python3.9/site-packages/openai/_utils/_utils.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/openai/resources/chat/completions.py", line 556, in create
    return self._post(
  File "/usr/local/lib/python3.9/site-packages/openai/_base_client.py", line 1055, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/usr/local/lib/python3.9/site-packages/openai/_base_client.py", line 834, in request
    return self._request(
  File "/usr/local/lib/python3.9/site-packages/openai/_base_client.py", line 877, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.OpenAIError: Error code: 400 - {'error': {'message': "None is not of type 'object' - 'messages.2.function_call'", 'type': 'invalid_request_error', 'param': None, 'code': None}}

This used to work before the recent updates.

I also posted this bug in the community forum since it is related to the API https://community.openai.com/t/none-is-not-of-type-object/488873

To Reproduce

An example of the function I try to call:

tools = [
  {
      "type": "function",
      "function": {
          "name":  "escalate_to_manager",
          "description": "Contact the manager when there is nothing else that can be done",
          "parameters": {
              "type": "object",
              "properties": {},
              "required": [],
          },
      }
  }
]

And I am calling the chat completion normally:

response = client.chat.completions.create(timeout=10,
                                         model="gpt-3.5-turbo-1106",
                                         messages=messages,
                                         tools=tools,
                                         tool_choice="auto",
                                         temperature=0,
                                         )

Code snippets

No response

OS

debian bullseye

Python version

python 3.9

Library version

openai v1.1.1

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Comments: 22

Most upvoted comments

I think the issue is related with “function_call” being null (or None in this case), I have removed it from incoming message of openai and the problem is gone.

problem stopped after I did @aemr3 's advice, thank you

You likely need to pass exclude_unset=True when converting the model into a dictionary.

@ganlbarone

{‘timeDisplay’: {‘type’: ‘string’, ‘description’: ‘Time must be displayed in a time stamp format YYYY-MM-DD 00:00:00 at midnight.’}}, ‘type’: ‘object’, ‘required’: [‘timeDisplay’]}}] is not of type ‘object’ - ‘too ls.0.function’",

Just a guess without knowing the tool api: there you say timeDisplay is 'type': 'string' and then the error says 'timeDisplay' is not type 'object'. But I don’t know really.

Maybe using the python client would be easier to get those things right.

I actually ended up figuring out the problem. The function node in the json cant be an array. That was the part that was breaking it. Tools is an array, that can contain multiple funtions, but function itself is singular.

It isnt working exactly how I want/expected it to from a feature standpoint, but that is a me problem to sort out now that the JSON is structured appropriately and the API isnt erroring.

Anyway, just posting this back in case anyone runs into this type of error, their return errors could likely be a bit more robust than they are.

{
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "GetTime",
                "description": "Convert the user prompt to a time stamp in the format YYYY-MM-DD HH:MM:SS, assume a start date of 01/30/2024 14:31:03",
                "parameters": {
                    "properties": {
                        "timeString": {
                            "type": "string",
                            "description": "Convert the user prompt to a time stamp in the format YYYY-MM-DD HH:MM:SS, assume a start date of 01/30/2024 14:31:03"
                        }
                    },
                    "type": "object",
                    "required": [
                        "timeString"
                    ]
                }
            }
        }
    ],
    "tool_choice": {
        "function": {
            "name": "GetTime"
        },
        "type": "function"
    },
    "messages": [
        {
            "content": "Convert the user prompt to a time stamp in the format YYYY-MM-DD HH:MM:SS, assume a start date of 01/30/2024 14:31:03.",
            "role": "system"
        },
        {
            "content": "book a meeting for 60 min in a few weeks",
            "role": "user"
        }
    ],
    "max_tokens": 200,
    "temperature": 0.1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "top_p": 0.95,
    "stop": null
}

I have the same issue with openai 1.2.3. Error message is:

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

Message logs are like this:

[{"role":"system","content":"You are  [REDACTED]"},{"role":"user","content":"[REDACTED]"},{"content":null,"function_call":null,"role":"assistant","tool_calls":[{"index":0,"id":"[REDACTED]","function":{"arguments":"{\"query\": \"[REDACTED]\"}","name":"[REDACTED]"},"type":"function"},{"index":1,"id":"[REDACTED]","function":{"arguments":"{}","name":"[REDACTED]"},"type":"function"},{"index":2,"id":"[REDACTED]","function":{"arguments":"{}","name":"[REDACTED]"},"type":"function"}]},{"tool_call_id":"[REDACTED]","role":"tool","name":"[REDACTED]","content":"[REDACTED]"},{"tool_call_id":"[REDACTED]","role":"tool","name":"[REDACTED]","content":"[REDACTED]"},{"tool_call_id":"[REDACTED]","role":"tool","name":"[REDACTED]","content":"[REDACTED]"}]

Sorry, I had to redact confidential information.