liveBook · Manning

Structured data generation

As explained in Build AI-Enhanced Web Apps

The generation of machine-readable values organized according to a defined structure or schema rather than returned as unstructured text.

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);

How Build AI-Enhanced Web Apps covers it

How deep it goes
Traces schema-driven generation and validation, then stops short of a separate streamObject implementation example.
Examples
Demonstrates a Zod-validated product list generated with Gemini and displayed through console.table.
What it assumes
Assumes React and AI model integration, while introducing schema definition, validation, and typed structured responses.
What you can do after
Build it from scratch by defining Zod schemas and calling generateObject for typed data usable directly in an application.
How it connects
Connects prompt engineering, JSON, schema validation, Zod, React components, function calling, and the Vercel AI SDK.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.