Structured Output with mixed objects in the array

Hey there!

I’m trying to get my schema to accept two different objects within my response.

This is example JSON I’d like to get:

{
  "tasks": [
    {
      "title": "Lolipop",
      "type": "candy"
    },
    {
      "title": "Apples",
      "type": "fruit",
      "taste": "sweet",
      "cal": 1000
    },
    {
      "title": "Cucumber",
      "type": "vegetable",
      "taste": "regular",
      "cal": 200
    }
  ]
}

And here is my current scheme:

       let jsonSchema: [String: Any] = [
            "type": "json_schema",
            "json_schema": [
                "name": "groceries_schema", // Name of the schema
                "schema": [
                    "type": "object",
                    "properties": [
                        "tasks": [
                            "type": "array",
                            "items": [
                                "type": "object",
                                "properties": [
                                    "title": ["type": "string"],
                                    "type": [
                                        "type": "string",
                                        "enum": ["vegetable", "fruit", "candy"] // Define enum values for type
                                    ],
                                    "taste": [
                                        "type": ["string", "null"] // Allow date to be null if not provided
                                    ],
                                    "cal": [
                                        "type": ["string", "null"] // Allow time to be null if not provided
                                    ]
                                ],
                                "required": ["title", "type", "cal", "taste"], // Include all properties in required
                                "additionalProperties": false
                            ]
                        ]
                    ],
                    "required": ["tasks"], // Require the "tasks" array
                    "additionalProperties": false
                ],
                "strict": true // Enable strict mode
            ]
        ]

This makes the chat response with “cal”: null and “taste”: null, I’m trying to get results without those values if the chat doesn’t have information on those.

I’d love some help here, thank you!

1 Like

Hi @admin215 !

So if I understood correctly, if there is no data on calories, i.e. cal, it shouldn’t return anything?

According to the docs, the optional fields here are really just emulated, and a value for a field will always be returned.

So you need to implement your own business logic to check for null values and handle those as you wish.

Because I use Python + Pydantic, what I’ve done in the past is have two separate Pydantic models - one as a “response” model, just for handling structured output responses, and another a “canonical” model representing that data. Then I have a “factory” that takes the response model as input, does some additional logic/cleanup, and outputs the canonical model, which is then propagated via the rest of the application code.

1 Like

Hey @platypus, thank you for replying!

I’m using a raw JSON schema as I’m currently testing my idea through Swift.

Exactly. I’m giving the user the option to say what they ate today. The user may or may not include the calories (cal) and taste when describing what they ate. If those details aren’t provided, I expect the output not to include them.

I tried creating another object in the array with only [title, type] for such cases and used oneOf to pick the appropriate object, but it didn’t work. Do you know how I can mix two objects in one array?