Skip to main content

What is extract()?

extract() grabs structured data from a webpage. You can define your schema with Zod (TypeScript) or JSON. If you don’t want to define a schema, you can also call extract with just a natural language prompt, or call extract with no parameters.

Why use extract()?

Structured

Turn messy webpage data into clean objects that follow a schema.

Resilient

Build resilient extractions that don’t break when the website changes

Return value

When you use extract(), Stagehand will return a Promise<ExtractResult> with the following structure:
When extracting with a schema, the return type is inferred from your Zod schema:
Example result:

Advanced Configuration

You can pass additional options to configure the model, timeout, selector scope, and whether to include a screenshot:

Server-side Caching

serverCache only works when running with env: "BROWSERBASE". It has no effect in local environments.
When running on Browserbase, Stagehand automatically caches extract() results server-side. Repeated calls with the same inputs return instantly without consuming LLM tokens. Caching is enabled by default and can be controlled globally on the constructor or overridden per call:
For how the cache works, tuning the validation threshold, and best practices, see the Server Caching guide.

Targeted Extract

Pass a selector to extract to target a specific element on the page.
This helps reduce the context passed to the LLM, optimizing token usage/speed and improving accuracy.
You can also exclude specific nodes (including their descendant nodes) with ignoreSelectors.
ignoreSelectors removes all matches for each selector, along with each matched node’s descendants. selector still scopes extraction to a single resolved subtree.

Visual Extract

Set screenshot: true when the extraction needs visual information from the current viewport in addition to the page accessibility tree.
screenshot: true captures the current viewport, not the full page. It is only supported with AI SDK clients.

Best practices

Extract with Context

You can provide additional context to your schema to help the model extract the data more accurately.
To extract links or URLs, define the relevant field as z.string().url().
Here is how an extract call might look for extracting a link or URL. This also works for image links.
Inside Stagehand, extracting links works by asking the LLM to select an ID. Stagehand looks up that ID in a mapping of IDs -> URLs. When logging the LLM trace, you should expect to see IDs. The actual URLs will be included in the final ExtractResult.

Troubleshooting

Problem: extract() returns empty or incomplete dataSolutions:
  • Check your instruction clarity: Make sure your instruction is specific and describes exactly what data you want to extract
  • Verify the data exists: Use stagehand.observe() first to confirm the data is present on the page
  • Wait for dynamic content: If the page loads content dynamically, use stagehand.act("wait for the content to load") before extracting
Solution: Wait for content before extracting
Problem: Getting schema validation errors or type mismatchesSolutions:
  • Use optional fields: Make fields optional with z.optional() if the data might not always be present
  • Use flexible types: Consider using z.string() instead of z.number() for prices that might include currency symbols
  • Add descriptions: Use .describe() to help the model understand field requirements
Solution: More flexible schema
Problem: Extraction results vary between runsSolutions:
  • Be more specific in instructions: Instead of “extract prices”, use “extract the numerical price value for each item”
  • Use context in schema descriptions: Add field descriptions to guide the model
  • Combine with observe: Use stagehand.observe() to understand the page structure first
Solution: Validate with observe first
Problem: Extraction is slow or timing outSolutions:
  • Reduce scope: Extract smaller chunks of data in multiple calls rather than everything at once
  • Use targeted instructions: Be specific about which part of the page to focus on
  • Consider pagination: For large datasets, extract one page at a time
  • Increase timeout: Use timeoutMs parameter for complex extractions
Solution: Break down large extractions

Next steps

Act

Execute actions efficiently

Observe

Analyze pages and preview actions