Also bundled
EXAMPLES16 chapters · 94 min
Skills
Chapter 5 of 16
Migrate browser-use (Python) browser-automation scripts to Stagehand v3 (TypeScript) on Browserbase.
5 minutes · 1,108 words · 13 sections
/browser-use-to-stagehand)Convert a browser-use (Python) script into an idiomatic Stagehand v3 (TypeScript) script on Browserbase, choosing the right level of determinism at each step rather than producing a one-to-one agentic copy.
Core principle: browser-use is agentic-by-default (the LLM decides every action). Stagehand lets you choose how much AI to use. A good migration replaces opaque agent loops with an inspectable, mostly-deterministic pipeline — using AI only where the page is genuinely unpredictable. This is a refactor with judgment, not a transpile.
Source of truth & versions. This skill’s durable value is the judgment — the determinism spectrum and the decompose-vs-agent decision — not the API specifics, which drift every release. The code mappings here are a snapshot validated against
@browserbasehq/stagehand3.6.x and browser-use 0.13.x (2026-06). On any conflict, the live docs win — always verify against the installed package and these sources before emitting code:
- Stagehand v3: https://docs.stagehand.dev/v3 (opens in a new tab) · installed types:
node_modules/@browserbasehq/stagehand- Browserbase: https://docs.browserbase.com (opens in a new tab)
- browser-use: https://docs.browser-use.com (opens in a new tab)
If the installed Stagehand major is not 3, treat this skill as conceptual only and follow the live docs for every signature.
references/api-mapping.md (opens in a new tab) — the mechanical browser-use → Stagehand
mapping: variant detection, the full feature table, before/after code, Browserbase platform
options, and v3 version gotchas. Read this for any non-trivial construct.references/determinism.md (opens in a new tab) — how to choose agent() vs
act/extract/observe vs cached observe→act. The decision tree. Read this when deciding
how to translate an Agent(task=…).references/trace-assisted.md (opens in a new tab) — the optional “run it on
Browserbase, read the logs, then rewrite” workflow for opaque/flaky scripts.Obtain the browser-use script(s). If the user only described a script, ask for the file(s). Note the target: TypeScript Stagehand on Browserbase unless they say otherwise.
First, gate on scope — is this even migratable? Not every browser-use file is an
Agent(task=…)script. If the source is browser-use running as an MCP server (uvx browser-use --mcp, amcpServersconfig) there is no Stagehand equivalent — flag it as out of scope, don’t invent one (see api-mapping §3.7b). If the browser-use call is embedded in a larger app (a class/tool wrapper, web route, queue task), convert only the browser-use surface and preserve the surrounding app glue — see api-mapping §3.8.
Identify legacy (pre-0.12) vs stable vs Rust beta (only when imports come from browser_use.beta)
— see api-mapping §1. Note: the classic top-level from browser_use import Agent, ChatBrowserUse
surface is alive and well in 0.13.x — ChatBrowserUse alone is not a beta tell; only a
browser_use.beta import is. All variants translate identically, so when unsure, proceed with the
stable mapping. Normalize legacy names before translating. State which variant you found.
Extract a structured inventory before writing any TypeScript:
task= string(s); split each into its implied ordered steps.Chat* provider + model id.cdp_url/Browserbase; headless; proxies; user_data_dir/storage_state.output_model_schema Pydantic models.sensitive_data, env-var usage, login flows.allowed_domains, max_steps.@tools.action / Controller functions, and whether each is a deterministic
side-effect or an agent capability.initial_actions, secondary models (page_extraction_llm, planner_llm).For each step from the inventory, apply the decision tree in determinism.md:
page.goto(url) on the Stagehand page (no AI).act("…"); if it repeats, observe() once then replay act(action) (no LLM call).extract("…", zodSchema).stagehand.agent().execute(...) (tightened with maxSteps/systemPrompt).Default to decomposition when the flow is known; keep agent() only where it isn’t. For a
first lift-and-shift, a faithful agent() translation is acceptable — say so and note the
optimization path.
First, verify the API. Before writing, confirm the exact signatures you’re about to use against
the installed package (node_modules/@browserbasehq/stagehand types) or https://docs.stagehand.dev/v3 (opens in a new tab).
The mappings below are a 3.6.x snapshot; if anything differs in the installed version, the installed
version wins. Then emit runnable TypeScript. Always:
import { Stagehand } from "@browserbasehq/stagehand"; and import { z } from "zod"; when extracting.const page = stagehand.context.pages()[0];.stagehand.act(...), stagehand.extract(...),
stagehand.observe(...) — never page.act(...)."provider/model" string.env: "BROWSERBASE"; show env: "LOCAL" as the dev option.variables and process.env, never hardcoded.await stagehand.init() at the start, await stagehand.close() in a finally.Include the project setup so it runs (see the templates below).
Alongside the code, produce a short summary:
allowed_domains guardrails,
custom-action logic, secondary-model intent, ambiguous task strings.If the source was one large opaque agent(task=…), was flaky, or your rewrite can’t be confidently
mapped, offer the trace-assisted workflow (trace-assisted.md): run the original on Browserbase, pull
sessions.logs.list, and rewrite from observed behavior. Don’t run anything without the user’s go-ahead.
package.json
{
"name": "stagehand-migration",
"type": "module",
"scripts": { "start": "tsx index.ts" },
"dependencies": {
"@browserbasehq/stagehand": "^3.0.0",
"dotenv": "^16.0.0",
"zod": "^3.25.0"
},
"devDependencies": { "tsx":
Add
"ai": "^5.0.0"(Vercel AI SDK) only if a custom browser-use action maps to an agenttool. Pin v5, not v4 — Stagehand 3.6.x bundlesaiv5 and typesagent({ tools })as the v5ToolSet, where a tool’s schema field isinputSchema. The v4tool()helper emitsparametersinstead and will fail to type-check against Stagehand’s v5ToolSet. If you can’t control the hoistedaiversion, skip thetool()helper and pass a plain object{ description, inputSchema: zodSchema, execute }— it satisfies the v5ToolSetregardless of whichaimajor resolves.
.env
BROWSERBASE_API_KEY=...
BROWSERBASE_PROJECT_ID=...
ANTHROPIC_API_KEY=... # or the provider matching your model stringindex.ts skeleton (decomposed, the preferred shape)
import "dotenv/config";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";
async function main() {
const stagehand = new Stagehand({
env: "BROWSERBASE",
model: "anthropic/claude-sonnet-4-6"
stagehand.act/extract/observe), not the page.stagehand.context.pages()[0]."provider/model" string; the matching provider key is in .env.extract uses a zod schema; zod is in dependencies.variables + process.env; nothing hardcoded.init() / close() present; close() in finally.page.act(), stagehand.page, modelName/modelClientOptions,
enableCaching) from old blog posts. Use v3 — see api-mapping “Version notes”.act() — navigate with page.goto and cache repeatable steps via observe→act; don’t spend an LLM call on every action.agent() — that just reproduces browser-use’s non-determinism in a
new framework. Decompose where the flow is known.allowed_domains — Stagehand has no domain firewall; flag it for review.Install this repository
npx skills add browserbase/skills/plugin marketplace add browserbase/skillsSkills install per repository, not per chapter — the CLI has no documented per-skill form, so we do not print one.
Migrate browser-use (Python) browser-automation scripts to Stagehand v3 (TypeScript) on Browserbase. Use when the user wants to convert, port, rewrite, or migrate a browser-use Agent script to Stagehand, map browser-use features/APIs to Stagehand primitives (act/extract/observe/agent), or move agentic browser automation onto Browserbase with more determinism. Triggers on "browser-use", "browser_use", or "Agent(task=...)".
The verbatim description from this skill’s front matter — the string an agent matches on to decide whether to load it.
Read,Write,Edit,Grep,Bashskills/browser-use-to-stagehand/SKILL.mdmain, last pushed 5 August 2026.SKILL.md, not by matching a directory convention. One layout observed: skills/*/SKILL.md.h1 and no skipped levels:.claude-plugin/marketplace.json by Browserbase, declaring 5 plugins. It is read for editorial metadata only — never as the skill index, which is always the repository tree.references/guide.mdreferences/prompt.md (opens in a new tab) — a self-contained, tool-agnostic version of this
skill; paste it into any AI assistant along with a browser-use script.EXAMPLES.md (opens in a new tab) — before/after script pairs./browserbase/skills.md.md7 files · 67 KB
Everything this skill ships beside its prose. All of it is set here, as subchapters of chapter 5.
Documentation the agent loads on demand, rather than up front.
Everything else published alongside the skill.