Skip to content

Commit a1e475a

Browse files
committed
stash
1 parent 1aadcc7 commit a1e475a

File tree

17 files changed

+876
-0
lines changed

17 files changed

+876
-0
lines changed

.fernignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Specify files that shouldn't be modified by Fern
2+
.gitignore
3+
.vscode/
4+
5+
src/browser_use/client.py
6+
src/browser_use/wrapper/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
__pycache__/
44
dist/
55
poetry.toml
6+
7+
.env*
8+
!.env.example

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.analysis.importFormat": "relative",
3+
"editor.codeActionsOnSave": {
4+
"source.organizeImports": "always",
5+
"source.fixAll.ruff": "always"
6+
}
7+
}

assets/cloud-banner-python.png

33.9 KB
Loading

examples/async_create.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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() -> None:
16+
res = await client.tasks.create(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
""",
20+
agent_settings={"llm": "gemini-2.5-flash"},
21+
)
22+
23+
print(f"Regular Task ID: {res.id}")
24+
25+
26+
# Structured Output
27+
async def create_structured_task() -> None:
28+
class HackerNewsPost(BaseModel):
29+
title: str
30+
url: str
31+
32+
class SearchResult(BaseModel):
33+
posts: List[HackerNewsPost]
34+
35+
res = await client.tasks.create(
36+
task="""
37+
Find top 10 Hacker News articles and return the title and url.
38+
""",
39+
agent_settings={"llm": "gpt-4.1"},
40+
structured_output_json=SearchResult,
41+
)
42+
43+
print(f"Structured Task ID: {res.id}")
44+
45+
46+
# Main
47+
48+
49+
async def main() -> None:
50+
await asyncio.gather(
51+
#
52+
create_regular_task(),
53+
create_structured_task(),
54+
)
55+
56+
57+
asyncio.run(main())

examples/async_retrieve.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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() -> None:
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+
agent_settings={"llm": "gemini-2.5-flash"},
27+
)
28+
29+
print(f"Regular Task ID: {regular_task.id}")
30+
31+
while True:
32+
regular_status = await client.tasks.retrieve(regular_task.id)
33+
print(f"Regular Task Status: {regular_status.status}")
34+
if regular_status.status == "finished":
35+
print(f"Regular Task Output: {regular_status.done_output}")
36+
break
37+
38+
await asyncio.sleep(1)
39+
40+
print("Done")
41+
42+
43+
async def retrieve_structured_task() -> None:
44+
"""
45+
Retrieves a structured task and waits for it to finish.
46+
"""
47+
48+
print("Retrieving structured task...")
49+
50+
# Structured Output
51+
class HackerNewsPost(BaseModel):
52+
title: str
53+
url: str
54+
55+
class SearchResult(BaseModel):
56+
posts: List[HackerNewsPost]
57+
58+
structured_task = await client.tasks.create(
59+
task="""
60+
Find top 10 Hacker News articles and return the title and url.
61+
""",
62+
agent_settings={"llm": "gpt-4.1"},
63+
structured_output_json=SearchResult,
64+
)
65+
66+
print(f"Structured Task ID: {structured_task.id}")
67+
68+
while True:
69+
structured_status = await client.tasks.retrieve(task_id=structured_task.id, structured_output_json=SearchResult)
70+
print(f"Structured Task Status: {structured_status.status}")
71+
72+
if structured_status.status == "finished":
73+
if structured_status.parsed_output is None:
74+
print("Structured Task No output")
75+
else:
76+
for post in structured_status.parsed_output.posts:
77+
print(f" - {post.title} - {post.url}")
78+
79+
break
80+
81+
await asyncio.sleep(1)
82+
83+
print("Done")
84+
85+
86+
# Main
87+
88+
89+
async def main() -> None:
90+
await asyncio.gather(
91+
#
92+
retrieve_regular_task(),
93+
retrieve_structured_task(),
94+
)
95+
96+
97+
asyncio.run(main())

examples/async_run.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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() -> None:
16+
regular_result = await client.tasks.run(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
""",
20+
agent_settings={"llm": "gemini-2.5-flash"},
21+
)
22+
23+
print(f"Regular Task ID: {regular_result.id}")
24+
25+
print(f"Regular Task Output: {regular_result.done_output}")
26+
27+
print("Done")
28+
29+
30+
# Structured Output
31+
async def run_structured_task() -> None:
32+
class HackerNewsPost(BaseModel):
33+
title: str
34+
url: str
35+
36+
class SearchResult(BaseModel):
37+
posts: List[HackerNewsPost]
38+
39+
structured_result = await client.tasks.run(
40+
task="""
41+
Find top 10 Hacker News articles and return the title and url.
42+
""",
43+
agent_settings={"llm": "gpt-4.1"},
44+
structured_output_json=SearchResult,
45+
)
46+
47+
print(f"Structured Task ID: {structured_result.id}")
48+
49+
if structured_result.parsed_output is not None:
50+
print("Structured Task Output:")
51+
for post in structured_result.parsed_output.posts:
52+
print(f" - {post.title} - {post.url}")
53+
54+
print("Structured Task Done")
55+
56+
57+
async def main() -> None:
58+
await asyncio.gather(
59+
#
60+
run_regular_task(),
61+
run_structured_task(),
62+
)
63+
64+
65+
asyncio.run(main())

examples/async_stream.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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() -> None:
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() -> None:
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+
agent_settings={"llm": "gpt-4.1"},
52+
structured_output_json=SearchResult,
53+
)
54+
55+
print(f"Structured Task ID: {structured_task.id}")
56+
57+
async for res in client.tasks.stream(structured_task.id, structured_output_json=SearchResult):
58+
print(f"Structured Task Status: {res.status}")
59+
60+
if res.status == "finished":
61+
if res.parsed_output is None:
62+
print("Structured Task No output")
63+
else:
64+
for post in res.parsed_output.posts:
65+
print(f" - Structured Task Post: {post.title} - {post.url}")
66+
break
67+
68+
print("Structured Task Done")
69+
70+
71+
# Main
72+
73+
74+
async def main() -> None:
75+
await asyncio.gather(
76+
#
77+
stream_regular_task(),
78+
stream_structured_task(),
79+
)
80+
81+
82+
asyncio.run(main())

examples/create.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
13+
# Regular Task
14+
def create_regular_task() -> None:
15+
res = client.tasks.create(
16+
task="""
17+
Find top 10 Hacker News articles and return the title and url.
18+
""",
19+
agent_settings={"llm": "gemini-2.5-flash"},
20+
)
21+
22+
print(res.id)
23+
24+
25+
create_regular_task()
26+
27+
28+
# Structured Output
29+
def create_structured_task() -> None:
30+
class HackerNewsPost(BaseModel):
31+
title: str
32+
url: str
33+
34+
class SearchResult(BaseModel):
35+
posts: List[HackerNewsPost]
36+
37+
res = client.tasks.create(
38+
task="""
39+
Find top 10 Hacker News articles and return the title and url.
40+
""",
41+
agent_settings={"llm": "gpt-4.1"},
42+
structured_output_json=SearchResult,
43+
)
44+
45+
print(res.id)
46+
47+
48+
create_structured_task()

0 commit comments

Comments
 (0)