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.1

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.1
File Size Uploaded Provenance
stagehand_middleware-0.1.1.tar.gz 63.1 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 128.5 kB

Release files / stagehand_middleware-0.1.1.tar.gz

Download URL stagehand_middleware-0.1.1.tar.gz
Size 63.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8930d0b21c3698b973f580c14ce14c0d59d19e83410cfbd28dc0f182e95d48c3
BLAKE2b-256 checksum
How to use checksums
c2c8b9c5a15eaa324b3f05050094dd1a6b5a3603592501b29ccdab1a7100660b
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.1-py3-none-any.whl

Download URL stagehand_middleware-0.1.1-py3-none-any.whl
Size 65.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5fe16ce77695dd2c67851f549ac1eba4e296797def1b5ce590ea7593a6121e7
BLAKE2b-256 checksum
How to use checksums
3e8c4db665bc0657f2845c869454c83da8e6caa5cbbc18d2dc5aa5ce0e71748c
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.1

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.1 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