Skip to content

Commit 8afada1

Browse files
committed
Added x_sdk_version headers + resolved api path
1 parent 48115fb commit 8afada1

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

examples/act_example.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Example demonstrating calling act() with a string instruction.
3+
4+
This example shows how to use the act() method with a natural language
5+
string instruction instead of an Action object from observe().
6+
7+
The act() method accepts either:
8+
1. A string instruction (demonstrated here): input="click the button"
9+
2. An Action object from observe(): input=action_object
10+
11+
Required environment variables:
12+
- BROWSERBASE_API_KEY: Your Browserbase API key
13+
- BROWSERBASE_PROJECT_ID: Your Browserbase project ID
14+
- MODEL_API_KEY: Your OpenAI API key
15+
"""
16+
17+
import os
18+
19+
from stagehand import Stagehand
20+
21+
22+
def main() -> None:
23+
# Create client using environment variables
24+
# BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY
25+
client = Stagehand(
26+
browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"),
27+
browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"),
28+
model_api_key=os.environ.get("MODEL_API_KEY"),
29+
)
30+
31+
# Start a new browser session
32+
start_response = client.sessions.start(
33+
model_name="openai/gpt-5-nano",
34+
)
35+
36+
session_id = start_response.data.session_id
37+
print(f"Session started: {session_id}")
38+
39+
try:
40+
# Navigate to example.com
41+
client.sessions.navigate(
42+
id=session_id,
43+
url="https://www.example.com",
44+
frame_id="", # Empty string for main frame
45+
)
46+
print("Navigated to example.com")
47+
48+
# Call act() with a string instruction directly
49+
# This is the key test - passing a string instead of an Action object
50+
print("\nAttempting to call act() with string input...")
51+
act_response = client.sessions.act(
52+
id=session_id,
53+
input="click the 'More information' link", # String instruction
54+
)
55+
56+
print(f"Act completed successfully!")
57+
print(f"Result: {act_response.data.result.message}")
58+
print(f"Success: {act_response.data.result.success}")
59+
60+
except Exception as e:
61+
print(f"Error: {e}")
62+
print(f"Error type: {type(e).__name__}")
63+
import traceback
64+
traceback.print_exc()
65+
66+
finally:
67+
# End the session to clean up resources
68+
client.sessions.end(
69+
id=session_id,
70+
)
71+
print("\nSession ended")
72+
73+
74+
if __name__ == "__main__":
75+
main()

examples/full_example.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323

2424
def main() -> None:
25-
# SDK version for API compatibility (matches TypeScript SDK v3)
26-
SDK_VERSION = "3.0.6"
27-
2825
# Create client using environment variables
2926
# BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY
3027
client = Stagehand(
@@ -36,8 +33,6 @@ def main() -> None:
3633
# Start a new browser session
3734
start_response = client.sessions.start(
3835
model_name="openai/gpt-5-nano",
39-
x_language="typescript",
40-
x_sdk_version=SDK_VERSION,
4136
)
4237

4338
session_id = start_response.data.session_id
@@ -49,17 +44,13 @@ def main() -> None:
4944
id=session_id,
5045
url="https://news.ycombinator.com",
5146
frame_id="", # Empty string for main frame
52-
x_language="typescript",
53-
x_sdk_version=SDK_VERSION,
5447
)
5548
print("Navigated to Hacker News")
5649

5750
# Observe to find possible actions - looking for the comments link
5851
observe_response = client.sessions.observe(
5952
id=session_id,
6053
instruction="find the link to view comments for the top post",
61-
x_language="typescript",
62-
x_sdk_version=SDK_VERSION,
6354
)
6455

6556
results = observe_response.data.result
@@ -77,8 +68,6 @@ def main() -> None:
7768
act_response = client.sessions.act(
7869
id=session_id,
7970
input=result, # type: ignore[arg-type]
80-
x_language="typescript",
81-
x_sdk_version=SDK_VERSION,
8271
)
8372
print(f"Act completed: {act_response.data.result.message}")
8473

@@ -101,8 +90,6 @@ def main() -> None:
10190
},
10291
"required": ["commentText"]
10392
},
104-
x_language="typescript",
105-
x_sdk_version=SDK_VERSION,
10693
)
10794

10895
# Get the extracted result
@@ -133,8 +120,6 @@ def main() -> None:
133120
},
134121
"cua": False,
135122
},
136-
x_language="typescript",
137-
x_sdk_version=SDK_VERSION,
138123
timeout=300.0, # 5 minutes
139124
)
140125

@@ -146,8 +131,6 @@ def main() -> None:
146131
# End the session to clean up resources
147132
client.sessions.end(
148133
id=session_id,
149-
x_language="typescript",
150-
x_sdk_version=SDK_VERSION,
151134
)
152135
print("Session ended")
153136

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "stagehand-alpha"
3-
version = "0.2.3"
3+
version = "1.0.0"
44
description = "The official Python library for the stagehand API"
55
dynamic = ["readme"]
66
license = "Apache-2.0"

src/stagehand/_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(
149149
if base_url is None:
150150
base_url = os.environ.get("STAGEHAND_BASE_URL")
151151
if base_url is None:
152-
base_url = f"https://api.stagehand.browserbase.com/v1"
152+
base_url = f"https://api.stagehand.browserbase.com"
153153

154154
super().__init__(
155155
version=__version__,
@@ -223,6 +223,7 @@ def default_headers(self) -> dict[str, str | Omit]:
223223
return {
224224
**super().default_headers,
225225
"x-language": "python",
226+
"x-sdk-version": __version__,
226227
"X-Stainless-Async": "false",
227228
**self._custom_headers,
228229
}
@@ -438,7 +439,7 @@ def __init__(
438439
if base_url is None:
439440
base_url = os.environ.get("STAGEHAND_BASE_URL")
440441
if base_url is None:
441-
base_url = f"https://api.stagehand.browserbase.com/v1"
442+
base_url = f"https://api.stagehand.browserbase.com"
442443

443444
super().__init__(
444445
version=__version__,
@@ -512,6 +513,7 @@ def default_headers(self) -> dict[str, str | Omit]:
512513
return {
513514
**super().default_headers,
514515
"x-language": "python",
516+
"x-sdk-version": __version__,
515517
"X-Stainless-Async": f"async:{get_async_library()}",
516518
**self._custom_headers,
517519
}

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)