Repository navigation
fix: convert zod 4 and separately installed zod 3 schemas to JSON Schema - #266
Open
vidiyala99 wants to merge 3 commits into
Open
vidiyala99 wants to merge 3 commits into
vidiyala99 wants to merge 3 commits into
Conversation
added 3 commits
November 18, 2026 17:25
isZodSchema checked instanceof the SDK's own zod/v3 ZodType, so schemas from zod 4 (classic or mini) or from a separate copy of zod 3 were not detected and were sent to the API as raw zod internals, which the API silently ignores. Detect schemas by shape (zod 4: _zod.def; zod 3: _def + _parse) and convert zod 4 schemas with toJSONSchema from zod/v4/core (draft-07, inlined). Refs exa-labs#93
…stricter zod 4 detection
- Convert classic zod 4 schemas with their own toJSONSchema method; the SDK's
zod/v4/core is the fallback for zod/mini (an older one drops .min()).
- Require _zod.run, so a JSON Schema with a _zod extension key passes through.
- When output types throw (transforms), use input types with strip objects
closed, instead of sending the field as {}.
A z.date() anywhere used to switch the whole schema to input types, which
changed codec and default fields. Convert by output types with unrepresentable
types as {}, and replace only pipes ending in a transform with their input side.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
isZodSchemachecksobj instanceof ZodTypeagainst the SDK's ownzod/v3. Any schema that isn't an instance of that exact class is not detected, so it is sent to the API as raw zod internals ({"def": ...}or{"_def": ...}). The API accepts the request and ignores the schema, so the call "succeeds" with unstructured output and no error.This happens for:
import { z } from "zod"on zod 4, orzod/mini), even when the app and SDK share one zod install, because a zod 4 schema is never an instance ofzod/v3'sZodType.^3.25.28 || ^4.6.6.Affected:
summary.schema(search / getContents / findSimilar / searchAndContents),answer/streamAnsweroutputSchema, and agent runoutputSchema.Live check against the API (exa-js 2.28.0,
getContentswith a summary schema{ headline, launch_year: int, topic: enum }):{headline, launch_year, topic}import { z } from "zod"){"def": ...}{model_name, release_date, key_improvements}{"_def": ...}{summary: "..."}All three responses were 200 with no warning.
Fix
instanceof: zod 4 schemas by_zod.defplus a_zod.runfunction (so a plain JSON Schema that happens to carry a_zodkey passes through untouched), zod 3 by_def+_parse. zod 4 is checked first because zod 4 classic also exposes_def.toJSONSchemamethod, so a newer app zod is converted by its own converter: the SDK'szod/v4/corefrom zod 3.25 dropsminLengthfrom a zod 4.6.min().zod/minischemas have no method and use the SDK'szod/v4/core, which ships in every zod the SDK allows (3.25.28+ and 4.x). zod 3 schemas still go throughzod-to-json-schemaas before..transform()field has no output type zod can describe and would be sent as{}. Only that field is described by its input side instead (whatzod-to-json-schemadoes for zod 3 effects); sibling fields such as codecs and.default()keep their output form. Types with no JSON Schema form, likez.date(), are still{}, as for zod 3.ZodSchema<T>now accepts zod 3 and zod 4 schemas (Zod3Schema<T> | Zod4Schema<T>), still typed by shape so it doesn't hit the deep instantiation from Excessively deep type instantiation onSummaryOptions#93.Tis inferred for both (covered by type tests).No dependency or public API changes.
Tests
New
test/unit/zod-versions.test.tscovers zod 3, zod 3 from a separately loaded copy (a fresh module instance, asserted notinstanceofthe SDK'sZodType), zod 4 classic and zod 4 mini, across detection, conversion (including.describe()descriptions),getContents,answer,streamAnswerandagent.runs.create, plus non-schema values (including JSON Schema with a_zodkey) staying undetected, transformed fields, mixed schemas (transform + date + codec + default), and the schema's own converter being used. Against currentmaster, most of them fail (every separate-copy, zod 4 and zod mini case).vitest.unit.config.ts: 34 files, 535 tests passtsc --noEmitpasses,npm run buildpassesdist(CJS and ESM) was also checked with the SDK resolving zod 3.25.28, the bottom of the supported range: bothzod(v3) andzod/v4schemas convert, and a separately installed zod 4.6.7 schema keepsminLength.Known limit: if the app's zod 4 is a separate copy older than the SDK's (4.0 to 4.6.5),
.describe()text may be dropped, because those versions keep metadata in a per-copy registry. The schema structure still converts. Newer zod 4 shares the registry throughglobalThis. Azod/minischema from a newer zod than the SDK resolves can lose constraints such asminLength, since mini has no converter of its own to use.Refs #93
🤖 Generated with Claude Code
https://claude.ai/code/session_01KQzogfNAeakir5aPnsm89p