@@ -84,7 +84,7 @@ await stagehand.agent.execute("book a reservation for 2 people for a trip to the
84
84
```
85
85
86
86
87
- ## Installation:
87
+ ## Installation
88
88
89
89
To get started, simply:
90
90
@@ -96,10 +96,14 @@ pip install stagehand
96
96
97
97
``` bash
98
98
uv venv .venv
99
- source .venv/bin/activate
99
+ source .venv/bin/activate # On Windows: .venv\Scripts\activate
100
100
uv pip install stagehand
101
101
```
102
102
103
+ ### Prerequisites
104
+
105
+ Stagehand requires a local browser installation. The library uses Playwright to manage browser instances automatically. No additional browser setup is required - Playwright will download the necessary browser binaries on first use.
106
+
103
107
## Quickstart
104
108
105
109
``` python
@@ -111,7 +115,7 @@ from pydantic import BaseModel, Field
111
115
from stagehand import StagehandConfig, Stagehand
112
116
113
117
# Load environment variables
114
- load_dotenv()
118
+ load_dotenv() # Create a .env file or set environment variables in your shell
115
119
116
120
# Define Pydantic models for structured data extraction
117
121
class Company (BaseModel ):
@@ -122,25 +126,25 @@ class Companies(BaseModel):
122
126
companies: list[Company] = Field(... , description = " List of companies" )
123
127
124
128
async def main ():
125
- # Create configuration
129
+ # Create configuration with Alibaba ModelScope (DashScope)
126
130
config = StagehandConfig(
127
- env = " BROWSERBASE" , # or LOCAL
128
- api_key = os.getenv(" BROWSERBASE_API_KEY" ),
129
- project_id = os.getenv(" BROWSERBASE_PROJECT_ID" ),
130
- model_name = " google/gemini-2.5-flash-preview-05-20" ,
131
- model_api_key = os.getenv(" MODEL_API_KEY" ),
131
+ model_name = " dashscope/qwen-turbo" ,
132
+ model_client_options = {
133
+ " api_base" : os.getenv(" ALIBABA_ENDPOINT" , " https://dashscope.aliyuncs.com/compatible-mode/v1" ),
134
+ " api_key" : os.getenv(" ALIBABA_API_KEY" )
135
+ },
136
+ local_browser_launch_options = {
137
+ " headless" : False # Set to True for headless mode
138
+ }
132
139
)
133
140
134
141
stagehand = Stagehand(config)
135
142
136
143
try :
137
144
print (" \n Initializing 🤘 Stagehand..." )
138
- # Initialize Stagehand
145
+ # Initialize Stagehand with local browser
139
146
await stagehand.init()
140
147
141
- if stagehand.env == " BROWSERBASE" :
142
- print (f " 🌐 View your live browser: https://www.browserbase.com/sessions/ { stagehand.session_id} " )
143
-
144
148
page = stagehand.page
145
149
146
150
await page.goto(" https://www.aigrant.com" )
@@ -156,9 +160,9 @@ async def main():
156
160
for idx, company in enumerate (companies_data.companies, 1 ):
157
161
print (f " { idx} . { company.name} : { company.description} " )
158
162
159
- observe = await page.observe(" the link to the company Browserbase " )
163
+ observe = await page.observe(" the search bar " )
160
164
print (" \n Observe result:" , observe)
161
- act = await page.act(" click the link to the company Browserbase " )
165
+ act = await page.act(" click on the search bar " )
162
166
print (" \n Act result:" , act)
163
167
164
168
except Exception as e:
@@ -173,6 +177,143 @@ if __name__ == "__main__":
173
177
asyncio.run(main())
174
178
```
175
179
180
+ ## Configuration Options
181
+
182
+ ### Basic Configuration
183
+
184
+ ``` python
185
+ # OpenAI (default)
186
+ config = StagehandConfig(
187
+ model_name = " gpt-4o-mini" ,
188
+ model_client_options = {
189
+ " api_key" : os.getenv(" OPENAI_API_KEY" )
190
+ }
191
+ )
192
+
193
+ # Anthropic Claude
194
+ config = StagehandConfig(
195
+ model_name = " claude-3-haiku-20240307" ,
196
+ model_client_options = {
197
+ " api_base" : " https://api.anthropic.com" ,
198
+ " api_key" : os.getenv(" ANTHROPIC_API_KEY" )
199
+ }
200
+ )
201
+ ```
202
+
203
+ ### Custom API Endpoints
204
+
205
+ Stagehand supports various OpenAI/Anthropic compatible providers:
206
+
207
+ ``` python
208
+ # Together AI
209
+ config = StagehandConfig(
210
+ model_name = " meta-llama/Llama-2-7b-chat-hf" ,
211
+ model_client_options = {
212
+ " api_base" : " https://api.together.xyz/v1" ,
213
+ " api_key" : os.getenv(" TOGETHER_API_KEY" )
214
+ }
215
+ )
216
+
217
+ # Groq
218
+ config = StagehandConfig(
219
+ model_name = " llama2-70b-4096" ,
220
+ model_client_options = {
221
+ " api_base" : " https://api.groq.com/openai/v1" ,
222
+ " api_key" : os.getenv(" GROQ_API_KEY" )
223
+ }
224
+ )
225
+
226
+ # Local OpenAI-compatible server
227
+ config = StagehandConfig(
228
+ model_name = " local/custom-model" ,
229
+ model_client_options = {
230
+ " api_base" : " http://localhost:8000/v1" ,
231
+ " api_key" : " local-key"
232
+ }
233
+ )
234
+ ```
235
+
236
+ ### Browser Configuration
237
+
238
+ ``` python
239
+ config = StagehandConfig(
240
+ model_name = " gpt-4o-mini" ,
241
+ model_client_options = {" api_key" : os.getenv(" OPENAI_API_KEY" )},
242
+ local_browser_launch_options = {
243
+ " headless" : True , # Run in headless mode
244
+ " viewport" : {" width" : 1280 , " height" : 720 },
245
+ " user_data_dir" : " ./browser_data" , # Persistent browser data
246
+ " args" : [" --no-sandbox" , " --disable-dev-shm-usage" ] # Additional Chrome args
247
+ }
248
+ )
249
+ ```
250
+
251
+ ## Migration from Browserbase
252
+
253
+ If you're upgrading from a previous version that used Browserbase, here's how to migrate your configuration:
254
+
255
+ ### Quick Migration Check
256
+
257
+ Use our migration utility to scan your project:
258
+
259
+ ``` bash
260
+ # Scan current directory for files needing migration
261
+ python docs/migration_utility.py scan .
262
+
263
+ # Generate configuration examples
264
+ python docs/migration_utility.py config openai
265
+ ```
266
+
267
+ ### Before (Browserbase Configuration)
268
+ ``` python
269
+ # Old Browserbase configuration
270
+ config = StagehandConfig(
271
+ env = " BROWSERBASE" ,
272
+ api_key = " browserbase-api-key" ,
273
+ project_id = " browserbase-project-id" ,
274
+ model_name = " gpt-4o" ,
275
+ model_api_key = " openai-api-key"
276
+ )
277
+ ```
278
+
279
+ ### After (Local Configuration)
280
+ ``` python
281
+ # New local configuration
282
+ config = StagehandConfig(
283
+ model_name = " gpt-4o" ,
284
+ model_client_options = {
285
+ " api_key" : " openai-api-key" ,
286
+ # Optional: specify custom endpoint
287
+ " api_base" : " https://api.openai.com/v1"
288
+ },
289
+ local_browser_launch_options = {
290
+ " headless" : False # Configure browser options as needed
291
+ }
292
+ )
293
+ ```
294
+
295
+ ### Key Changes
296
+ - ** Removed** : ` env ` , ` api_key ` , ` project_id ` parameters
297
+ - ** Replaced** : ` model_api_key ` with ` model_client_options.api_key `
298
+ - ** Added** : ` local_browser_launch_options ` for browser configuration
299
+ - ** Enhanced** : Support for custom API endpoints via ` model_client_options.api_base `
300
+
301
+ ### Environment Variables
302
+ Update your environment variables:
303
+ ``` bash
304
+ # Remove these (no longer needed)
305
+ # BROWSERBASE_API_KEY=your-browserbase-key
306
+ # BROWSERBASE_PROJECT_ID=your-project-id
307
+
308
+ # Keep or add these
309
+ OPENAI_API_KEY=your-openai-key
310
+ # Or for other providers:
311
+ # ANTHROPIC_API_KEY=your-anthropic-key
312
+ # TOGETHER_API_KEY=your-together-key
313
+ ```
314
+
315
+ For a complete migration guide with troubleshooting, see [ docs/migration_guide.md] ( docs/migration_guide.md ) .
316
+
176
317
## Documentation
177
318
178
319
See our full documentation [ here] ( https://docs.stagehand.dev/ ) .
@@ -219,8 +360,21 @@ cd stagehand-python
219
360
220
361
# Install in editable mode with development dependencies
221
362
pip install -r requirements.txt
363
+
364
+ # On Windows, you may need to install Playwright browsers
365
+ playwright install
222
366
```
223
367
368
+ ### Dependencies
369
+
370
+ Stagehand has minimal dependencies and no longer requires external browser services:
371
+
372
+ - ** Core** : ` playwright ` , ` pydantic ` , ` python-dotenv `
373
+ - ** LLM Support** : ` openai ` , ` anthropic ` , ` litellm `
374
+ - ** Utilities** : ` rich ` , ` nest-asyncio `
375
+
376
+ All dependencies are automatically installed with ` pip install stagehand ` .
377
+
224
378
225
379
## License
226
380
0 commit comments