Skip to main content

The official Python library for the stagehand API

Project description

Stagehand Python API Library

PyPI version

The Stagehand Python SDK provides convenient access to the Stagehand REST API from applications written in Python.

It is generated with Stainless.

Installation

pip install stagehand-alpha

For local development or when working from this repository, sync the dependency lockfile with uv (see the Local development section below) before running project scripts.

Requirements

Python 3.9 or higher.

Running the Example

A complete working example is available at examples/full_example.py.

To run it, first export the required environment variables, then use Python:

export BROWSERBASE_API_KEY="your-bb-api-key"
export BROWSERBASE_PROJECT_ID="your-bb-project-uuid"
export MODEL_API_KEY="sk-proj-your-llm-api-key"

uv run python examples/full_example.py

Local mode example

If you want to run Stagehand locally, use the local example (examples/local_example.py). It shows how to configure the client for server="local".

Local mode runs Stagehand’s embedded server and launches a local Chrome/Chromium browser (it is not bundled with the Python wheel), so you must have Chrome installed on the machine running the example.

If Chrome is installed but Stagehand can’t find it, set CHROME_PATH to your browser executable (or pass browser.launchOptions.executablePath when starting the session).

Common Windows paths:

  • C:\Program Files\Google\Chrome\Application\chrome.exe
  • C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

PowerShell:

# optional if you don't already have Chrome installed
winget install -e --id Google.Chrome

# optional if Stagehand can't auto-detect Chrome
$env:CHROME_PATH="C:\Program Files\Google\Chrome\Application\chrome.exe"

uv run python examples/local_example.py
pip install stagehand-alpha
uv run python examples/local_example.py

Streaming logging example

See examples/logging_example.py for a remote-only flow that streams StreamEvents with verbose=2, stream_response=True, and x_stream_response="true" so you can watch the SDK’s logs as they arrive.

uv run python examples/logging_example.py
Local development

This repository relies on uv to install the sanctioned Python version and dependencies. After cloning, bootstrap the environment with:

./scripts/bootstrap

Once the environment is ready, execute repo scripts with uv run:

uv run python examples/full_example.py
uv run python scripts/download-binary.py
uv run --isolated --all-extras pytest

Usage

This example demonstrates the full Stagehand workflow: starting a session, navigating to a page, observing possible actions, acting on elements, extracting data, and running an autonomous agent.

import asyncio

from stagehand import AsyncStagehand


async def main() -> None:
    # Create client using environment variables:
    # BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY
    client = AsyncStagehand()

    # Start a new browser session (returns a session helper bound to a session_id)
    session = await client.sessions.create(model_name="openai/gpt-5-nano")

    print(f"Session started: {session.id}")

    try:
        # Navigate to a webpage
        await session.navigate(
            url="https://news.ycombinator.com",
        )
        print("Navigated to Hacker News")

        # Observe to find possible actions on the page
        observe_response = await session.observe(
            instruction="find the link to view comments for the top post",
        )

        results = observe_response.data.result
        print(f"Found {len(results)} possible actions")
        if not results:
            return

        # Take the first action returned by Observe and pass it to Act
        action = results[0].to_dict(exclude_none=True)
        print("Acting on:", action.get("description"))

        act_response = await session.act(input=action)
        print("Act completed:", act_response.data.result.message)

        # Extract structured data from the page using a JSON schema
        extract_response = await session.extract(
            instruction="extract the text of the top comment on this page",
            schema={
                "type": "object",
                "properties": {
                    "commentText": {"type": "string"},
                    "author": {"type": "string"},
                },
                "required": ["commentText"],
            },
        )

        extracted = extract_response.data.result
        author = extracted.get("author", "unknown") if isinstance(extracted, dict) else "unknown"
        print("Extracted author:", author)

        # Run an autonomous agent to accomplish a complex task
        execute_response = await session.execute(
            execute_options={
                "instruction": f"Find any personal website, GitHub, or LinkedIn profile for the Hacker News user '{author}'.",
                "max_steps": 10,
            },
            agent_config={"model": "openai/gpt-5-nano"},
            timeout=300.0,
        )

        print("Agent completed:", execute_response.data.result.message)
        print("Agent success:", execute_response.data.result.success)
    finally:
        # End the browser session to clean up resources
        await session.end()
        print("Session ended")


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

Client configuration

Configure the client using environment variables:

from stagehand import AsyncStagehand

client = AsyncStagehand()

Or manually:

from stagehand import AsyncStagehand

client = AsyncStagehand(
    browserbase_api_key="My Browserbase API Key",
    browserbase_project_id="My Browserbase Project ID",
    model_api_key="My Model API Key",
)

Or using a combination of the two approaches:

from stagehand import AsyncStagehand

client = AsyncStagehand(
    # Configures using environment variables
    browserbase_api_key="My Browserbase API Key",  # override just this one
)

See this table for the available options:

Keyword argument Environment variable Required Default value
browserbase_api_key BROWSERBASE_API_KEY true -
browserbase_project_id BROWSERBASE_PROJECT_ID true -
model_api_key MODEL_API_KEY true -
base_url STAGEHAND_BASE_URL false "https://api.stagehand.browserbase.com"

Keyword arguments take precedence over environment variables.

[!TIP] Don't create more than one client in the same application. Each client has a connection pool, which is more efficient to share between requests.

Modifying configuration

To temporarily use a modified client configuration while reusing the same connection pool, call with_options() on any client:

client_with_options = client.with_options(model_api_key="sk-your-llm-api-key-here", max_retries=42)

The with_options() method does not affect the original client.

Requests and responses

To send a request to the Stagehand API, call the corresponding client method using keyword arguments.

Nested request parameters are dictionaries typed using TypedDict. Responses are Pydantic models which also provide helper methods like:

  • Serializing back into JSON: model.to_json()
  • Converting to a dictionary: model.to_dict()

Immutability

Response objects are Pydantic models. If you want to build a modified copy, prefer model.model_copy(update={...}) (Pydantic v2) rather than mutating in place.

Asynchronous execution

This SDK recommends using AsyncStagehand and awaiting each API call:

import asyncio
from stagehand import AsyncStagehand


async def main() -> None:
    client = AsyncStagehand()
    session = await client.sessions.create(model_name="openai/gpt-5-nano")
    response = await session.act(input="click the first link on the page")
    print(response.data)


asyncio.run(main())

With aiohttp

By default, the async client uses httpx for HTTP requests. For improved concurrency performance you may also use aiohttp as the HTTP backend.

Install aiohttp:

uv run pip install stagehand-alpha[aiohttp]

Then instantiate the client with http_client=DefaultAioHttpClient():

import asyncio
from stagehand import AsyncStagehand, DefaultAioHttpClient


async def main() -> None:
    async with AsyncStagehand(http_client=DefaultAioHttpClient()) as client:
        session = await client.sessions.create(model_name="openai/gpt-5-nano")
        response = await session.act(input="click the first link on the page")
        print(response.data)


asyncio.run(main())

Streaming responses

We provide support for streaming responses using Server-Sent Events (SSE).

To enable SSE streaming, you must:

  1. Ask the server to stream by setting x_stream_response="true" (header), and
  2. Tell the client to parse an SSE stream by setting stream_response=True.
import asyncio

from stagehand import AsyncStagehand


async def main() -> None:
    async with AsyncStagehand() as client:
        session = await client.sessions.create(model_name="openai/gpt-5-nano")

        stream = await client.sessions.act(
            id=session.id,
            input="click the first link on the page",
            stream_response=True,
            x_stream_response="true",
        )
        async for event in stream:
            # event is a StreamEvent (type: "system" | "log")
            print(event.type, event.data)


asyncio.run(main())

Raw responses

The SDK defines methods that deserialize responses into Pydantic models. However, these methods don't provide access to response headers, status code, or the raw response body.

To access this data, prefix any HTTP method call on a client or service with with_raw_response:

import asyncio

from stagehand import AsyncStagehand


async def main() -> None:
    async with AsyncStagehand() as client:
        response = await client.sessions.with_raw_response.start(model_name="openai/gpt-5-nano")
        print(response.headers.get("X-My-Header"))

        session = response.parse()  # get the object that `sessions.start()` would have returned
        print(session.data)


asyncio.run(main())

.with_streaming_response

The with_raw_response interface eagerly reads the full response body when you make the request.

To stream the response body (not SSE), use with_streaming_response instead. It requires a context manager and only reads the response body once you call .read(), .text(), .json(), .iter_bytes(), .iter_text(), .iter_lines() or .parse().

import asyncio

from stagehand import AsyncStagehand


async def main() -> None:
    async with AsyncStagehand() as client:
        async with client.sessions.with_streaming_response.start(model_name="openai/gpt-5-nano") as response:
            print(response.headers.get("X-My-Header"))
            async for line in response.iter_lines():
                print(line)


asyncio.run(main())

Error handling

When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of stagehand.APIConnectionError is raised.

When the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of stagehand.APIStatusError is raised, containing status_code and response properties.

All errors inherit from stagehand.APIError.

import asyncio

import stagehand
from stagehand import AsyncStagehand


async def main() -> None:
    async with AsyncStagehand() as client:
        try:
            await client.sessions.start(model_name="openai/gpt-5-nano")
        except stagehand.APIConnectionError as e:
            print("The server could not be reached")
            print(e.__cause__)  # an underlying Exception, likely raised within httpx.
        except stagehand.RateLimitError:
            print("A 429 status code was received; we should back off a bit.")
        except stagehand.APIStatusError as e:
            print("A non-200-range status code was received")
            print(e.status_code)
            print(e.response)


asyncio.run(main())

Error codes are as follows:

Status Code Error Type
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
422 UnprocessableEntityError
429 RateLimitError
>=500 InternalServerError
N/A APIConnectionError

Retries

Certain errors are automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors are all retried by default.

You can use the max_retries option to configure or disable retry settings:

import asyncio

from stagehand import AsyncStagehand


async def main() -> None:
    async with AsyncStagehand(max_retries=0) as client:
        # Or, configure per-request:
        await client.with_options(max_retries=5).sessions.start(model_name="openai/gpt-5-nano")


asyncio.run(main())

Timeouts

By default requests time out after 1 minute. You can configure this with a timeout option, which accepts a float or an httpx.Timeout object.

On timeout, an APITimeoutError is thrown. Note that requests that time out are retried twice by default.

Logging

The SDK uses the standard library logging module.

Enable logging by setting the STAGEHAND_LOG environment variable to info:

export STAGEHAND_LOG=info

Or to debug for more verbose logging:

export STAGEHAND_LOG=debug

Undocumented API functionality

This library is typed for convenient access to the documented API, but you can still access undocumented endpoints, request params, or response properties when needed.

Undocumented endpoints

To make requests to undocumented endpoints, use client.get, client.post, and other HTTP verbs. Client options (such as retries) are respected.

import httpx
from stagehand import AsyncStagehand

import asyncio


async def main() -> None:
    async with AsyncStagehand() as client:
        response = await client.post("/foo", cast_to=httpx.Response, body={"my_param": True})
        print(response.headers.get("x-foo"))


asyncio.run(main())

Undocumented request params

To send extra params that aren't available as keyword args, use extra_query, extra_body, and extra_headers.

Undocumented response properties

To access undocumented response properties, you can access extra fields like response.unknown_prop. You can also get all extra fields as a dict with response.model_extra.

Response validation

In rare cases, the API may return a response that doesn't match the expected type.

By default, the SDK is permissive and will only raise an error if you later try to use the invalid data.

If you would prefer to validate responses upfront, instantiate the client with _strict_response_validation=True. An APIResponseValidationError will be raised if the API responds with invalid data for the expected schema.

import asyncio

from stagehand import APIResponseValidationError, AsyncStagehand

try:
    async def main() -> None:
        async with AsyncStagehand(_strict_response_validation=True) as client:
            await client.sessions.start(model_name="openai/gpt-5-nano")

    asyncio.run(main())
except APIResponseValidationError as e:
    print("Response failed schema validation:", e)

FAQ

Why are some values typed as Literal[...] instead of Python Enums?

Using Literal[...] types is forwards compatible: the API can introduce new enum values without breaking older SDKs at runtime.

How can I tell whether None means null or “missing” in a response?

In an API response, a field may be explicitly null, or missing entirely; in either case its value is None in this library. You can differentiate the two cases with .model_fields_set:

if response.my_field is None:
    if "my_field" not in response.model_fields_set:
        print('Got json like {}, without a "my_field" key present at all.')
    else:
        print('Got json like {"my_field": null}.')

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes that only affect static types, without breaking runtime behavior.
  2. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  3. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.

Determining the installed version

If you've upgraded to the latest version but aren't seeing any new features you were expecting then your python environment is likely still using an older version.

You can determine the version that is being used at runtime with:

import stagehand

print(stagehand.__version__)

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_alpha-0.3.0.tar.gz (247.6 kB view details)

Uploaded Source

Built Distributions

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

stagehand_alpha-0.3.0-py3-none-win_amd64.whl (100.8 kB view details)

Uploaded Python 3Windows x86-64

stagehand_alpha-0.3.0-py3-none-manylinux2014_x86_64.whl (100.0 kB view details)

Uploaded Python 3

stagehand_alpha-0.3.0-py3-none-macosx_11_0_arm64.whl (100.0 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

stagehand_alpha-0.3.0-py3-none-macosx_10_9_x86_64.whl (100.0 kB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file stagehand_alpha-0.3.0.tar.gz.

File metadata

  • Download URL: stagehand_alpha-0.3.0.tar.gz
  • Upload date:
  • Size: 247.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stagehand_alpha-0.3.0.tar.gz
Algorithm Hash digest
SHA256 85e359d7d07a40914caf0d6edf2f13c0ac9bb78db53f0a098f3575d97713b2f4
MD5 0e1fbe915379fe6c620630e57cdf10aa
BLAKE2b-256 17824c22d02bb697a80cf0c8a009080a64709a0f7f82e90f488498b752221fb8

See more details on using hashes here.

File details

Details for the file stagehand_alpha-0.3.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: stagehand_alpha-0.3.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 100.8 kB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stagehand_alpha-0.3.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 65734777986ce4bc2869329300d0b31a688a9fa3dc7c27e889c33364d8cf54c3
MD5 267c29997f6cd93e75f2c2d222e573c6
BLAKE2b-256 5d9d57427b511897a946ea3c838fb3e4b30467c8229cf78a22f3c7922b1be763

See more details on using hashes here.

File details

Details for the file stagehand_alpha-0.3.0-py3-none-manylinux2014_x86_64.whl.

File metadata

  • Download URL: stagehand_alpha-0.3.0-py3-none-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 100.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stagehand_alpha-0.3.0-py3-none-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6277f61337aa5869cc7e448d74d470756750825e963b9663754b56c181bd7770
MD5 ac59dd83182d5ffc0ba16c2e146fe5b8
BLAKE2b-256 a6c62f4a0edef6fc1556482aa7dff2d612771f7d009ac0e1463a2bd34d0ecb63

See more details on using hashes here.

File details

Details for the file stagehand_alpha-0.3.0-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: stagehand_alpha-0.3.0-py3-none-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 100.0 kB
  • Tags: Python 3, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stagehand_alpha-0.3.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c536ea6854929f6a1d99a1711ae0cab3c8fe076f61551ffae7dea2b176a271d
MD5 ebeeb576c8bb2ac6965ac530834c8a0b
BLAKE2b-256 4b12af9c1d6c71529587e39afa58114104feab65fa8d7b8b2eccc04a896fc009

See more details on using hashes here.

File details

Details for the file stagehand_alpha-0.3.0-py3-none-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: stagehand_alpha-0.3.0-py3-none-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 100.0 kB
  • Tags: Python 3, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for stagehand_alpha-0.3.0-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 56d92131ac5c0b566522116d5e669c3fb07907461aa9431f6fe8d9e74844f110
MD5 9e6388725f016adad04af791d46035f8
BLAKE2b-256 2729d0a3c9328052e03dd6e79c98c59f100d1ed6840c725409f99cb6fbc2a561

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