Error when using recursive property in zod since 4.1
Discussed in https://github.com/vercel/ai/discussions/4688
Originally posted by defless February 4, 2025 Since updating ai sdk from 4.0 to 4.1 i got this error:
Recursive reference detected at #/properties/content/items/anyOf/4/anyOf/1/properties/content/items/anyOf/11/properties/cols/items/properties/content/items/anyOf/1! Defaulting to any
it seems related to the use of z.lazy()(references) in my zod schemas. Probably since https://github.com/vercel/ai/commit/33592d2614e24eadc96729bffff60b32dd3bd851
Isn't https://github.com/vercel/ai/blob/39af38fa816a04c6b6b39e74440b7447abbcf436/packages/ui-utils/src/zod-schema.ts#L11 causing the issue ? If its the case, we must be able to override this parameter
@lgrammel in the doc you import it from ai:
import { zodSchema } from 'ai
But ai does not export it, Isn't that more like it ?
import { zodSchema } from '@ai-sdk/ui-utils
Will add re-export
Identify issue type
Identified as Bug
Confidence: High
Rationale
This reports a concrete regression in recursive Zod schema handling, along with a missing documented export. Both concern broken current behavior rather than solely requesting a new capability.
Evidence
- Upgrading AI SDK from 4.0 to 4.1 causes recursive Zod schemas using
z.lazy()to emit a recursive-reference error and default the schema toany. - The reporter also found that the documented
zodSchemaimport fromaiwas not exported. - A maintainer confirmed they would add the missing re-export.