Zod .refine() Rules Are Silently Dropped From JSON Schema Sent to LLMs

Passing a Zod 4 schema to structured-output helpers such as the OpenAI Node SDK or Vercel AI SDK silently discards every .refine() rule. The SDKs convert schemas with z.toJSONSchema(), and the converter only maps checks it recognizes, while custom refinements are stored under an unrecognized name and skipped. The refinements still run when the response is parsed, but the model is never told about them. The author illustrates this with a refund extraction schema, showing that regex, positive-number, enum and date rules survive while a field-level refinement and a cross-field date check do not. Transforms are also lost in input mode, leaving only a plain string.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in