Uncategorized

artificial intelligence – Error when calling the OpenAI Chat API with functions available in Python


When I call the OpenAI Chat API in Python 3.11 with functions available to it to use via function calling, it returns an error saying openai.BadRequestError: Error code: 400 - {'error': {'message': "None is not of type 'object' - 'messages.9.function_call'", 'type': 'invalid_request_error', 'param': None, 'code': None}}.

This only happens when it has functions available and when it doesn’t then it works as it should. The problem started when OpenAI rewrote their library and I updated to the rewritten version.

With functions available – Error – Minimal reproducible example:

gpt_response = openai.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"}],
    functions=[
        {
            "name": "open_link",
            "description": "Open a URL in the user's default browser",
            "parameters": {
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "The URL to open, e.g. theverge.com",
                    }
                },
                "required": ["url"],
            }
        }
    ]

Without functions available – No error – Minimal reproducible example:

gpt_response = openai.chat.completions.create(
    model="gpt-3.5-turbo-1106,
    messages=[
            {"role": "system", "content": "You are a helpful assistant"}]

I am very sure it has something to do with the rewriting of the library and I have asked MS Copilot about the problem but it can’t solve it. Remember that the examples of my problem are not even close to my actual code but I think they should function the same and represent the problem in an easy way.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *