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>.An LLM client instance. Create one with
createLLMClientFromModel(). 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
createLLMClientFromModel()
Creates a Libretto LLMClient from a Vercel AI SDK LanguageModel. This is the standard way to supply an LLM client to extractFromPage, attemptWithRecovery, and detectSubmissionError.
A Vercel AI SDK language model instance. Pass the result of calling a provider factory such as
openai("gpt-4o"), anthropic("claude-3-5-sonnet-20241022"), or google("gemini-1.5-pro").Supported providers
Any provider that implements the Vercel AI SDKLanguageModel interface works. 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") |
Example
LLMClient interface
You can also implement LLMClient directly if you want to use a provider that is not in the Vercel AI SDK:
Implementations should throw on failure rather than returning sentinel values like
null or undefined. Libretto relies on thrown exceptions to trigger retry and recovery logic.