Skip to main content

Python SDK for Stagehand

Project description

An AI web browsing framework focused on simplicity and extensibility.

PyPI version MIT License Slack Community

NOTE: This is a Python SDK for Stagehand. Original implementation is in TypeScript and is available here.

A Python SDK for Stagehand, enabling automated browser control and data extraction.

Stagehand is the easiest way to build browser automations. It is fully compatible with Playwright, offering three simple AI APIs (act, extract, and observe) on top of the base Playwright Page class that provide the building blocks for web automation via natural language.

You can write all of your Playwright commands as you normally would, while offloading the AI-powered act/extract/observe operations to Stagehand hosted on our Stagehand API.

Here's a sample of what you can do with Stagehand:

import asyncio

async def main():
    # Keep your existing Playwright code unchanged
    await page.goto("https://docs.stagehand.dev");

    # Stagehand AI: Act on the page via Stagehand API
    await page.act("click on the 'Quickstart'");

    # Stagehand AI: Extract data from the page
    from pydantic import BaseModel

    class DescriptionSchema(BaseModel):
        description: str

    data = await page.extract(
        instruction="extract the description of the page",
        schema=DescriptionSchema
    )
    description = data.description

if __name__ == "__main__":
    asyncio.run(main())

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] Stagehand is 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

pip install stagehand-py

Quickstart

Before running your script, make sure you have exported the necessary environment variables:

export BROWSERBASE_API_KEY="your-api-key"
export BROWSERBASE_PROJECT_ID="your-project-id"
export OPENAI_API_KEY="your-openai-api-key"
export STAGEHAND_SERVER_URL="url-of-stagehand-server" 

Usage

Here is a minimal example to get started:

import asyncio
import os
from stagehand.client import Stagehand
from dotenv import load_dotenv

load_dotenv()

async def main():
    # Create a Stagehand client - it will create a new session automatically
    stagehand = Stagehand(
        model_name="gpt-4o",  # optional - defaults to server's default
    )

    # Initialize - this will create a new session
    await stagehand.page.init()
    print(f"Created new session: {stagehand.session_id}")

    # Example: navigate to google.com - from Playwright in Python
    await stagehand.page.goto("https://www.google.com")
    print("Navigation complete.")

    # Example: ACT to do something like 'search for openai'
    # executes remote on a Typescript server and logs are streamed back
    await stagehand.page.act("search for openai")

    # Pure client side Playwright - after searching for OpenAI, click on the News tab
    await stagehand.page.get_by_role("link", name="News", exact=True).first.click()
    print("Clicked on News tab")

    # Close the session (if needed)
    await stagehand.close()

if __name__ == "__main__":
    asyncio.run(main())

More Examples

For further examples, you can check out the scripts in the “examples/” directory:

  1. “examples/example.py”: Demonstrates combined server-side/page navigation and AI-based actions.
  2. “examples/extract-example.py”: Shows how to use the “extract” functionality with JSON schema or a pydantic model.
  3. “examples/observe-example.py”: Demonstrates the “observe” functionality to get natural-language readings of the page.

Configuration

  • stagehand_server_url: The Stagehand API server URL
  • browserbase_api_key: Your BrowserBase API key (can also be set via BROWSERBASE_API_KEY environment variable)
  • browserbase_project_id: Your BrowserBase project ID (can also be set via BROWSERBASE_PROJECT_ID environment variable)
  • openai_api_key: Your OpenAI API key (can also be set via OPENAI_API_KEY environment variable)
  • verbose: Verbosity level (default: 1)
  • model_name: (optional) Model name to use for the conversation
  • dom_settle_timeout_ms: (optional) Additional time for the DOM to settle
  • debug_dom: (optional) Whether or not to enable DOM debug mode

Features

  • Automated browser control with natural language commands
  • Data extraction with schema validation (either pydantic or JSON schema)
  • Async/await support
  • Extension of Playwright - run playwright commands normally, with act/extract/observe offloaded to an API

Requirements

  • Python 3.7+
  • httpx
  • asyncio
  • pydantic
  • python-dotenv (optional if using a .env file)

License

MIT License (c) Browserbase, Inc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stagehand_py-0.1.8.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stagehand_py-0.1.8-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file stagehand_py-0.1.8.tar.gz.

File metadata

  • Download URL: stagehand_py-0.1.8.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for stagehand_py-0.1.8.tar.gz
Algorithm Hash digest
SHA256 816a7f3a7ff77559a8c6401b1abe75d3de365b8c0ef058bd2c983427cdfd67bf
MD5 d3c0b2fa4c6b1b898657b735b33d2ece
BLAKE2b-256 e508fda64b9f5e4b865015e91d9c36e1c2eefbd10fe073276518b393e0e0299b

See more details on using hashes here.

File details

Details for the file stagehand_py-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: stagehand_py-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for stagehand_py-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 b7e1ecbf310670e352a46033a17cac9e21fbc96351fd5d2fc6e0bd6569f12d30
MD5 7feedc9f3de2fbdc1fc15467a81c653f
BLAKE2b-256 b315faf1e5765647e36c46ba71c9fb14ace7913f1325137a5c18bc1a68394b5c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page