[Docs] v3-->v4 migration guide - #2613
Conversation
|
| | Streaming, callbacks, abort signal, message continuation | No equivalent. Your loop already sits between steps, so log, cancel, or persist there | | ||
|
|
||
| <Note> | ||
| If you want a framework to own the loop instead of writing it yourself, the [LangChain](/v4/integrations/langchain/introduction) and [CrewAI](/v4/integrations/crew-ai/introduction) integrations expose the primitives as agent tools. |
There was a problem hiding this comment.
i think we'll have an overview page for agent integrations as a port to all the integrations which we can link to as well later
There was a problem hiding this comment.
Sounds great, I've made this a TODO for now
29e0822 to
8279a88
Compare
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Architecture diagram
sequenceDiagram
participant Dev as Developer
participant AS as AI Assistant
participant Rules as AI Rules File
participant SH as Stagehand v4 SDK
participant Browser as Browser Instance
participant Page as Page / Context
participant Model as LLM Model
Note over Dev,Model: v3→v4 Migration - Code Mode Path
Dev->>AS: Request: "Write a Stagehand v4 script that..."
AS->>Rules: Read AI rules (v4 API conventions)
Rules-->>AS: Return rules
AS->>SH: Generate script using Stagehand.create()
AS-->>Dev: Return generated script
Dev->>SH: Execute generated script
SH->>Browser: browserbase.launch() / localBrowser.launch()
Browser-->>SH: Return browser instance
SH->>SH: Stagehand.create({ browser })
SH->>Browser: browser.context.newPage(url)
Browser-->>SH: Return page
alt Deterministic operations (stable selectors)
SH->>Page: page.locator().click()
SH->>Page: page.goto()
Page-->>SH: Standard Playwright responses
else Model-backed operations (natural language)
SH->>Page: page.snapshot()
Page-->>SH: formattedTree + xpathMap
SH->>Model: stagehand.act("Open most-commented story")
Model-->>SH: Action instruction
SH->>Page: Locator-based action
SH->>Model: stagehand.extract("Extract top 5 comments", schema)
Model-->>SH: Structured data
end
SH-->>Dev: Return { data, metadata }
Dev->>SH: stagehand.close()
SH->>Browser: browser.close()
Note over Dev,Model: v3→v4 Migration - Tool Calling Path
Dev->>AS: Request: "Create tool-calling loop"
AS->>Rules: Read AI rules
Rules-->>AS: Return rules
AS-->>Dev: Return tool definitions
alt Tool execution loop
loop Each step
Dev->>Model: Call with tool definitions + context
Model-->>Dev: Select tool + parameters
alt Navigation tools
Dev->>Page: page.goto() / page.reload() / page.goBack()
else Perception tools
Dev->>Page: page.snapshot() / page.screenshot()
Page-->>Dev: formattedTree (for next model call)
else Element interaction
Dev->>Page: locator.click() / locator.fill()
else Model-backed tools
Dev->>SH: stagehand.act() / stagehand.extract()
SH->>Model: Process natural language instruction
Model-->>SH: Result
SH-->>Dev: { data, metadata }
end
end
end
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
0595ba6 to
3db26a8
Compare
There was a problem hiding this comment.
1 issue found across 2 files
Confidence score: 4/5
- In
packages/docs/v4/migrations/v3.mdx, the migration mapping reads as language-agnostic even though it is TypeScript-specific, so Python users may copy invalid fields (modelName,apiKey,onLog) and hit avoidable runtime/type errors during migration — split the guidance by language (or clearly label TS-only fields) and provide Python-safe equivalents.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/docs/v4/migrations/v3.mdx">
<violation number="1" location="packages/docs/v4/migrations/v3.mdx:263">
P2: The copy-paste migration mapping is TypeScript-only but appears language-agnostic, so Python users can migrate to non-existent fields (`modelName`, `apiKey`, `onLog`) and get runtime/type errors. Consider splitting this block by language (or annotating TS vs Python equivalents) so Python points to `model`, `model_api_key`, and `on_log`.
(Based on your team's feedback about SDK-native field spellings for TS/Python.)</violation>
</file>
Architecture diagram
sequenceDiagram
participant CodeGen as AI Coding Assistant
participant UserScript as Stagehand Script
participant SDK as Stagehand v4 SDK
participant Page as Browser Page
participant Model as AI Model (LLM)
participant Snap as Page Snapshot
Note over CodeGen,Snap: Migration Path 1: Code Mode (Recommended)
UserScript->>SDK: browserbase.launch() / localBrowser.launch()
SDK-->>UserScript: Browser instance
UserScript->>SDK: Stagehand.create({ browser })
SDK-->>UserScript: Stagehand instance
UserScript->>Page: browser.context.newPage(url)
Page-->>UserScript: Page handle
alt Deterministic Navigation
UserScript->>Page: page.locator(selector).click()
UserScript->>Page: page.goto(url)
Page-->>UserScript: DOM state
else Model-backed Interaction
UserScript->>SDK: stagehand.act("Natural language instruction")
SDK->>Model: Process instruction
Model-->>SDK: Action plan
SDK->>Page: Execute action
Page-->>SDK: Result
SDK-->>UserScript: { data, metadata }
end
UserScript->>SDK: stagehand.extract("Extract structured data", schema)
SDK->>Page: Read page content
Page-->>SDK: Raw content
SDK->>Model: Parse structure
Model-->>SDK: Structured result
SDK-->>UserScript: { data, metadata }
Note over CodeGen,Snap: Migration Path 2: Tool-Calling Loop
UserScript->>Page: page.snapshot()
Page->>Snap: Build formattedTree + xpathMap
Snap-->>UserScript: Accessibility tree + selectors
loop Each step
UserScript->>Model: Expose tools (click, fill, snapshot, act, extract, etc.)
Model->>SDK: Choose tool + parameters
alt Deterministic tool
SDK->>Page: Use Playwright locator (click, fill, goto)
Page-->>SDK: Result
else Model-backed tool
SDK->>Model: Process natural language
Model-->>SDK: Structured action
SDK->>Page: Execute
Page-->>SDK: Result
end
SDK-->>UserScript: { data, metadata }
end
Note over UserScript,SDK: Cleanup
UserScript->>SDK: stagehand.close()
UserScript->>SDK: browser.close()
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| - act/extract/observe now return { data, metadata }; read .data | ||
| - extract({ instruction, schema }) -> extract(instruction, schema) | ||
| - page.deepLocator(sel) -> page.locator(sel) | ||
| - modelName + modelClientOptions -> model: { modelName, apiKey } |
There was a problem hiding this comment.
P2: The copy-paste migration mapping is TypeScript-only but appears language-agnostic, so Python users can migrate to non-existent fields (modelName, apiKey, onLog) and get runtime/type errors. Consider splitting this block by language (or annotating TS vs Python equivalents) so Python points to model, model_api_key, and on_log.
(Based on your team's feedback about SDK-native field spellings for TS/Python.)
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/docs/v4/migrations/v3.mdx, line 263:
<comment>The copy-paste migration mapping is TypeScript-only but appears language-agnostic, so Python users can migrate to non-existent fields (`modelName`, `apiKey`, `onLog`) and get runtime/type errors. Consider splitting this block by language (or annotating TS vs Python equivalents) so Python points to `model`, `model_api_key`, and `on_log`.
(Based on your team's feedback about SDK-native field spellings for TS/Python.) </comment>
<file context>
@@ -0,0 +1,488 @@
+ - act/extract/observe now return { data, metadata }; read .data
+ - extract({ instruction, schema }) -> extract(instruction, schema)
+ - page.deepLocator(sel) -> page.locator(sel)
+ - modelName + modelClientOptions -> model: { modelName, apiKey }
+ - enableCaching -> cache
+ - verbose + logger -> logging: { level, format, onLog }
</file context>
| icon: 'arrow-up-right-dots' | ||
| --- | ||
|
|
||
| Two changes account for most of this migration: |
There was a problem hiding this comment.
should we recommend just giving this page to your agent to help you fast migrate?
why
what changed
test plan
Summary by cubic
Adds a v3→v4 migration guide with two paths to replace
agent()—code mode or a tool-calling loop—plus a quick-reference table, troubleshooting, and rationale for removingagent(). Updates the docs nav with a new “Migration guide” group and aligns with Linear STG-2685.v4/migrations/v3with TypeScript and Python examples using@browserbasehq/stagehand; includes a copy-paste upgrade block, a recommended migration order, quick reference, troubleshooting, and a TODO for an integrations overview link.agent()with either: (1) Code mode (generate a v4 script), or (2) a tool-calling loop exposing the full API; usepage.snapshot()for planner context; optional page tools via WebMCP.browserbase.launch()/localBrowser.launch()andStagehand.create({ browser })(constructor is private).browser.contextwith async getters;act(),extract(), andobserve()moved to the Stagehand instance (target tabs with{ page }); prefer retryingobserve().{ data, metadata };extract()uses positional args.model: { modelName, apiKey }; enable server caching withcache(requires a Browserbase browser).logging: { level, format, onLog }; metrics viaawait stagehand.metrics().page.deepLocator()removed (usepage.locator()with the same selectors).Written for commit 3db26a8. Summary will update on new commits.