Skip to content

fix: convert zod 4 and separately installed zod 3 schemas to JSON Schema - #266

Open
vidiyala99 wants to merge 3 commits into
exa-labs:masterfrom
vidiyala99:fix/zod-schema-detection
Open

vidiyala99 wants to merge 3 commits into
exa-labs:masterfrom
vidiyala99:fix/zod-schema-detection

Conversation

@vidiyala99

@vidiyala99 vidiyala99 commented Nov 19, 2026 •

Copy link
Copy Markdown

Problem

isZodSchema checks obj instanceof ZodType against the SDK's own zod/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:

  • zod 4 schemas (import { z } from "zod" on zod 4, or zod/mini), even when the app and SDK share one zod install, because a zod 4 schema is never an instance of zod/v3's ZodType.
  • zod 3 schemas from a separate copy, e.g. an app on zod 3.23, which npm installs apart from the SDK's ^3.25.28 || ^4.6.6.

Affected: summary.schema (search / getContents / findSimilar / searchAndContents), answer / streamAnswer outputSchema, and agent run outputSchema.

Live check against the API (exa-js 2.28.0, getContents with a summary schema { headline, launch_year: int, topic: enum }):

schema sent as summary returned
JSON Schema (control) JSON Schema {headline, launch_year, topic}
zod 4 (import { z } from "zod") {"def": ...} {model_name, release_date, key_improvements}
zod 3.23 (separate copy) {"_def": ...} {summary: "..."}

All three responses were 200 with no warning.

Fix

  • Detect by shape, not instanceof: zod 4 schemas by _zod.def plus a _zod.run function (so a plain JSON Schema that happens to carry a _zod key passes through untouched), zod 3 by _def + _parse. zod 4 is checked first because zod 4 classic also exposes _def.
  • Convert zod 4 schemas to draft-07 by their output types, following zod's library author guidance. Classic schemas use their own toJSONSchema method, so a newer app zod is converted by its own converter: the SDK's zod/v4/core from zod 3.25 drops minLength from a zod 4.6 .min(). zod/mini schemas have no method and use the SDK's zod/v4/core, which ships in every zod the SDK allows (3.25.28+ and 4.x). zod 3 schemas still go through zod-to-json-schema as before.
  • A .transform() field has no output type zod can describe and would be sent as {}. Only that field is described by its input side instead (what zod-to-json-schema does for zod 3 effects); sibling fields such as codecs and .default() keep their output form. Types with no JSON Schema form, like z.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 on SummaryOptions #93. T is inferred for both (covered by type tests).

No dependency or public API changes.

Tests

New test/unit/zod-versions.test.ts covers zod 3, zod 3 from a separately loaded copy (a fresh module instance, asserted not instanceof the SDK's ZodType), zod 4 classic and zod 4 mini, across detection, conversion (including .describe() descriptions), getContents, answer, streamAnswer and agent.runs.create, plus non-schema values (including JSON Schema with a _zod key) staying undetected, transformed fields, mixed schemas (transform + date + codec + default), and the schema's own converter being used. Against current master, most of them fail (every separate-copy, zod 4 and zod mini case).

  • vitest.unit.config.ts: 34 files, 535 tests pass
  • tsc --noEmit passes, npm run build passes
  • The built dist (CJS and ESM) was also checked with the SDK resolving zod 3.25.28, the bottom of the supported range: both zod (v3) and zod/v4 schemas convert, and a separately installed zod 4.6.7 schema keeps minLength.

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 through globalThis. A zod/mini schema from a newer zod than the SDK resolves can lose constraints such as minLength, since mini has no converter of its own to use.

Refs #93

🤖 Generated with Claude Code

https://claude.ai/code/session_01KQzogfNAeakir5aPnsm89p

vidiyala99 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant