Python SDK for Stagehand
Project description
An AI web browsing framework focused on simplicity and extensibility.
Stagehand is the easiest way to build browser automations with AI-powered interactions. It extends the Playwright API with three powerful AI primitives:
- act — Instruct the AI to perform actions (e.g. click a button or scroll).
- extract — Extract and validate data from a page using a JSON schema (generated either manually or via a Pydantic model).
- observe — Get natural language interpretations to, for example, identify selectors or elements from the DOM.
Pydantic Schemas
Stagehand uses Pydantic models to define the options for AI commands:
-
ActOptions
TheActOptionsmodel takes anactionfield that tells the AI what to do on the page, plus optional fields such asuseVisionandvariables:from stagehand.schemas import ActOptions # Example: await page.act(ActOptions(action="click on the 'Quickstart' button"))
-
ObserveOptions
TheObserveOptionsmodel lets you find elements on the page using natural language. TheonlyVisibleoption helps limit the results:from stagehand.schemas import ObserveOptions # Example: await page.observe(ObserveOptions(instruction="find the button labeled 'News'", onlyVisible=True))
-
ExtractOptions
TheExtractOptionsmodel extracts structured data from the page. Pass your instructions and a schema defining your expected data format. Note: If you are using a Pydantic model for the schema, call its.model_json_schema()method to ensure JSON serializability.from stagehand.schemas import ExtractOptions from pydantic import BaseModel class DescriptionSchema(BaseModel): description: str # Example: data = await page.extract( ExtractOptions( instruction="extract the description of the page", schemaDefinition=DescriptionSchema.model_json_schema() ) ) description = data.get("description") if isinstance(data, dict) else data.description
Why?
Stagehand adds determinism to otherwise unpredictable agents.
While there's no limit to what you could instruct Stagehand to do, our primitives allow you to control how much you want to leave to an AI. It works best when your code is a sequence of atomic actions. Instead of writing a single script for a single website, Stagehand allows you to write durable, self-healing, and repeatable web automation workflows that actually work.
[!NOTE]
Stagehandis currently available as an early release, and we're actively seeking feedback from the community. Please join our Slack community to stay updated on the latest developments and provide feedback.
Installation
Install the Python package via pip:
pip install stagehand-py
Environment Variables
Before running your script, set the following environment variables:
export BROWSERBASE_API_KEY="your-api-key"
export BROWSERBASE_PROJECT_ID="your-project-id"
export OPENAI_API_KEY="your-openai-api-key" # or your preferred model's API key
export STAGEHAND_SERVER_URL="url-of-stagehand-server"
Quickstart
Below is a minimal example to get started with Stagehand using the new schema-based options:
import asyncio
import os
from stagehand.client import Stagehand
from stagehand.schemas import ActOptions, ExtractOptions
from pydantic import BaseModel
from dotenv import load_dotenv
load_dotenv()
class DescriptionSchema(BaseModel):
description: str
async def main():
# Create a Stagehand client - it will automatically create a new session if needed
stagehand = Stagehand(
model_name="gpt-4o", # Optional: defaults are available from the server
)
# Initialize Stagehand and create a new session
await stagehand.init()
print(f"Created new session: {stagehand.session_id}")
# Navigate to a webpage using local Playwright controls
await stagehand.page.goto("https://www.example.com")
print("Navigation complete.")
# Perform an action using the AI (e.g. simulate a button click)
await stagehand.page.act(ActOptions(action="click on the 'Quickstart' button"))
# Extract data from the page with schema validation
data = await stagehand.page.extract(
ExtractOptions(
instruction="extract the description of the page",
schemaDefinition=DescriptionSchema.model_json_schema()
)
)
description = data.get("description") if isinstance(data, dict) else data.description
print("Extracted description:", description)
await stagehand.close()
if __name__ == "__main__":
asyncio.run(main())
Running Evaluations
To test all evaluations, run the following command in your terminal:
python evals/run_all_evals.py
This script will dynamically discover and execute every evaluation module within the evals directory and print the results for each.
More Examples
For further examples, check out the scripts in the examples/ directory:
- examples/example.py: Demonstrates combined server-side/page navigation with AI-based actions.
- examples/extract-example.py: Shows how to use the extract functionality with a JSON schema or a Pydantic model.
- examples/observe-example.py: Demonstrates the observe functionality to get natural-language readings of the page.
Configuration
Stagehand can be configured via environment variables or through a StagehandConfig object. Available configuration options include:
stagehand_server_url: URL of the Stagehand API server.browserbase_api_key: Your Browserbase API key (BROWSERBASE_API_KEY).browserbase_project_id: Your Browserbase project ID (BROWSERBASE_PROJECT_ID).model_api_key: Your model API key (e.g. OpenAI, Anthropic, etc.) (MODEL_API_KEY).verbose: Verbosity level (default: 1).model_name: Optional model name for the AI.dom_settle_timeout_ms: Additional time (in ms) to have the DOM settle.debug_dom: Enable debug mode for DOM operations.
Example using a unified configuration:
from stagehand.config import StagehandConfig
import os
config = StagehandConfig(
env="BROWSERBASE" if os.getenv("BROWSERBASE_API_KEY") and os.getenv("BROWSERBASE_PROJECT_ID") else "LOCAL",
api_key=os.getenv("BROWSERBASE_API_KEY"),
project_id=os.getenv("BROWSERBASE_PROJECT_ID"),
debug_dom=True,
headless=False,
dom_settle_timeout_ms=3000,
model_name="gpt-4o-mini",
model_client_options={"apiKey": os.getenv("MODEL_API_KEY")}
)
Features
- AI-powered Browser Control: Execute natural language instructions over a running browser.
- Validated Data Extraction: Use JSON schemas (or Pydantic models) to extract and validate information from pages.
- Async/Await Support: Built using Python's asyncio, making it easy to build scalable web automation workflows.
- Extensible: Seamlessly extend Playwright functionality with AI enrichments.
Requirements
- Python 3.7+
- httpx
- asyncio
- pydantic
- python-dotenv (optional, for .env support)
- playwright
License
MIT License (c) 2025 Browserbase, Inc.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stagehand_py-0.2.1.tar.gz.
File metadata
- Download URL: stagehand_py-0.2.1.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8e23ba8d78a40e3628ebe2ee1ea267041de01dda15b734b9452f765c999b1eb
|
|
| MD5 |
2b8e2ba8a8ac2719aaaff773b1e1ff93
|
|
| BLAKE2b-256 |
8d16e9b36737e348b4e0560abcead44fb05c289f396119446e9ae8489dd142b1
|
File details
Details for the file stagehand_py-0.2.1-py3-none-any.whl.
File metadata
- Download URL: stagehand_py-0.2.1-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97c33463ce2f9fe9dead5e35b3fe7827ae2563e0ab65ffa41baeb14af4ddece2
|
|
| MD5 |
3763e2cf4ff72fa94f4d5e74c29e3ceb
|
|
| BLAKE2b-256 |
5d7bc4cf95ece5af961e53ed2c620d42ab4bbf482e534ad3e828dc065f116309
|