Skip to main content

What is extract()?

extract() grabs structured data from a webpage. Every call takes an instruction and an output shape: Zod in TypeScript, Pydantic in Python, and a Go type parameter whose JSON Schema Stagehand derives automatically. Stagehand validates the result against that shape before returning it, so what you get back is already typed.

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

extract() returns a result with two fields: data holds the extracted value, and metadata carries the action ID and server-side cache status. The shape of data follows the schema or Go type you supplied:
When extracting with an object schema, the return type is inferred from that schema:
Example result:

Advanced configuration

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

Server-side caching

cache requires a Browserbase browser and a Browserbase API key. It has no effect on local browsers.
When running on Browserbase, Stagehand can cache extract() results server-side. Repeated calls with the same inputs return instantly without consuming LLM tokens. Enable caching on Stagehand.create() and override it per call: Locator-scoped extractions, including calls with locator or ignoreLocators, bypass the server-side cache and report metadata.cache.status as DISABLED.

Targeted extract

Pass a page locator 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 ignored locators.
ignoreLocators remove each resolved locator target and its descendants from the snapshot. A locator without nth removes all matching targets; a locator with nth removes only that indexed match. The locator option scopes extraction to one resolved subtree, using nth when present. Scoped extract currently supports CSS and XPath locators. text= locators are supported by locator methods, but not yet by extract snapshot scoping.

Visual extract

Turn the screenshot option on when the extraction needs visual information from the current viewport in addition to the page accessibility tree.
The screenshot option captures the current viewport, not the full page. Visual extractions always bypass the server-side cache, because a cache key is built from DOM state and cannot represent the pixels the model saw.

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 a URL type.
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 to URLs. When logging the LLM trace, you should expect to see IDs. The actual URLs will be included in the final result.

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 observe() first to confirm the data is present on the page
  • Wait for dynamic content: If the page loads content dynamically, wait for it before extracting
Solution: Wait for content before extracting
Problem: Getting schema validation errors or type mismatchesSolutions:
  • Use optional fields: Make fields optional if the data might not always be present
  • Use flexible types: Consider using a string instead of a number for prices that might include currency symbols
  • Add descriptions: Describe each field to help the model understand its 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 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 the timeout option for complex extractions
Solution: Break down large extractions

Next steps

Act

Execute actions efficiently

Observe

Analyze pages and preview actions