Convert Zod validation models
You need an AI SDK schema from an existing Zod definition.
AI Tools · Function
zodSchema converts a Zod schema into an AI SDK-compatible schema. It is useful for structured data generation and tools, especially when conversion options such as references must be configured.
Call zodSchema with a Zod schema and, optionally, conversion settings. It produces an AI SDK-compatible schema that contains the generated JSON Schema representation together with validation behavior, making it suitable for structured generation and tool definitions.
zodSchema(zodSchema, options?)| Name | Type | Description |
|---|---|---|
zodSchema | z.Schema | The Zod schema definition. |
options | object | Additional options for the schema conversion. |
options.useReferences | boolean | Enables support for references in the schema; defaults to false. |
A Schema object compatible with the AI SDK, containing both the JSON Schema representation and validation functionality.
useReferences option enables schema references when required.useReferences: true for recursive schemas such as those built with z.lazy; without references, recursive conversion is not supported.import { zodSchema } from 'ai';
import { z } from 'zod';
const userSchema = zodSchema(
z.object({
name: z.string(),
age: z.number().int(),
}),
);
console.log(userSchema);Wraps a Zod object so it can be supplied wherever the AI SDK expects a schema.
You need an AI SDK schema from an existing Zod definition.
A structured-generation operation needs a typed output shape.
A tool needs validated, model-visible input parameters.
A recursive Zod definition must be represented with references.
zodSchema return?zodSchema converts a Zod schema into an AI SDK-compatible schema. The result includes both a JSON Schema representation and validation functionality.
Follow-up: Why use the helper instead of passing a Zod object directly? Use it when conversion options such as useReferences must be specified.
The first parameter is the Zod schema definition. The optional second parameter contains conversion settings, including useReferences, which defaults to false.
Follow-up: When should useReferences be enabled? Enable it for recursive schemas, for example schemas using z.lazy, subject to provider support.
zodSchema call be used?Zod objects can often be passed directly to AI SDK functions because the SDK converts them internally. zodSchema is useful when explicit schema conversion configuration is needed.
Follow-up: Where can the resulting schema be used? It can support structured data generation and tool definitions.
generateTexttoolsjsonSchemagenerate structured data