LangChain Stagehand
Add a persistent browser to a LangChain or Deep Agents agent with StagehandMiddleware. The middleware exposes Stagehand's shared run, snapshot, and screenshot tools, includes browser usage guidance in their descriptions, and returns screenshots as model-visible images. The agent’s system prompt is left untouched. No MCP server is needed.
Install
pip install langchain-stagehand
Python 3.11+ is required. Set your model provider's API key in your environment, and BROWSERBASE_API_KEY to use Browserbase. Never put credentials in browser tool inputs.
Use with LangChain
import asyncio
from langchain.agents import create_agent
from langchain_stagehand import StagehandMiddleware
from stagehand import Stagehand, browserbase
async def main():
import os
browser = await browserbase.launch(api_key=os.environ["BROWSERBASE_API_KEY"])
stagehand = await Stagehand.create(browser=browser)
middleware = StagehandMiddleware(stagehand=stagehand)
agent = create_agent("anthropic:claude-sonnet-4-6", middleware=[middleware])
try:
result = await agent.ainvoke({
"messages": [{"role": "user", "content": "Open example.com and summarize the page."}]
})
print(result["messages"][-1].content)
finally:
await browser.close()
asyncio.run(main())
Install the model integration separately, such as pip install langchain-anthropic, and set ANTHROPIC_API_KEY for this example. Browser tool calls use Stagehand's Playwright-shaped facade; they do not require a separate Stagehand model for these operations.
Use with Deep Agents
Use the same Stagehand client setup and replace create_agent with:
from deepagents import create_deep_agent
agent = create_deep_agent("anthropic:claude-sonnet-4-6", middleware=[StagehandMiddleware(stagehand=stagehand)])
Install deepagents separately. Use ainvoke or astream: browser tools and middleware are asynchronous.
Browser lifecycle
Pass a configured Stagehand client attached to local Chrome or Browserbase:
from stagehand import Stagehand, local_browser
browser = await local_browser.launch(headless=True)
stagehand = await Stagehand.create(browser=browser)
middleware = StagehandMiddleware(stagehand=stagehand)
Your application configures and owns the Stagehand client and browser. All SDK settings remain available through Stagehand.create(...), including API URL, model, caching, telemetry, and logging. The middleware reuses that exact client across runs, preserving tabs, logins, and snapshot IDs. Close the browser when finished; that also closes the client. The middleware never closes or recreates the client, and browser.close() in run code leaves both open. After a disconnection, the application supplies a new configured client. Give concurrent agents separate clients/browsers. Take a new snapshot after navigation before using snapshot IDs.
The screenshot tool returns images to the model. Screenshots that run code saves with a path are written under the current working directory, not to a Deep Agents backend; backend support is not implemented yet.
Security and limitations
- Browser tools can navigate, submit forms, and interact with authenticated websites; use LangChain's human-in-the-loop middleware for sensitive actions
- Web page content is untrusted input, not instructions for your agent
- The Playwright-shaped API is not a general-purpose security sandbox or a substitute for network isolation
- Agent execution is asynchronous only; browser state lives in the browser and is not checkpointed
Development
make install
make format
make check
RUN_BROWSER_TESTS=1 uv run pytest tests/test_browser.py --color=no
make build
make check enforces Ruff formatting and lint rules (including bug-prone patterns, async misuse, and accidental prints), ty checking of the package and public API examples with all diagnostics treated as errors, and focused middleware tests. The distribution includes py.typed so downstream type checkers can validate usage. Astral's ty is pinned in the Makefile and runs through uvx; no extra runtime dependency is needed.
The live browser test covers local Chrome and Browserbase; the remote case is skipped without BROWSERBASE_API_KEY. Set CHROME_PATH if Chrome is not in its usual install location. The Python distribution is langchain-stagehand, and the import is langchain_stagehand; this repository does not publish the separate upstream stagehand-deepagents package.
Metadata
Release files for langchain-stagehand 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_stagehand-0.0.1.tar.gz | 4.7 kB | Without provenance | Details for langchain_stagehand-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_stagehand-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for langchain_stagehand-0.0.1-py3-none-any.whl |
Total release size: 10.3 kB
Release files / langchain_stagehand-0.0.1.tar.gz
| Download URL | langchain_stagehand-0.0.1.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0bd77039cc0d465b6012178a5fd098d509dbd9c8cfa8bda6b5134dcdd288ad6b
|
|
BLAKE2b-256 checksum How to use checksums |
7d34a36cd72d0ef93430132804b5fd773bdbb5d888b7a24b56168bb18de6a87a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / langchain_stagehand-0.0.1-py3-none-any.whl
| Download URL | langchain_stagehand-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69c406a435ddb247a1174b88ce96dce5689a26f1771580725a1581c335ae09a6
|
|
BLAKE2b-256 checksum How to use checksums |
d22d985947378ededbdca63c8e697a02fb2e5fbcc062ac6b132eeffe10834925
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for langchain-stagehand 0.0.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.