ai icon indicating copy to clipboard operation
ai copied to clipboard

Error when using recursive property in zod since 4.1

Open lgrammel opened this issue 1 year ago • 3 comments

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 avatar Feb 05 '25 09:02 lgrammel

@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

defless avatar Feb 05 '25 14:02 defless

Will add re-export

lgrammel avatar Feb 05 '25 14:02 lgrammel

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 to any.
  • The reporter also found that the documented zodSchema import from ai was not exported.
  • A maintainer confirmed they would add the missing re-export.

lgrammel avatar Jul 22 '26 16:07 lgrammel