A single object is not always the right shape: a list of books, tasks or appointments needs a JSON array, with each item following the same item schema. Structured Outputs supports this by letting the schema describe an array of objects. The reply is then a list your code can loop over without parsing text.
Prerequisites
- A JSON Schema that describes an array of objects.
- The list you want, with a clear item count or a clear stopping rule.
- A JSON parser in your code.
Steps
Step 1: Schema the array
Write the schema as an object with one property that is an array, or as a top-level array if your API allows it. Give the array a name, such as books, so the reply has a clear structure.
Step 2: Name the item fields
Define each item’s fields: for a book, title and author; for a task, title and owner. Mark the fields your code needs as required on the item, not only on the outer object.
Step 3: Count the items
After the call, count the items in the array and check the count against what you asked for. A short list may mean the model stopped early, even though the JSON is valid.
Worked example: Harbor Light library new arrivals
The fictional Harbor Light library asks for its 4 newest arrivals as a JSON array with title and author for each book.
| Item | Title | Author |
|---|---|---|
| 1 | The Salt Road | M. Ferreira |
| 2 | A Field Guide to Birds | J. Lindqvist |
| 3 | Baking with Rye | A. Novak |
| 4 | The Long Winter | S. Adeyemi |
The reply is a JSON array with 4 items, each with a title and an author, and the count matches the 4 requested.
Expected result. The reply is a JSON array with the requested item count, and every item has the fields your code reads. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.
Limits
- An array schema cannot force the model to know 4 real items; it can only force the shape of 4 items.
- A truncated reply can cut an array short; check the count and the finish reason together.
- Item counts above about 20 get unreliable; split large lists into several calls.
