Skip to main content

stagehand-middleware (Python)

The facade's run, snapshot and screenshot tools for Python, over an existing stagehand.Stagehand. It is a thin host for the facade bundle that @browserbasehq/stagehand-integrations builds (dist/facade-bundle.json): the JavaScript sent to experimental_batch and the model-facing contract (tool names, descriptions, input schemas, error messages and result caps) come from that bundle, so agents see the same tools as with the TypeScript facade.

from stagehand import Stagehand
from stagehand_middleware.facade import StagehandFacadeTools, facade_instructions, facade_tools

stagehand = await Stagehand.create(...)
tools = StagehandFacadeTools(stagehand, artifact_root="artifacts")

await tools.run(code="await page.goto('https://example.com'); return await page.title();")
tree = await tools.snapshot()
await tools.run(actions=[{"op": "click", "id": "0-7"}])
image = await tools.screenshot(type="jpeg", quality=40)

facade_tools()  # tools/list entries to register with an agent framework
facade_instructions()  # the agent instructions for the system prompt
  • run code runs inside an async function with Playwright-shaped page, context and browser. An error it throws is raised as StagehandFacadeExecutionError (with the JavaScript name and stack). StagehandFacadeInputError covers bad input and contract errors (stale snapshot IDs, malformed run results, artifact paths and caps, a closed facade). Other failures keep their own types: an error from the batch or RPC call itself (for example stagehand.rpc_client.RPCError) propagates unchanged, a snapshot or screenshot past its capture deadline raises TimeoutError, and a failed close raises StagehandFacadeCleanupError (or an ExceptionGroup with the run's own error). Code that does not parse as an async function body raises SyntaxError before anything is sent.
  • Screenshots saved with a path are written under artifact_root (default: the current directory). A path must stay within it and must not pass through a symlink.
  • browser.close() in run code closes the Stagehand and its browser once the call finishes (pass on_close_requested to own that cleanup); later calls fail.
  • quiet_wait=True (experimental, off by default) makes a page.waitForTimeout(ms) longer than 400 ms in run code return once the page is quiet (DOM loaded, no mutation in the main document for 400 ms; shadow roots are not observed), never later than ms. Shorter waits sleep in full.

This host has no keeper tab and does not latch session loss; the TypeScript StagehandFacadeTools does both.

Development

src/stagehand_middleware/facade/_bundle.json is a copy of the built bundle. After changing the TypeScript facade, rebuild the integrations package and run pnpm run sync:python in packages/integrations/core; tests/facade-python-bundle.test.ts and node scripts/sync-python-facade-bundle.mjs --check (which CI runs in the Python job) fail while the copy is stale. The package also checks the copy's sha256 when it loads it.

uv run ruff format --check
uv run ruff check
uv run ty check
uv run pytest

tests/test_conformance.py runs the cases in ../conformance against this host. tests/test_browser.py drives a local Chrome end to end; it is skipped unless STAGEHAND_INTEGRATIONS_BROWSER_TESTS=1 (set CHROME_PATH if the Stagehand SDK does not find Chrome).

Metadata

Release files for stagehand-middleware 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stagehand-middleware 0.1.0
File Size Uploaded Provenance
stagehand_middleware-0.1.0.tar.gz 62.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stagehand-middleware 0.1.0
File Interpreter ABI Platform
stagehand_middleware-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 128.1 kB

Release files / stagehand_middleware-0.1.0.tar.gz

Download URL stagehand_middleware-0.1.0.tar.gz
Size 62.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d09647a30bec55865f92169d54ac4b9c74db19c67ad9db65aa7e0f47362cffb2
BLAKE2b-256 checksum
How to use checksums
119b1e3bb478e29d31679afc3ef5e10989e9a9453593443d7f3ff08d104f0532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / stagehand_middleware-0.1.0-py3-none-any.whl

Download URL stagehand_middleware-0.1.0-py3-none-any.whl
Size 65.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed04255cc9ca7050f4ed661db547108b6f3fcb71006daa992be9cd34002ae251
BLAKE2b-256 checksum
How to use checksums
1cfd5be653d7703c3ce5841829e173a49305ab2acbacc4588f10d06a590a672b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for stagehand-middleware 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page