40. Advanced Structured Output
Overview and links for this section of the guide.
On this page
Beyond Simple JSON
Basic structured output is getting JSON back from the model. Advanced structured output is building production-grade systems with:
- Deep schemas with nested objects and arrays
- Error handling that's machine-parseable
- Schema evolution without breaking clients
- Internationalization for multi-locale outputs
- Contract testing to ensure compliance
Key Techniques
// Advanced schema with Zod
const ProductCatalogSchema = z.object({
products: z.array(z.object({
id: z.string().uuid(),
name: z.string().min(1),
price: z.object({
amount: z.number().positive(),
currency: z.enum(['USD', 'EUR', 'GBP'])
}),
variants: z.array(z.object({
sku: z.string(),
attributes: z.record(z.string()) // Flexible key-value
})).optional(),
metadata: z.object({
createdAt: z.string().datetime(),
tags: z.array(z.string())
})
})),
pagination: z.object({
page: z.number().int().positive(),
totalPages: z.number().int().nonnegative(),
hasMore: z.boolean()
})
});
Where to go next
Explore next
40. Advanced Structured Output sub-sections
5 pages
40.1 Deep schemas: nested objects and arrays
Open page
40.2 "Explain errors as JSON" for debuggability
Open page
40.3 Schema evolution and backwards compatibility
Open page
40.4 Internationalization schemas (multi-locale outputs)
Open page
40.5 Contract tests for schema compliance
Open page