Invalid schema error in function using arrays

Hello!

I’m trying to extract addresses from a text using a function call.
Since I want all the addresses I decided to go with an array.

I found an example in another post and used it as a template:

{
        name: 'get_weather', 
        description: 'Get weather from given locations and datetimes', 
        parameters: {
            type: 'object', 
            properties: {
                locations: {
                    type: 'array', 
                    items: {
                        type: "object",
                        properties: {
                            name: { type: "string", description: "Name of location, e.g. San Francisco, CA" },
                            datetime: { type: "string", description: "Date or time, e.g. today, tomorrow, 2023-06-29" },
                        }
                    }
                }
            }, 
            required: ['locations']
        }
 }

This is the function I wrote:

    functions = [{
                "name": "get_addresses",
                "description": "Get all unique addresses from a text",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "addresses": {
                            "type": "array",
                            "description": "The address of a company or building",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string",
                                        "description": "The name of the company or building"
                                    },
                                    "street": {
                                        "type": "string",
                                        "description": "The street name of the building or company"
                                    },
                                    "postcode": {
                                        "type": "string",
                                        "description": "The postcode of the building or company, e.g. 65496, 3824 ME, ..."
                                    },
                                    "city": {
                                        "type": "string",
                                        "description": "The city name of the building or company, e.g. Köln, Kopenhagen, ..."
                                    },
                                    "country_code": {
                                        "type": "string",
                                        "description": "The ISO country code of the building or company, e.g. UK, DE, DK"
                                    }
                                }
                            }
                        },
                        "required": ["addresses"]
                    }
                }
                }]

I can’t see any structural difference between the example and my code, but I always get the following error message:
openai.error.InvalidRequestError: Invalid schema for function ‘get_addresses’: [‘addresses’] is not of type ‘object’, ‘boolean’

What am I missing?
Or is there a better way to extract a list of results altogether?

Best regards
Ralf

It looks like your "required:" is nested incorrectly, try this

functions = [{
    "name": "get_addresses",
    "description": "Get all unique addresses from a text",
    "parameters": {
        "type": "object",
        "properties": {
            "addresses": {
                "type": "array",
                "description": "The address of a company or building",
                "items": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "description": "The name of the company or building"
                        },
                        "street": {
                            "type": "string",
                            "description": "The street name of the building or company"
                        },
                        "postcode": {
                            "type": "string",
                            "description": "The postcode of the building or company, e.g. 65496, 3824 ME, ..."
                        },
                        "city": {
                            "type": "string",
                            "description": "The city name of the building or company, e.g. Köln, Kopenhagen, ..."
                        },
                        "country_code": {
                            "type": "string",
                            "description": "The ISO country code of the building or company, e.g. UK, DE, DK"
                        }
                    },
                    "required": ["name", "street", "postcode", "city", "country_code"]
                }
            }
        },
        "required": ["addresses"]
    }
}]
1 Like

Yep, that’s it.
Thanks for the quick help!

Why exactly does it cause this error message though?
It made me look at the type definition and the nesting in the upper part of the code.
I didn’t think the error could be in the required section.

Not sure, error messages from code interpreters have always been a challenging aspect of computing in general, many memes are made in it’s honour.

1 Like