Create a Stagehand instance and connect it to a browser. Stagehand.create() is the only way to build one; there is no public constructor, and the returned instance is already initialized.
A browser handle from browserbase.launch(), browserbase.connect(), localBrowser.launch(), or localBrowser.connect(). Each handle can back only one Stagehand instance.
The browser handle you passed to Stagehand.create(). Reach pages and the context through it when you hold the instance but not the handle: stagehand.browser.context. Stagehand does not close this browser for you.
Run a trusted, self-contained JavaScript callback inside the Stagehand extension service worker.
Operations made through the supplied batch context route directly through the worker, avoiding an
SDK-to-browser round trip for every command.
Batch callbacks execute in the Stagehand service worker, not the webpage. Treat callback source
as application code rather than untrusted input.
Stagehand invokes the function in the service worker with two arguments: a worker-local
ExperimentalBatchContext and the JSON input supplied to experimentalBatch().
A self-contained callback executed in the extension service worker. It receives the batch
context first and input second. Values from the caller’s lexical scope are not captured.
The page exposed as the callback context’s page. The active page at batch startup is used
when omitted. This does not change the default target of act(), observe(), or extract();
those methods use their own page option or the active page when called.
The callback receives an ExperimentalBatchContext, not the outer SDK Stagehand instance:
page: the worker-local Page selected when the batch starts;
context: a worker-local browser context facade;
act, observe, and extract: the normal Stagehand AI operations;
metrics: the normal Stagehand metrics operation.
The context intentionally does not expose context.close(), Stagehand lifecycle methods, nested
callback batches, or page event subscriptions. Host-only behavior such as local file paths and
screenshot output paths is also unavailable because the callback runs in a browser service worker,
not Node.js.
The callback is serialized and evaluated in a different JavaScript runtime, so it cannot capture
variables from the calling process. Pass external data through input, similarly to
page.evaluate():
const selector = "button[type=submit]";// Incorrect: selector is not defined in the service worker.await stagehand.experimentalBatch(async ({ page }) => { await page.locator(selector).click();});// Correct: input explicitly crosses the runtime boundary.await stagehand.experimentalBatch( async ({ page }, input) => { await page.locator(input.selector).click(); }, { selector },);
Both input and the callback’s return value must be JSON-serializable.
options.page transfers an SDK page ID to the worker. The callback receives the corresponding
worker-local page as batch.page; the outer Page object itself does not cross the boundary.
batch.page is fixed when the batch starts: it represents options.page, or the active page at
startup when no page is supplied. The AI methods follow the same targeting rules as the regular
SDK: an operation-level page option wins; otherwise they resolve the active page when the
operation runs. Changing the active page therefore affects later act(), observe(), and
extract() calls, but does not replace batch.page.
await stagehand.experimentalBatch(async (batch) => { const nextPage = await batch.context.newPage("https://example.com/next"); await batch.context.setActivePage(nextPage); await batch.act("Click Continue"); // Uses nextPage, the current active page. await batch.act("Click Back", { page: batch.page }); // Explicitly uses the startup page.});
Timeout cancellation prevents the callback from starting further Stagehand operations. An
operation already running when the timeout occurs may still finish, so a batch is not an atomic
transaction.
Return token usage and inference timing metrics for this session.
Each Stagehand instance starts at zero. Every successful operation returns metadata.usage; deterministic actions and cache hits report zero-valued usage, so they leave the counters unchanged. Calls that throw before returning a result are not recorded. Reading metrics does not reset them.
Page-created locators to exclude from instruction-planning context. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match.
Override server-side caching for this request. Requests with locator or ignoreLocators bypass server-side caching and return metadata.cache.status as DISABLED.
Page-created CSS or XPath locators to exclude from consideration. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match. text= locators are not yet supported for observe snapshot scoping.
A page-created CSS or XPath locator that scopes the operation. Create it with page.locator("..."); use .nth(index) on the locator to target a specific match. text= locators are not yet supported for observe snapshot scoping.
The schema used to validate extracted data. Defaults to an object with a string extraction field
when omitted; when selecting a custom schema generic, provide the matching runtime schema.
Page-created CSS or XPath locators to exclude from consideration. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match. text= locators are not yet supported for extract snapshot scoping.
A page-created CSS or XPath locator that scopes the operation. Create it with page.locator("..."); use .nth(index) on the locator to target a specific match. text= locators are not yet supported for extract snapshot scoping.
Create a Stagehand instance and connect it to a browser. Stagehand.create() is the only way to build one; there is no public constructor, and the returned instance is already initialized.
A browser handle from browserbase.launch(), browserbase.connect(), local_browser.launch(), or local_browser.connect(). Each handle can back only one Stagehand instance.
The browser handle you passed to Stagehand.create(). Reach pages and the context through it when you hold the instance but not the handle: stagehand.browser.context. Stagehand does not close this browser for you.
Run trusted, self-contained JavaScript source inside the Stagehand extension service worker.
Operations made through the supplied batch context route directly through the worker, avoiding a
Python-to-browser round trip for every command. Python source is not translated to JavaScript;
this method accepts an explicit JavaScript function string.
Batch callbacks execute in the Stagehand service worker, not the webpage. Treat callback source
as application code rather than untrusted input.
Stagehand evaluates the function in the service worker and calls it with two JavaScript arguments:
a worker-local batch context containing page, context, act, observe, extract, and
metrics;
the decoded JSON value passed as the Python method’s input argument.
JavaScript destructuring such as async ({ page, act }, input) => { ... } only creates local
variables from that context. It does not select a page or tell Stagehand which values to inject.
JSON-serializable input passed as the JavaScript callback’s second argument. When omitted, the
callback receives JavaScript undefined; explicitly passing None produces JavaScript null.
The page exposed as the callback context’s page. The active page at batch startup is used when
omitted. This does not change the default target of act(), observe(), or extract(); those
methods use their own page option or the active page when called.
The first JavaScript argument is a batch context, not the outer Python Stagehand object:
batch.page: the worker-local page selected when the batch starts;
batch.context: a worker-local browser context facade;
batch.act(), batch.observe(), and batch.extract(): Stagehand AI operations;
batch.metrics(): Stagehand metrics.
The context does not expose context.close(), Stagehand lifecycle methods, nested batches, or page
event subscriptions. Node.js APIs, local filesystem paths, screenshot output paths, and other
host-only behavior are unavailable in the browser service worker.
Passing page=my_page transfers the Python page’s ID. The service worker reconstructs it as
batch.page; the Python Page object itself is not available inside the JavaScript source.
batch.page is fixed at batch startup. It represents the supplied page, or the active page at
startup when page is omitted. batch.act(), batch.observe(), and batch.extract() instead use
their own operation-level page option or resolve the active page when called, matching ordinary
Stagehand behavior.Timeout cancellation prevents later Stagehand operations from starting, but an operation already
running at the deadline may still finish. Callback batches are not atomic transactions.
Return token usage and inference timing metrics for this session.
Each Stagehand instance starts at zero. Every successful operation returns metadata.usage; deterministic actions and cache hits report zero-valued usage, so they leave the counters unchanged. Calls that raise before returning a result are not recorded. Reading metrics does not reset them.
Page-created locators to exclude from instruction-planning context. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match.
A page-created CSS or XPath locator that scopes the operation. Create it with page.locator("..."); use .nth(index) on the locator to target a specific match. text= locators are not yet supported for observe snapshot scoping.
Page-created CSS or XPath locators to exclude from consideration. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match. text= locators are not yet supported for observe snapshot scoping.
Override server-side caching for this request. Requests with locator or ignore_locators bypass server-side caching and return metadata.cache.status as DISABLED.
A page-created CSS or XPath locator that scopes the operation. Create it with page.locator("..."); use .nth(index) on the locator to target a specific match. text= locators are not yet supported for extract snapshot scoping.
Page-created CSS or XPath locators to exclude from consideration. Create each with page.locator("..."); use .nth(index) on a locator to exclude only that indexed match. text= locators are not yet supported for extract snapshot scoping.
Create a Stagehand instance and connect it to a browser. The package-level stagehand.Create() function is the only way to build one, and the returned instance is already initialized.
A browser handle from LaunchBrowserbase(), ConnectBrowserbase(), LaunchLocalBrowser(), or ConnectLocalBrowser(). Each handle can back only one Stagehand instance.
The remaining CreateOptions fields (APIKey, APIURL, Cache, DOMSettleTimeoutMs, Model, Generate, Logging, SelfHeal, SystemPrompt, and Telemetry) are optional and covered in the configuration guides.
The browser handle you passed to Create(). Reach pages and the context through it when you hold the instance but not the handle: client.Browser().Context(). Stagehand does not close this browser for you.
Run trusted, self-contained JavaScript source inside the Stagehand extension service worker.
Operations made through the supplied batch context route directly through the worker, avoiding a
Go-to-browser round trip for every command. Go source is not translated to JavaScript; this method
accepts an explicit JavaScript function string.
Batch callbacks execute in the Stagehand service worker, not the webpage. Treat callback source
as application code rather than untrusted input.
func (s *Stagehand) ExperimentalBatch(ctx context.Context, source string, input any, result any, options ExperimentalBatchOptions) error
var result struct { Title string `json:"title"`}err := client.ExperimentalBatch( ctx, `async (batch, input) => { await batch.page.goto(input.url); await batch.act("Click More information"); return { title: await batch.page.title() }; }`, map[string]string{"url": "https://example.com"}, &result, stagehand.ExperimentalBatchOptions{},)if err != nil { return err}
Stagehand evaluates the function in the service worker and calls it with two JavaScript arguments:
a worker-local batch context containing page, context, act, observe, extract, and
metrics;
the decoded JSON value passed as the Go method’s input argument.
JavaScript destructuring such as async ({ page, act }, input) => { ... } only creates local
variables from that context. It does not select a page or tell Stagehand which values to inject.
The page exposed as the callback context’s page. The active page at batch startup is used when
omitted. This does not change the default target of Act(), Observe(), or Extract(); those
methods use their own Page option or the active page when called.
The first JavaScript argument is a batch context, not the outer Go Stagehand client:
batch.page: the worker-local page selected when the batch starts;
batch.context: a worker-local browser context facade;
batch.act(), batch.observe(), and batch.extract(): Stagehand AI operations;
batch.metrics(): Stagehand metrics.
The context does not expose context.close(), Stagehand lifecycle methods, nested batches, or page
event subscriptions. Node.js APIs, local filesystem paths, screenshot output paths, and other
host-only behavior are unavailable in the browser service worker.
Passing Page in the options transfers the Go page’s ID. The service worker reconstructs it as
batch.page; the Go Page object itself is not available inside the JavaScript source.
batch.page is fixed at batch startup. It represents the supplied page, or the active page at
startup when Page is omitted. batch.act(), batch.observe(), and batch.extract() instead use
their own operation-level page option or resolve the active page when called, matching ordinary
Stagehand behavior.Timeout cancellation prevents later Stagehand operations from starting, but an operation already
running at the deadline may still finish. Callback batches are not atomic transactions.
Return token usage and inference timing metrics for this session.
Each Stagehand instance starts at zero. Every successful operation returns Metadata.Usage; deterministic actions and cache hits report zero-valued usage, so they leave the counters unchanged. Calls that return an error before producing a result are not recorded. Reading metrics does not reset them.
A natural-language action to perform, or an action returned by Observe(). Construct with
stagehand.ActInstruction("...") for natural language, or stagehand.ObservedAction(action)
for an observed Action with these fields:
Override server-side caching for this request. Construct with stagehand.CacheEnabled(enabled)
or stagehand.CacheWithThreshold(threshold), where the threshold is the positive
identical-result count required before serving a cache hit.
Page-created locators to exclude from instruction-planning context. Create each with
page.Locator("..."); use Nth(index) on a locator to exclude only that indexed match.
Override server-side caching for this request. Construct with stagehand.CacheEnabled(enabled)
or stagehand.CacheWithThreshold(threshold), where the threshold is the identical-result count
required before serving a cache hit.
Page-created CSS or XPath locators to exclude from consideration. Create each with
page.Locator("..."); use Nth(index) on a locator to exclude only that indexed match.
text= locators are not yet supported for observe snapshot scoping.
A page-created CSS or XPath locator that scopes the operation. Create it with
page.Locator("..."); use Nth(index) on the locator to target a specific match. text=
locators are not yet supported for observe snapshot scoping.
Extract structured data from the page. Extract is a package-level generic function rather than a method: the type parameter is the schema. Stagehand derives a JSON Schema from T, extracts matching data, and decodes the result into a T. Unlike TypeScript and Python, there is no default schema; supply T explicitly.
type Product struct { Name string `json:"name"` Price float64 `json:"price"`}product, err := stagehand.Extract[Product](ctx, client, "Extract the product", nil)if err != nil { return err}fmt.Println(product.Data, product.Metadata.Cache.Status)
Override server-side caching for this request. Construct with stagehand.CacheEnabled(enabled)
or stagehand.CacheWithThreshold(threshold), where the threshold is the identical-result count
required before serving a cache hit.
Page-created CSS or XPath locators to exclude from consideration. Create each with
page.Locator("..."); use Nth(index) on a locator to exclude only that indexed match.
text= locators are not yet supported for extract snapshot scoping.
A page-created CSS or XPath locator that scopes the operation. Create it with
page.Locator("..."); use Nth(index) on the locator to target a specific match. text=
locators are not yet supported for extract snapshot scoping.