Structured outputs with arrays

Is there a way to use structured outputs with arrays as root element ?

Issue

I want to parse collection of objects from textual data. So I need the model to return it as a collection to parse and use in list / array. Here’s how I’m using structured outputs in .NET SDK

var chatCompletionOptions = new ChatCompletionOptions
{
    ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
        jsonSchemaFormatName: $"{nameof(Employee)}s",
        jsonSchemaFormatDescription: "Employees list",
        jsonSchema: BinaryData.FromString(generator.Generate(typeof(Employee[])).ToString()))
};

When sending a prompt with these options, it fails from API side with exception :

HTTP 400 (invalid_request_error: )
Parameter: response_format

Invalid schema for response_format ‘Employees’: schema must be a JSON Schema of ‘type: “object”’, got ‘type: “array”’.

Is there a fix for this ?

2 Likes

Hi @sultonbek.rakhimov and welcome to the community!

Yes, so you just need to go down one more level and define your array. The root has to be a JSON object. This is how it would look in vanilla JSON:

response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "employee_list",
            "strict": True,
            "description": "Employee list",
            "schema": {
                "type": "object",
                "properties": {
                    "employees": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "required": ["employees"],
                "additionalProperties": False
            }
        }
    }

Unfortunately you have to have thisemployee_list object root at the very top.

1 Like

Thanks @platypus it worked )

1 Like