TypeSpec is used as an argument type for extraction-based methods on pages. It allows specifying the type of data to return after extraction.

TypeSpec supports:

  • Pydantic models (for data validation and serialization)
  • JSON Schema (for defining data structure and validation)
  • Primitive types (int, float, bool, str)

Usage example

from pydantic import BaseModel
from typing import Optional
from datetime import datetime

class Article(BaseModel):
    title: str
    content: str
    author: str
    published_at: Optional[datetime] = None
    views: Optional[int] = 0

article = browser.extract("The article information", type_spec=Article)
print(article.title)