AI SDK CoreSchemas and Zod

Schema Specification and Validation with Zod

Tool usage and structured object generation require the specification of schemas. The AI SDK uses Zod, the most popular JavaScript schema validation library, for schema specification and validation.

You can install Zod with

pnpm
npm
yarn
pnpm install zod

You can then specify schemas, for example:

import z from 'zod';
const recipeSchema = z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(
z.object({
name: z.string(),
amount: z.string(),
}),
),
steps: z.array(z.string()),
}),
});

These schemas can be used to define parameters for tool calls and to generated structured objects with generateObject and streamObject.