update the extract tool in v2 agent - #1593
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: c1d8cf1 The changes in this PR will be included in the next version bump. 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 |
Contributor
There was a problem hiding this comment.
2 issues found across 2 files
Confidence score: 3/5
- There is some user-impacting risk:
jsonSchemaToZodinlib/agent/tools/extract.tscan crash at runtime when nested property values are null/undefined. z.enum()is called even whenschema.enumis an empty array, which will throw at runtime; add a length check to avoid invalid enums.- Because these are concrete runtime errors in schema conversion, the merge risk is moderate rather than minimal.
- Pay close attention to
lib/agent/tools/extract.ts- runtime guards for null property values and empty enums.
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="lib/agent/tools/extract.ts">
<violation number="1" location="lib/agent/tools/extract.ts:13">
P1: Missing null/undefined guard in `jsonSchemaToZod` will cause runtime crash when nested property values are null. The parameter schema allows `z.unknown()` for property values, but the function immediately accesses `schema.type` without checking if schema exists.</violation>
<violation number="2" location="lib/agent/tools/extract.ts:31">
P1: `z.enum()` requires at least one enum value, but this check will pass when `schema.enum` is an empty array `[]` (empty arrays are truthy). This will cause a runtime error. Add a length check: `if (schema.enum && schema.enum.length > 0)`</violation>
</file>
Architecture diagram
sequenceDiagram
participant AI as AI Agent
participant Tool as Extract Tool
participant Logic as jsonSchemaToZod
participant Core as Stagehand Core
Note over AI, Tool: NEW: Schema passed as structured JSON object\n(formerly a string containing JS code)
AI->>Tool: execute({ instruction, schema: JsonSchema })
Tool->>Core: log(instruction, schema)
Note over Tool, Logic: NEW: Safe conversion strategy replacing\nunsafe "new Function()" evaluation
Tool->>Logic: Convert JSON Schema to Zod
loop Recursively parse schema properties
alt type is "object"
Logic->>Logic: Create z.object(properties)
else type is "array"
Logic->>Logic: Create z.array(items)
else type is "string"
opt format defined
Logic->>Logic: Apply .url(), .email(), or .uuid()
end
opt enum defined
Logic->>Logic: Create z.enum(values)
end
else primitives
Logic->>Logic: Create z.number(), z.boolean(), etc.
end
end
Logic->>Tool: ZodSchema Instance
alt Schema is not ZodObject
Tool->>Tool: Wrap as z.object({ result: schema })
end
Tool->>Core: page.extract({ instruction, schema: zodObject })
Core->>Tool: Extracted Data (JSON)
Tool->>AI: Return Result
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
seanmcguire12
approved these changes
Jan 22, 2026
Merged
miguelg719
pushed a commit
that referenced
this pull request
Jan 22, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to v2, this PR will be updated. # Releases ## @browserbasehq/stagehand@2.5.7 ### Patch Changes - [#1593](#1593) [`159f6d3`](159f6d3) Thanks [@tkattkat](https://github.com/tkattkat)! - Update extract tool ## @browserbasehq/stagehand-evals@1.1.7 ### Patch Changes - Updated dependencies \[[`159f6d3`](159f6d3)]: - @browserbasehq/stagehand@2.5.7 ## @browserbasehq/stagehand-examples@1.0.16 ### Patch Changes - Updated dependencies \[[`159f6d3`](159f6d3)]: - @browserbasehq/stagehand@2.5.7 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Release Stagehand 2.5.7 with an updated extract tool. Evals and examples are bumped to match this version. - **Dependencies** - @browserbasehq/stagehand → 2.5.7 - @browserbasehq/stagehand-evals → 1.1.7 (depends on 2.5.7) - @browserbasehq/stagehand-examples → 1.0.16 (depends on 2.5.7) <sup>Written for commit 9c438d0. Summary will update on new commits. <a href="https://cubic.dev/pr/browserbase/stagehand/pull/1594">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. --> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.
why
update extract handling
what changed
test plan
Summary by cubic
Switch the v2 agent extract tool to accept JSON Schema and convert it to Zod internally. This removes unsafe schema string evaluation and adds format support (url, email, uuid) for clearer, safer extraction.
Refactors
Migration
Written for commit c1d8cf1. Summary will update on new commits. Review in cubic