Structured data generation asks an AI model for organized, type-safe values such as JSON instead of raw text. Defined schemas make responses easier to validate, display, and pass to other tools. section 4.3 section 4.2.3
A React component initiates the action, the Vercel AI SDK sends a structured prompt, the model returns a raw response, and a schema validator checks it before the client updates the UI. section 4.3.1
Figure 4.11 This sequence diagram illustrates the process of generating structured data from an AI model using the Vercel AI SDK. The flow begins with a user action and ends with the display of validated, structured data in the user interface. Key components include the React component, Vercel AI SDK, AI model, and schema validator.
Use the sequence diagram to follow the path from user action through the React component, Vercel AI SDK, AI model, schema validator, and updated interface. section 4.3.1
Choose the Vercel AI SDK helper according to the response workflow:
- Use generateObject for one-time structured generation. Provide a Zod schema and completion function; receive a validated typed object, with retries available when validation fails. section 4.3.3
- Use streamObject for real-time structured updates. It streams model output, repeatedly parses and validates it, and returns valid objects as they become available. section 4.3.3
Generating a typed product list
import { z } from 'zod'; import { google } from '@ai-sdk/google'; import { generateObject } from 'ai'; const ProductSchema = z.object({ name: z.string(), description: z.string(), price: z.number(), category: z.string() }); const ProductListSchema = z.array(ProductSchema); const { object: { products } } = await generateObject({ model: google('models/gemini-2.0-flash'), schema: z.object({ products: ProductListSchema }), prompt: 'Generate five products related to cereal types.' }); console.table(products);
