Skip to content

Feature request: let extract() return the element each value came from #3406

Open

Description

@garrettgruss-realpage

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:

  1. Extract every account number that is shown on the page.
  2. Compare the numbers with deterministic code.
  3. 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.ts takes only combinedTree and combinedUrlMap from the snapshot (around line 99). The XPath map is not used.
  • Only z.string().url() fields are mapped back (transformUrlStringsToNumericIds and then injectUrls, 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. locator and ignoreLocators only 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

  1. observe("each account number entry") to get the selectors.
  2. For each selector, read textContent() with code.
  3. 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/stagehand 3.7.3 in production. We also checked main (4.4.0) and saw the same behavior.
  • Browserbase sessions, Node.js

Activity

  1. martentropp commented on Nov 12, 2026

    @martentropp

    Hi, working on this.

  2. miguelg719 commented on Nov 18, 2026

    @miguelg719
    Collaborator

    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 each ObserveResult entry?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions