Repository navigation
[feat]: let extract() return element references - #3447
Open
martentropp wants to merge 1 commit into
Open
martentropp wants to merge 1 commit into
martentropp wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 524ec62 The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
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
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
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.
Closes #3406
why
extract()returns data andobserve()returns elements, but no call returns both. #3406 asks for "these values, and the element each one came from", so code can pick a value deterministically and then act on its element without the LLM doing the comparison.what changed
Adds
elementRef(), a schema helper that resolves to the element a value came from, the same wayz.url()resolves to a link's URL today:sdk-ts):elementRef(description?)returnsz.string()tagged with"x-stagehand": "element"in its JSON Schema.transformSchemaswaps marked fields for element-ID fields, alongside URL fields. After inference,extractServiceresolves them through the snapshot's XPath map toxpath=selectors, the same format asobserve(). The marker is never sent to the model provider.$refs, a non-object root, and optional, nullable, default, catch and readonly wrappers. An element field inside a record or recursive schema throws a clear error before inference instead of returning free text."", like URL fields, and log a warning, likeobserve().extract()calls get the same prompt as before.basics/extract.mdx(TypeScript, and Python using the raw marker) and a pointer from the reference page.notes for reviewers
schemais already free-form JSON, so there's no wire schema change. However, a new SDK talking to an older extension (for example one preinstalled on a Browserbase session) would treat element fields as plain strings and return unresolved model text. Since the extension ships inside each SDK, I left the protocol alone, but I'm happy to add a protocol minor bump if you'd prefer new clients to reject older extensions."x-stagehand": "element"string is defined in bothextension/utils.tsandsdk-ts/src/extractSchema.ts(cross-referenced in comments), to keep the protocol package untouched. Happy to move it intopackages/protocolinstead..default()or.catch()(e.g.z.url().default("")) were previously not resolved and came back as model text. They now resolve like other URL fields, and their descriptions are kept. Both wrappers are covered by a new service-level test.Field(json_schema_extra={"x-stagehand": "element"}), and the Python SDK forwards it unchanged (checked on the serialized schema; a full Pythonextract()call wasn't run). A namedElementhelper could follow if you want parity. The Go SDK wasn't checked.injectUrls,transformUrlStringsToNumericIds) unchanged to limit conflicts.test plan
just fmt,just check$refand wrapper shapes; descriptions on wrappers; the record and recursion errors; unresolved-ID warnings; the conditional system prompt; and URL fields behind.default()/.catch().just test: everything passed except two timing flakes under full parallel load, in suites this change doesn't touch:integrations/corebrowser tests (Chrome shutdown inafterAll) andscripts/release/scoped-release.test.ts. Both pass when run alone.anthropic/claude-sonnet-5-5: extracted a 4-row account list (numbers that only match after normalization), all rows resolved to selectors, and clicking the code-selected match hit the right row.Summary by cubic
Closes #3406.
extract()now returns data and, for fields you mark, the element each value came from, surfaced as anxpath=selector you can pass topage.locator(). Before,extract()andobserve()each returned only half of this.Mark a field with
elementRef()in TypeScript, or"x-stagehand": "element"in JSON Schema from other SDKs. Unresolved IDs become"", matching URL fields, and log a warning.Notes
extract()calls keep the previous prompt.$refs, and optional, nullable, default, catch, and readonly wrappers. Records and recursive schemas throw a clear error before inference..default()or.catch()previously came back as model text; they now resolve like other URL fields.Written for commit 524ec62. Summary will update on new commits.