Skip to content

Commit bd129fb

Browse files
authored
docs(zod-schema): add warning about .meta() / .describe() method chaining order (#10452)
## Summary Adds a warning note explaining that `.meta()` and .`describe()` must be called at the end of schema method chains to preserve metadata in JSON schema output, (just to prevent confusion) ## Related Issues closes #10005 see also colinhacks/zod#5447
1 parent e9558f1 commit bd129fb

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)