ai icon indicating copy to clipboard operation
ai copied to clipboard

Request: Support for Zod v4

Open AsharibAli opened this issue 7 months ago • 1 comments
trafficstars

Hi Maintainers,

Zod v4 was recently announced (Introducing Zod 4 beta) and is currently in beta. It brings significant improvements over v3, including:

  • Major performance gains (parsing strings, arrays, objects)
  • Reduced core bundle size
  • Improved TypeScript compiler efficiency
  • New features like z.interface(), JSON schema conversion, metadata, etc.

While Zod v4 introduces breaking changes (detailed in the Migration Guide), upgrading would allow users of ai to benefit from these enhancements, especially when defining complex schemas for tools or input/output validation.

Could you please consider adding support for Zod v4 in ai? It would be helpful to understand if there are plans to address compatibility with the upcoming stable release of Zod v4.

Thank you for your great work on this library!

AsharibAli avatar Apr 11 '25 04:04 AsharibAli

Maybe it would be better to consider supporting Standard Schema (#3282), although it doesn't support the JSON schema generation out of the box. There is an ongoing discussion regarding schema emission (standard-schema/standard-schema#7).

dannote avatar Apr 26 '25 05:04 dannote

FWIW yesterday zod v4 was released to stable, and is ready for production - so perhaps this request is even more relevant now, though I second @dannote in supporting Standard Schema rather than zod.

see:

  • announcement: https://zod.dev/v4
  • for library maintainers: https://zod.dev/library-authors

pattobrien avatar May 21 '25 02:05 pattobrien

👋🏼 we are working on supporting both v3 and v4 for validation for the upcoming stable release of [email protected]: https://ai-sdk.dev/docs/announcing-ai-sdk-5-alpha

Maybe it would be better to consider supporting Standard Schema

This should already be addressed via https://github.com/vercel/ai/pull/6184. Please give v5 a try to confirm. Note that it's not yet ready for production, but we very much appreciate feedback by early adopters.

gr2m avatar May 21 '25 20:05 gr2m

I ran into a problem with z4's new .toJSONSchema() method. I suggested an approach how to address it in zod 4 itself instead of working around it within the ai SKD. Would love to hear your thoughts: https://github.com/colinhacks/zod/issues/4498

gr2m avatar May 23 '25 16:05 gr2m

Last remaining error I run into is a "Type instantiation is excessively deep and possibly infinite" TypeScript error when trying to infer a type from a schema that might be z3 or z4. I created a minimal test case at https://github.com/colinhacks/zod/issues/4474#issuecomment-2913489124. I hope someone can help get us unblocked, I've been banging my head against it for a while now.

gr2m avatar May 27 '25 19:05 gr2m

The latest v5 alpha release implements support for v4 schemas for validation: [email protected]

Please give it a try and let us know if it works, or if you run into any problem.

One known issue is that zod v4's .toJSONSchema() does not set "type" for .literal() types, which causes problems with Google's APIs, see

  • https://github.com/colinhacks/zod/issues/4249
  • https://github.com/colinhacks/zod/pull/4590

gr2m avatar Jun 02 '25 16:06 gr2m

Last remaining error I run into is a "Type instantiation is excessively deep and possibly infinite" TypeScript error when trying to infer a type from a schema that might be z3 or z4. I created a minimal test case at colinhacks/zod#4474 (comment). I hope someone can help get us unblocked, I've been banging my head against it for a while now.

Was running into this issue on and off when experimenting with AI SDK v5 and using pnpm in a turborepo monorepo. I had some strange combination of:

  • ai installed globally (new-ish verison)
  • zod installed globally (old-ish version)
  • ai and other sub-packages installed in several apps / packages in the monorepo (v5 alpha latest)
  • zod installed in several apps / packages (latest)

When I dug inside one of the ai packages (specifically the @ai-sdk/provider-utils package) I saw that my machine was resolving the zod imports to my global, old-ish zod package and not the latest local version. Deleting the old global package installs resolved the issue for me!

financialvice avatar Jun 03 '25 06:06 financialvice

When I dug inside one of the ai packages (specifically the @ai-sdk/provider-utils package) I saw that my machine was resolving the zod imports to my global, old-ish zod package and not the latest local version. Deleting the old global package installs resolved the issue for me!

Thanks for sharing!

Peer dependencies have always been causing problems. We will add a note to our migration guide to help folks who run into this problem

One known issue is that zod v4's .toJSONSchema() does not set "type" for .literal() types, which causes problems with Google's APIs, see

The fix has been merged and released in v3.25.49. I have a draft PR ready to apply the fix, I'll finish it today and let you know once it's released

gr2m avatar Jun 03 '25 19:06 gr2m

The latest v5 alpha has been released, which includes the fix for JSON schema generated from zod v4's .literal()

https://github.com/vercel/ai/releases/tag/ai%405.0.0-alpha.9

gr2m avatar Jun 05 '25 16:06 gr2m