Skip to content

extract() can return element IDs instead of URLs in plain string fields #3234

Open

Description

@vishalanton16

Description

Extracting links into plain z.string() fields can return internal accessibility-tree element IDs instead of destination URLs. The extraction succeeds because these IDs are valid strings.

Reproduction

With an initialized TypeScript Stagehand instance:

import { z } from "zod/v4";

await page.goto("https://news.ycombinator.com/");

const result = await stagehand.extract(
  "Extract the first story title and its destination URL",
  z.object({ title: z.string(), url: z.string() }),
  { page },
);

console.log(result.data);

Also try:

const result = await stagehand.extract(
  "Extract the first 5 story destination URLs, separated by newlines",
  z.object({ stories: z.string() }),
  { page },
);

console.log(result.data);

The failure depends on the model returning element IDs. Exact IDs vary with the snapshot.

Actual behavior

Observed URL fields contained values such as "0-74" and "0-112". A multiline string contained:

{
  "stories": "[0-74]\n[0-112]\n[0-150]\n[0-188]\n[0-226]"
}

Expected behavior

Requested destination URLs should resolve to the links' actual hrefs, including when the schema uses plain strings.

URL-validated fields such as z.url() should continue working. Unrelated text resembling an ID, such as a score of "3-2", should remain unchanged.

Technical details

The extraction prompt requests link element IDs, but the extraction service resolves them through the snapshot URL map only for schema paths recognized as URL fields. Plain string fields bypass that resolution.

Activity

  1. RachitaPant commented on Oct 22, 2026

    @RachitaPant

    Hi, I'd like to take this one.

    Root cause: transformSchema (packages/extension/utils.ts) only maps schema fields that carry a URL check (z.url()) to element IDs and records their paths. The extract prompt tells the model to answer link requests with element IDs for every schema. A plain z.string() therefore gets the ID instruction but is never resolved through combinedUrlMap, so the ID comes back as the value. I reproduced this against extractService.extract() with a mocked LLM and a fake snapshot.

    Plan:

    1. Add a post-processing step in the extract service. It resolves string values that are exactly an element ID (0-74 or [0-74], including newline- or comma-separated lists) through combinedUrlMap. Strings that aren't in the map, such as a score like 3-2, stay unchanged.
    2. Leave z.url() handling as it is.
    3. Add regression tests with mocked LLM responses in packages/extension/tests/extract.test.ts, plus a patch changeset for the extension and the SDKs that bundle it.

    Happy to adjust the approach if you'd prefer the fix elsewhere.

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