Skip to content

Commit 917c3a4

Browse files
committed
Add Async
1 parent c5c21ea commit 917c3a4

File tree

10 files changed

+782
-132
lines changed

10 files changed

+782
-132
lines changed

examples/async_create.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use_sdk import AsyncBrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = AsyncBrowserUse()
12+
13+
14+
# Regular Task
15+
async def create_regular_task():
16+
res = await client.tasks.create(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
"""
20+
)
21+
22+
print(f"Regular Task ID: {res.id}")
23+
24+
25+
# Structured Output
26+
async def create_structured_task():
27+
class HackerNewsPost(BaseModel):
28+
title: str
29+
url: str
30+
31+
class SearchResult(BaseModel):
32+
posts: List[HackerNewsPost]
33+
34+
res = await client.tasks.create(
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"Structured Task ID: {res.id}")
42+
43+
44+
# Main
45+
46+
47+
async def main():
48+
await asyncio.gather(
49+
#
50+
create_regular_task(),
51+
create_structured_task(),
52+
)
53+
54+
55+
asyncio.run(main())

examples/async_retrieve.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env -S rye run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from pydantic import BaseModel
7+
8+
from browser_use_sdk import AsyncBrowserUse
9+
10+
# gets API Key from environment variable BROWSER_USE_API_KEY
11+
client = AsyncBrowserUse()
12+
13+
14+
# Regular Task
15+
async def retrieve_regular_task():
16+
"""
17+
Retrieves a regular task and waits for it to finish.
18+
"""
19+
20+
print("Retrieving regular task...")
21+
22+
regular_task = await client.tasks.create(
23+
task="""
24+
Find top 10 Hacker News articles and return the title and url.
25+
"""
26+
)
27+
28+
print(f"Regular Task ID: {regular_task.id}")
29+
30+
while True:
31+
regular_status = await client.tasks.retrieve(regular_task.id)
32+
print(f"Regular Task Status: {regular_status.status}")
33+
if regular_status.status == "finished":
34+
print(f"Regular Task Output: {regular_status.done_output}")
35+
break
36+
37+
await asyncio.sleep(1)
38+
39+
print("Done")
40+
41+
42+
async def retrieve_structured_task():
43+
"""
44+
Retrieves a structured task and waits for it to finish.
45+
"""
46+
47+
print("Retrieving structured task...")
48+
49+
# Structured Output
50+
class HackerNewsPost(BaseModel):
51+
title: str
52+
url: str
53+
54+
class SearchResult(BaseModel):
55+
posts: List[HackerNewsPost]
56+
57+
structured_task = await client.tasks.create(
58+
task="""
59+
Find top 10 Hacker News articles and return the title and url.
60+
""",
61+
structured_output_json=SearchResult,
62+
)
63+
64+
print(f"Structured Task ID: {structured_task.id}")
65+
66+
while True:
67+
structured_status = await client.tasks.retrieve(task_id=structured_task.id, structured_output_json=SearchResult)
68+
print(f"Structured Task Status: {structured_status.status}")
69+
70+
if structured_status.status == "finished":
71+
if structured_status.parsed_output is None:
72+
print("Structured Task No output")
73+
else:
74+
for post in structured_status.parsed_output.posts:
75+
print(f" - {post.title} - {post.url}")
76+
77+
break
78+
79+
await asyncio.sleep(1)
80+
81+
print("Done")
82+
83+
84+
# Main
85+
86+
87+
async def main():
88+
await asyncio.gather(
89+
#
90+
retrieve_regular_task(),
91+
retrieve_structured_task(),
92+
)
93+
94+
95+
asyncio.run(main())

examples/async_run.py

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

examples/async_stream.py

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

examples/create.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,38 @@
99
# gets API Key from environment variable BROWSER_USE_API_KEY
1010
client = BrowserUse()
1111

12+
1213
# Regular Task
13-
res = client.tasks.create(
14-
task="""
15-
Find top 10 Hacker News articles and return the title and url.
16-
"""
17-
)
14+
def create_regular_task():
15+
res = client.tasks.create(
16+
task="""
17+
Find top 10 Hacker News articles and return the title and url.
18+
"""
19+
)
20+
21+
print(res.id)
22+
1823

19-
print(res.id)
24+
create_regular_task()
2025

2126

2227
# Structured Output
23-
class HackerNewsPost(BaseModel):
24-
title: str
25-
url: str
28+
def create_structured_task():
29+
class HackerNewsPost(BaseModel):
30+
title: str
31+
url: str
2632

33+
class SearchResult(BaseModel):
34+
posts: List[HackerNewsPost]
2735

28-
class SearchResult(BaseModel):
29-
posts: List[HackerNewsPost]
36+
res = client.tasks.create(
37+
task="""
38+
Find top 10 Hacker News articles and return the title and url.
39+
""",
40+
structured_output_json=SearchResult,
41+
)
3042

43+
print(res.id)
3144

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-
)
3845

39-
print(res.id)
46+
create_structured_task()

0 commit comments

Comments
 (0)