40. Advanced Structured Output

Overview and links for this section of the guide.

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