Skip to main content
This function uses AI and incurs costs.
This function has multiple overloads
Extracts structured data from web pages using AI-powered content analysis.This function provides intelligent data extraction from web pages using various strategies including HTML parsing, image analysis, and Markdown conversion. Or by using Text or Image Content. It supports extraction from entire pages or specific elements, with built-in caching and retry mechanisms.
export declare function extractStructuredData(options: {
  source: Page | Locator;
  dataSchema: JsonSchema | z.ZodSchema;
  prompt?: string;
  strategy?: "IMAGE" | "MARKDOWN" | "HTML";
  enableDomMatching?: boolean;
  enableCache?: boolean;
  maxRetries?: number;
  model?: string;
  apiKey?: string;
}): Promise<any>;
Extract data from web pages or specific elements using HTML, IMAGE, or MARKDOWN strategies with DOM matching support.

Features and limitations

Features:
  • Smart caching: Hashes inputs and uses KV Cache for persistent storage
  • DOM matching: With enableDomMatching=true, values match DOM elements for smart caching
  • Multiple strategies: HTML, IMAGE, or MARKDOWN based on content type
  • Flexible models: Use any up-to-date model from Anthropic, OpenAI, or Google based on your needs
Limitations:
  • Model variability: Quality varies by model—experiment to find the best fit
  • DOM complexity: Dynamic structures can affect caching and matching
  • IMAGE strategy constraints: Can’t capture truncated or off-screen content
  • Schema design: Complex schemas may reduce accuracy

Examples

import { extractStructuredData } from "@intuned/browser/ai";
import { BrowserContext, Page } from "playwright";

interface Params {}

export default async function handler(
  params: Params,
  page: Page,
  context: BrowserContext
) {
  await page.goto(
    "https://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html"
  );
  // This will extract the book details from the page, using the HTML strategy with the gpt-4o model.
  // The dataSchema is a JSON Schema object that defines the structure of the data to extract.
  // You can also use a Zod schema instead of a JSON Schema object.
  const book = await extractStructuredData({
    source: page,
    strategy: "HTML", // The HTML strategy is the default strategy and will be used if no strategy is provided.
    model: "gpt-4o",
    dataSchema: {
      type: "object",
      properties: {
        name: { type: "string" },
        price: { type: "string" },
        description: { type: "string" },
        inStock: { type: "boolean" },
        rating: { type: "string" },
      },
      required: ["name", "price"],
    },
    prompt: "Extract book details from this page",
    enableCache: true, // since this is true, the method will call AI for the first time, and then whenever you call this method it will return cached results as long as the DOM is the same.
    enableDomMatching: true, // since this is true, the method will return the results mapped to the DOM elements, you MUST enable cache for this to work.
    maxRetries: 3,
  });

  console.log(`Found book: ${book.name} - ${book.price}`);
}

Arguments

options
Object
required
Configuration object containing extraction parameters

Returns: Promise<any>

The extracted structured data conforming to the provided schema.