Skip to content

Conversation

@tkattkat
Copy link
Collaborator

Why

Adds support for zod 4 while maintaining backwards compatibility with zod 3

What changed

  • Introduced a zodCompat layer so Stagehand speaks both Zod v3 and v4 natively. Every place that used to import zod/v3, zod-to-json-schema, or poke at schema internals now routes through the same helpers (StagehandZodSchema, InferStagehandSchema, toJsonSchema, etc.). That keeps existing apps on v3 working while letting us upgrade to the v4 ecosystem.
  • Swapped all examples, eval tasks, tests, and LLM clients over to standard zod imports, updated dependencies to accept 3.25.76 || 4.1.8, and dropped zod-to-json-schema. Tool plumbing (Anthropic/Google CUA) and custom clients now produce the right JSON Schema regardless of which Zod flavor the user passes in.
  • Tightened typings so structured LLM calls return { data, usage } directly no more as casts throughout inference or the samples.

Manual testing
To make sure nothing regressed, I ran:
Custom OpenAI client example (schema conversion + structured output).
Standard extract (including URL injection path).
Google computer-use agent with a custom tool.
Anthropic computer-use agent with a custom tool.
All behaved as before

tkattkat and others added 30 commits October 22, 2025 10:28
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
- Moved complete LLMUsage and LLMParsedResponse definitions to LLMClient.ts
- Added reasoning_tokens and cached_input_tokens to LLMUsage interface
- Re-export types from inference.ts for backward compatibility
- Keeps cleaner architecture with types in their proper location
@changeset-bot
Copy link

changeset-bot bot commented Nov 18, 2025

🦋 Changeset detected

Latest commit: d66a5d6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

92 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format

@seanmcguire12 seanmcguire12 added act These changes pertain to the act function extract These changes pertain to the extract function and removed act These changes pertain to the act function extract These changes pertain to the extract function labels Nov 18, 2025
@seanmcguire12 seanmcguire12 changed the title Zodv34 add support for zod v4 Nov 19, 2025
@seanmcguire12
Copy link
Member

@greptileai

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

93 files reviewed, no comments

Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format

@browserbase browserbase deleted a comment from greptile-apps bot Nov 19, 2025
@seanmcguire12
Copy link
Member

@greptileai

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Nov 19, 2025

Greptile Summary

  • Introduced zodCompat abstraction layer enabling dual support for Zod v3 (via zod-to-json-schema) and v4 (via built-in z.toJSONSchema()) with runtime version detection through _zod property inspection
  • Updated all LLM clients (Anthropic, OpenAI, Cerebras, Groq) to use unified toJsonSchema() helper instead of direct zod-to-json-schema or zodResponseFormat calls, ensuring consistent schema conversion across providers
  • Extended utils.ts schema introspection to handle both Zod v3 and v4 internal structures (_def vs _zod.def) for URL field transformation in extraction workflows

Confidence Score: 4/5

  • This PR is safe to merge with thorough manual testing already completed across multiple scenarios
  • The abstraction layer is well-designed with clear version detection logic, and the PR includes comprehensive manual testing of critical paths (OpenAI client, extract, Google/Anthropic CUA with custom tools). However, one point deducted because the implementation relies on runtime property detection (_zod) which is implementation-dependent and could be fragile if Zod internals change, and the fallback error path in toJsonSchema (line 48) could cause runtime failures if Zod v4.1+ assumptions don't hold
  • packages/core/lib/v3/zodCompat.ts requires attention due to reliance on internal Zod properties for version detection

Important Files Changed

Filename Overview
packages/core/lib/v3/zodCompat.ts Introduces abstraction layer to support both Zod v3 and v4, with type guards and schema conversion utilities
packages/core/lib/utils.ts Extended schema introspection helpers to support both Zod v3 and v4 internals for transformSchema operations
packages/core/package.json Changed Zod peer dependency to accept "3.25.76

Sequence Diagram

sequenceDiagram
    participant User
    participant Stagehand
    participant zodCompat
    participant LLMClient
    participant LLM as "LLM Provider"

    User->>Stagehand: extract(instruction, zodSchema)
    Stagehand->>zodCompat: toJsonSchema(zodSchema)
    zodCompat->>zodCompat: isZod4Schema(schema)?
    alt Zod v4
        zodCompat->>zodCompat: z.toJSONSchema(schema)
    else Zod v3
        zodCompat->>zodCompat: zodToJsonSchema(schema)
    end
    zodCompat-->>Stagehand: jsonSchema
    Stagehand->>LLMClient: createChatCompletion(options)
    LLMClient->>LLM: API call with jsonSchema
    LLM-->>LLMClient: structured response
    LLMClient-->>Stagehand: {data, usage}
    Stagehand-->>User: extracted data
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

93 files reviewed, no comments

Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format

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

Labels

act These changes pertain to the act function extract These changes pertain to the extract function

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants