Repository navigation
Description
Feature request: let extract() return the element each value came from
Summary
extract() returns data, and observe() returns elements. No single call returns both: "these values, and the element each one came from". We propose a schema helper that resolves to an element selector, in the same way that z.string().url() resolves to an href today.
Use case
Our agent logs in to a portal and must select one account from a list. It has a target account number. The list shows entries such as 0554063503-3 or 205354027-13.
We do not want the LLM to compare the account numbers. In production it sometimes misses a match that is exact after normalization (removing hyphens and leading zeros). We want to:
- Extract every account number that is shown on the page.
- Compare the numbers with deterministic code.
- Click the entry that matches.
Step 3 needs the element of the value that matched. extract() does not give it.
Current behavior (3.7.3, and main at ce298c6, sdk-ts 4.4.0)
packages/extension/services/extractService.tstakes onlycombinedTreeandcombinedUrlMapfrom the snapshot (around line 99). The XPath map is not used.- Only
z.string().url()fields are mapped back (transformUrlStringsToNumericIdsand theninjectUrls, around lines 131 and 167-174), and they resolve to a URL, not to the element. - The result is
{ data, metadata: { usage, cache } }, with no element references.locatorandignoreLocatorsonly scope the input. - A schema field such as
elementId: z.string()gets only an ID that the model copied, and the public API cannot resolve that ID.
Related: #3234 and #3363 fix element IDs that leak into plain string fields. This request is the opposite: return the element of each value on purpose, in a typed field.
Proposal
Add a schema marker that the handler resolves to a selector, like the .url() path does:
import { z } from "zod";
import { elementRef } from "@browserbasehq/stagehand";
const { accounts } = await stagehand.extract(
"every account number shown in the account list",
z.object({
accounts: z.array(
z.object({
accountNumber: z.string(),
element: elementRef(), // resolves to "xpath=/html[1]/body[1]/..."
}),
),
}),
);
const hit = accounts.find((a) => normalize(a.accountNumber) === normalize(target));
if (hit) await stagehand.act({ selector: hit.element, method: "click", description: "select account" });Implementation idea: in transformSchema, treat elementRef() fields like .url() fields (send the model a numeric or encoded ID). Then, after inference, resolve them with the snapshot's XPath map instead of combinedUrlMap.
Current workaround
observe("each account number entry")to get the selectors.- For each selector, read
textContent()with code. - Match with code, then call
act({ selector, method: "click" }).
This works, but it needs one more step, and it depends on observe finding the same set of entries that extract would find.
Environment
@browserbasehq/stagehand3.7.3 in production. We also checkedmain(4.4.0) and saw the same behavior.- Browserbase sessions, Node.js
Activity
martentropp commentedon Nov 12, 2026
Hi, working on this.
miguelg719 commentedon Nov 18, 2026
Hi @garrettgruss-realpage, thanks for raising the issue.
Curious, how would this remove the extra step on your proposed approach? Extract would also return an LLM-generated account number (
z.string()), so I would expect the behavior to match observe's. Have you tried to ask in the observe prompt to provide the given account number in the arguments of eachObserveResultentry?