Extract structured data from pages using AI-powered analysis.
extractFromPage() combines a screenshot with DOM content and sends both to an LLM to extract structured data matching a Zod schema. Because the visual analysis happens in a separate LLM call, it keeps your coding agent’s context window free of raw HTML.
extractFromPage()
How it works
- With
selector: waits for the element to be visible, takes a screenshot of just that element, and reads itsinnerHTML(truncated at 30 000 characters). - Without
selector: takes a full-page screenshot via CDP and reads the full page HTML (truncated at 50 000 characters).
ExtractOptions<T>
The Playwright
Page to extract from.A natural-language description of what to extract. For example:
"Extract the first 5 search results".A Zod schema that describes the shape of the data you want back. The return type of
extractFromPage is z.infer<T>.A Vercel AI SDK
LanguageModel instance. Pass the result of calling a
provider factory such as openai("gpt-4o") from @ai-sdk/openai. The backing
model must support vision (multimodal) input because extractFromPage always
sends a screenshot.Optional logger. Defaults to a simple
console-based logger when omitted.Optional CSS selector. When provided, the screenshot and DOM capture are
scoped to the matching element instead of the full page. Useful for large
pages where you only care about one section.
Example
Supported providers
Any provider that implements the Vercel AI SDKLanguageModel interface works with extractFromPage and the other LLM-powered functions. The most common ones:
| Package | Example |
|---|---|
@ai-sdk/openai | openai("gpt-4o") |
@ai-sdk/anthropic | anthropic("claude-3-5-sonnet-20241022") |
@ai-sdk/google | google("gemini-1.5-pro") |
@ai-sdk/google-vertex | vertex("gemini-1.5-pro") |