|
| 1 | +--- |
| 2 | +title: zodSchema |
| 3 | +description: Helper function for creating Zod schemas |
| 4 | +--- |
| 5 | + |
| 6 | +# `zodSchema()` |
| 7 | + |
| 8 | +`zodSchema` is a helper function that converts a Zod schema into a JSON schema object that is compatible with the AI SDK. |
| 9 | +It takes a Zod schema and optional configuration as inputs, and returns a typed schema. |
| 10 | + |
| 11 | +You can use it to [generate structured data](/docs/ai-sdk-core/generating-structured-data) and in [tools](/docs/ai-sdk-core/tools-and-tool-calling). |
| 12 | + |
| 13 | +<Note> |
| 14 | + You can also pass Zod objects directly to the AI SDK functions. Internally, |
| 15 | + the AI SDK will convert the Zod schema to a JSON schema using `zodSchema()`. |
| 16 | + However, if you want to specify options such as `useReferences`, you can pass |
| 17 | + the `zodSchema()` helper function instead. |
| 18 | +</Note> |
| 19 | + |
| 20 | +## Example with recursive schemas |
| 21 | + |
| 22 | +```ts |
| 23 | +import { zodSchema } from 'ai'; |
| 24 | +import { z } from 'zod'; |
| 25 | + |
| 26 | +// Define a base category schema |
| 27 | +const baseCategorySchema = z.object({ |
| 28 | + name: z.string(), |
| 29 | +}); |
| 30 | + |
| 31 | +// Define the recursive Category type |
| 32 | +type Category = z.infer<typeof baseCategorySchema> & { |
| 33 | + subcategories: Category[]; |
| 34 | +}; |
| 35 | + |
| 36 | +// Create the recursive schema using z.lazy |
| 37 | +const categorySchema: z.ZodType<Category> = baseCategorySchema.extend({ |
| 38 | + subcategories: z.lazy(() => categorySchema.array()), |
| 39 | +}); |
| 40 | + |
| 41 | +// Create the final schema with useReferences enabled for recursive support |
| 42 | +const mySchema = zodSchema( |
| 43 | + z.object({ |
| 44 | + category: categorySchema, |
| 45 | + }), |
| 46 | + { useReferences: true }, |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +## Import |
| 51 | + |
| 52 | +<Snippet text={`import { zodSchema } from "ai"`} prompt={false} /> |
| 53 | + |
| 54 | +## API Signature |
| 55 | + |
| 56 | +### Parameters |
| 57 | + |
| 58 | +<PropertiesTable |
| 59 | + content={[ |
| 60 | + { |
| 61 | + name: 'zodSchema', |
| 62 | + type: 'z.Schema', |
| 63 | + description: 'The Zod schema definition.', |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'options', |
| 67 | + type: 'object', |
| 68 | + description: 'Additional options for the schema conversion.', |
| 69 | + properties: [ |
| 70 | + { |
| 71 | + type: 'object', |
| 72 | + parameters: [ |
| 73 | + { |
| 74 | + name: 'useReferences', |
| 75 | + isOptional: true, |
| 76 | + type: 'boolean', |
| 77 | + description: |
| 78 | + 'Enables support for references in the schema. This is required for recursive schemas, e.g. with `z.lazy`. However, not all language models and providers support such references. Defaults to `false`.', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + ]} |
| 85 | +/> |
| 86 | + |
| 87 | +### Returns |
| 88 | + |
| 89 | +A Schema object that is compatible with the AI SDK, containing both the JSON schema representation and validation functionality. |
0 commit comments