fix: fallback to zod-to-json-schema when Zod v4 toJSONSchema is missing · Pull Request #1859 · browserbase/stagehand

When a Zod v4 schema is detected but the root zod import doesn't
expose z.toJSONSchema() (e.g., due to bundler resolution or older
Zod v4 builds), the code threw "Zod v4 toJSONSchema method not found".

Fall back to the zod-to-json-schema library instead of throwing,
since it can handle both v3 and v4 schemas.

Fixes #1845

cubic-dev-ai[bot]

greptile-apps[bot]

The previous fallback to zod-to-json-schema does not support Zod v4
schemas at runtime (ZodFirstPartyTypeKind was removed in v4). Instead,
import toJSONSchema directly from "zod" as a named import so it is
resolved at module load time and cannot be stripped by bundlers.

cubic-dev-ai[bot]

Reverts the static named import which breaks when Zod v3 is
installed (toJSONSchema doesn't exist in v3). Instead, keep the
original runtime check on the z namespace and fall back to
zod-to-json-schema when it's not available.

cubic-dev-ai[bot]

@ghost ghost closed this by deleting the head repository

Mar 26, 2026

This pull request was closed.