You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<strong>NOTE:</strong> This is a Python SDK for Stagehand. Original implementation is in TypeScript and is available <a href="https://github.com/browserbase/stagehand" style="color: blue;">here</a>.
<strong>NOTE:</strong> This is a Python SDK for Stagehand. The original implementation is in TypeScript and is available <ahref="https://github.com/browserbase/stagehand"style="color: blue;">here</a>.
39
+
</div>
41
40
42
41
---
43
42
44
-
A Python SDK for [Stagehand](https://stagehand.dev), enabling automated browser control and data extraction.
45
-
46
-
Stagehand is the easiest way to build browser automations. It is fully compatible with Playwright, offering three simple AI APIs (act, extract, and observe) on top of the base Playwright Page class that provide the building blocks for web automation via natural language.
47
-
48
-
You can write all of your Playwright commands as you normally would, while offloading the AI-powered `act/extract/observe` operations to Stagehand hosted on our Stagehand API.
49
-
50
-
51
-
Here's a sample of what you can do with Stagehand:
52
-
53
-
```python
54
-
import asyncio
55
-
56
-
asyncdefmain():
57
-
# Keep your existing Playwright code unchanged
58
-
await page.goto("https://docs.stagehand.dev");
59
-
60
-
# Stagehand AI: Act on the page via Stagehand API
61
-
await page.act("click on the 'Quickstart'");
62
-
63
-
# Stagehand AI: Extract data from the page
64
-
from pydantic import BaseModel
65
-
66
-
classDescriptionSchema(BaseModel):
67
-
description: str
68
-
69
-
data =await page.extract(
70
-
instruction="extract the description of the page",
71
-
schema=DescriptionSchema
72
-
)
73
-
description = data.description
74
-
75
-
if__name__=="__main__":
76
-
asyncio.run(main())
77
-
```
43
+
Stagehand is the easiest way to build browser automations with AI-powered interactions. It extends the Playwright API with three powerful AI primitives:
44
+
45
+
-**act** — Instruct the AI to perform actions (e.g. click a button or scroll).
46
+
-**extract** — Extract and validate data from a page using a JSON schema (generated either manually or via a Pydantic model).
47
+
-**observe** — Get natural language interpretations to, for example, identify selectors or elements from the DOM.
48
+
## Pydantic Schemas
49
+
50
+
Stagehand uses Pydantic models to define the options for AI commands:
51
+
52
+
-**ActOptions**
53
+
The `ActOptions` model takes an `action` field that tells the AI what to do on the page, plus optional fields such as `useVision` and `variables`:
54
+
```python
55
+
from stagehand.schemas import ActOptions
56
+
57
+
# Example:
58
+
await page.act(ActOptions(action="click on the 'Quickstart' button"))
59
+
```
60
+
61
+
-**ObserveOptions**
62
+
The `ObserveOptions` model lets you find elements on the page using natural language. The `onlyVisible` option helps limit the results:
63
+
```python
64
+
from stagehand.schemas import ObserveOptions
65
+
66
+
# Example:
67
+
await page.observe(ObserveOptions(instruction="find the button labeled 'News'", onlyVisible=True))
68
+
```
69
+
70
+
-**ExtractOptions**
71
+
The `ExtractOptions` model extracts structured data from the page. Pass your instructions and a schema defining your expected data format. **Note:** If you are using a Pydantic model for the schema, call its `.model_json_schema()` method to ensure JSON serializability.
72
+
```python
73
+
from stagehand.schemas import ExtractOptions
74
+
from pydantic import BaseModel
75
+
76
+
classDescriptionSchema(BaseModel):
77
+
description: str
78
+
79
+
# Example:
80
+
data =await page.extract(
81
+
ExtractOptions(
82
+
instruction="extract the description of the page",
0 commit comments