Skip to content

Commit ebfa423

Browse files
Backport: docs(zod-schema): add warning about .meta() / .describe() method chaining order (#10496)
This is an automated backport of #10452 to the release-v5.0 branch. FYI @IdoBouskila Co-authored-by: Ido Bouskila <[email protected]>
1 parent fa24ffe commit ebfa423

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

content/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ You can use it to [generate structured data](/docs/ai-sdk-core/generating-struct
1717
the `zodSchema()` helper function instead.
1818
</Note>
1919

20+
<Note type="warning">
21+
When using `.meta()` or `.describe()` to add metadata to your Zod schemas,
22+
make sure these methods are called **at the end** of the schema chain.
23+
24+
metadata is attached to a specific schema
25+
instance, and most schema methods (`.min()`, `.optional()`, `.extend()`, etc.)
26+
return a new schema instance that does not inherit metadata from the previous one.
27+
Due to Zod's immutability, metadata is only included in the JSON schema output
28+
if `.meta()` or `.describe()` is the last method in the chain.
29+
30+
```ts
31+
// ❌ Metadata will be lost - .min() returns a new instance without metadata
32+
z.string().meta({ describe: 'first name' }).min(1);
33+
34+
// ✅ Metadata is preserved - .meta() is the final method
35+
z.string().min(1).meta({ describe: 'first name' });
36+
```
37+
38+
</Note>
39+
2040
## Example with recursive schemas
2141

2242
```ts

0 commit comments

Comments
 (0)