Skip to content

Commit a30f2a1

Browse files
committed
Add Sync Methods
1 parent 5a08993 commit a30f2a1

File tree

7 files changed

+542
-5
lines changed

7 files changed

+542
-5
lines changed

examples/create.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
from typing import List
4+
5+
from pydantic import BaseModel
6+
7+
from browser_use_sdk import BrowserUse
8+
9+
# gets API Key from environment variable BROWSER_USE_API_KEY
10+
client = BrowserUse()
11+
12+
# Regular Task
13+
res = client.tasks.create(
14+
task="""
15+
Find top 10 Hacker News articles and return the title and url.
16+
"""
17+
)
18+
19+
print(res.id)
20+
21+
22+
# Structured Output
23+
class HackerNewsPost(BaseModel):
24+
title: str
25+
url: str
26+
27+
28+
class SearchResult(BaseModel):
29+
posts: List[HackerNewsPost]
30+
31+
32+
res = client.tasks.create(
33+
task="""
34+
Find top 10 Hacker News articles and return the title and url.
35+
""",
36+
structured_output_json=SearchResult,
37+
)
38+
39+
print(res.id)

examples/retrieve.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import time
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use_sdk import BrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = BrowserUse()
12+
13+
# Regular Task
14+
task = client.tasks.create(
15+
task="""
16+
Find top 10 Hacker News articles and return the title and url.
17+
"""
18+
)
19+
20+
print(f"Task ID: {task.id}")
21+
22+
while True:
23+
regular = client.tasks.retrieve(task.id)
24+
print(regular.status)
25+
if regular.status == "finished":
26+
print(regular.done_output)
27+
break
28+
29+
time.sleep(1)
30+
31+
32+
# Structured Output
33+
class HackerNewsPost(BaseModel):
34+
title: str
35+
url: str
36+
37+
38+
class SearchResult(BaseModel):
39+
posts: List[HackerNewsPost]
40+
41+
42+
structured = client.tasks.create(
43+
task="""
44+
Find top 10 Hacker News articles and return the title and url.
45+
""",
46+
structured_output_json=SearchResult,
47+
)
48+
49+
print(f"Task ID: {structured.id}")
50+
51+
while True:
52+
structured = client.tasks.retrieve(task_id=structured.id, structured_output_json=SearchResult)
53+
print(structured.status)
54+
55+
if structured.status == "finished":
56+
if structured.parsed_output is None:
57+
print("No output")
58+
else:
59+
for post in structured.parsed_output.posts:
60+
print(f" - {post.title} - {post.url}")
61+
62+
break
63+
64+
time.sleep(1)

examples/run.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
from typing import List
4+
5+
from pydantic import BaseModel
6+
7+
from browser_use_sdk import BrowserUse
8+
9+
# gets API Key from environment variable BROWSER_USE_API_KEY
10+
client = BrowserUse()
11+
12+
# Regular Task
13+
regular_result = client.tasks.run(
14+
task="""
15+
Find top 10 Hacker News articles and return the title and url.
16+
"""
17+
)
18+
19+
print(f"Task ID: {regular_result.id}")
20+
21+
print(regular_result.done_output)
22+
23+
24+
# Structured Output
25+
class HackerNewsPost(BaseModel):
26+
title: str
27+
url: str
28+
29+
30+
class SearchResult(BaseModel):
31+
posts: List[HackerNewsPost]
32+
33+
34+
structured_result = client.tasks.run(
35+
task="""
36+
Find top 10 Hacker News articles and return the title and url.
37+
""",
38+
structured_output_json=SearchResult,
39+
)
40+
41+
print(f"Task ID: {structured_result.id}")
42+
43+
if structured_result.parsed_output is not None:
44+
for post in structured_result.parsed_output.posts:
45+
print(f" - {post.title} - {post.url}")

examples/stream.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
from typing import List
4+
5+
from pydantic import BaseModel
6+
7+
from browser_use_sdk import BrowserUse
8+
from browser_use_sdk.types.task_create_params import AgentSettings
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = BrowserUse()
12+
13+
# Regular Task
14+
regular_task = client.tasks.create(
15+
task="""
16+
Find top 10 Hacker News articles and return the title and url.
17+
""",
18+
agent_settings=AgentSettings(llm="gemini-2.5-flash"),
19+
)
20+
21+
print(f"Task ID: {regular_task.id}")
22+
23+
for res in client.tasks.stream(regular_task.id):
24+
print(res.status)
25+
26+
if len(res.steps) > 0:
27+
last_step = res.steps[-1]
28+
print(f"{last_step.url} ({last_step.next_goal})")
29+
for action in last_step.actions:
30+
print(f" - {action}")
31+
32+
33+
# Structured Output
34+
class HackerNewsPost(BaseModel):
35+
title: str
36+
url: str
37+
38+
39+
class SearchResult(BaseModel):
40+
posts: List[HackerNewsPost]
41+
42+
43+
structured_task = client.tasks.create(
44+
task="""
45+
Find top 10 Hacker News articles and return the title and url.
46+
""",
47+
structured_output_json=SearchResult,
48+
)
49+
50+
print(f"Task ID: {structured_task.id}")
51+
52+
for res in client.tasks.stream(structured_task.id, structured_output_json=SearchResult):
53+
print(res.status)
54+
55+
if res.status == "finished":
56+
if res.parsed_output is None:
57+
print("No output")
58+
else:
59+
for post in res.parsed_output.posts:
60+
print(f" - {post.title} - {post.url}")
61+
break

src/browser_use/lib/.keep

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/browser_use_sdk/lib/parse.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Union, Generic, TypeVar
2+
3+
from pydantic import BaseModel
4+
5+
from browser_use_sdk.types.task_view import TaskView
6+
7+
T = TypeVar("T", bound=BaseModel)
8+
9+
10+
class TaskViewWithOutput(TaskView, Generic[T]):
11+
"""
12+
TaskView with structured output.
13+
"""
14+
15+
parsed_output: Union[T, None]

0 commit comments

Comments
 (0)