Skip to content

Commit 76b0aa1

Browse files
committed
fix: Fix Async Watch Method Constructor
1 parent a10e9b8 commit 76b0aa1

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

examples/async_watch.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env -S poetry run python
2+
3+
import asyncio
4+
from typing import List
5+
6+
from api import API_KEY
7+
from pydantic import BaseModel
8+
9+
from browser_use import AsyncBrowserUse
10+
11+
client = AsyncBrowserUse(api_key=API_KEY)
12+
13+
14+
# Regular Task
15+
async def watch_regular_task() -> None:
16+
task = await client.tasks.create_task(
17+
task="""
18+
Find top 10 Hacker News articles and return the title and url.
19+
""",
20+
llm="gemini-2.5-flash",
21+
)
22+
23+
print(f"Regular Task ID: {task.id}")
24+
25+
async for state in task.watch():
26+
print(f"Regular Task Status: {state.status}")
27+
28+
if state.status == "finished":
29+
print(f"Regular Task Output: {state.output}")
30+
break
31+
32+
print("Regular Task Done")
33+
34+
35+
# Structured Output
36+
async def watch_structured_task() -> None:
37+
class HackerNewsPost(BaseModel):
38+
title: str
39+
url: str
40+
41+
class SearchResult(BaseModel):
42+
posts: List[HackerNewsPost]
43+
44+
task = await client.tasks.create_task(
45+
task="""
46+
Find top 10 Hacker News articles and return the title and url.
47+
""",
48+
llm="gpt-4.1",
49+
schema=SearchResult,
50+
)
51+
52+
print(f"Structured Task ID: {task.id}")
53+
54+
async for state in task.watch():
55+
print(f"Structured Task Status: {state.status}")
56+
57+
if state.status == "finished":
58+
print(f"Structured Task Output: {state.output}")
59+
60+
for post in state.parsed_output.posts:
61+
print(f" - {post.title} - {post.url}")
62+
63+
break
64+
65+
66+
# Main
67+
68+
69+
async def main() -> None:
70+
await asyncio.gather(
71+
#
72+
watch_regular_task(),
73+
watch_structured_task(),
74+
)
75+
76+
77+
asyncio.run(main())

examples/watch.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env -S poetry run python
2+
3+
from typing import List
4+
5+
from api import API_KEY
6+
from pydantic import BaseModel
7+
8+
from browser_use import BrowserUse
9+
10+
client = BrowserUse(api_key=API_KEY)
11+
12+
13+
# Regular Task
14+
def watch_regular_task() -> None:
15+
task = client.tasks.create_task(
16+
task="""
17+
Find top 10 Hacker News articles and return the title and url.
18+
""",
19+
llm="gemini-2.5-flash",
20+
)
21+
22+
print(f"Task ID: {task.id}")
23+
24+
for state in task.watch():
25+
print(f"Regular Task Status: {state.status}")
26+
27+
if state.status == "finished":
28+
print(f"Regular Task Output: {state.output}")
29+
break
30+
31+
print("Regular: DONE")
32+
33+
34+
watch_regular_task()
35+
36+
37+
# Structured Output
38+
def watch_structured_task() -> None:
39+
class HackerNewsPost(BaseModel):
40+
title: str
41+
url: str
42+
43+
class SearchResult(BaseModel):
44+
posts: List[HackerNewsPost]
45+
46+
task = client.tasks.create_task(
47+
task="""
48+
Find top 10 Hacker News articles and return the title and url.
49+
""",
50+
llm="gpt-4.1",
51+
schema=SearchResult,
52+
)
53+
54+
print(f"Task ID: {task.id}")
55+
56+
for state in task.watch():
57+
print(f"Structured Task Status: {state.status}")
58+
59+
if state.status == "finished":
60+
print(f"Structured Task Output: {state.output}")
61+
62+
for post in state.parsed_output.posts:
63+
print(f" - {post.title} - {post.url}")
64+
65+
break
66+
67+
print("Structured Task Done")
68+
69+
70+
watch_structured_task()

src/browser_use/wrapper/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def stream(
198198
"""Streams the steps of the task and closes when the task is finished."""
199199
return _async_stream(self._client, self.id, interval, request_options)
200200

201-
async def watch(
201+
def watch(
202202
self, interval: float = 1, request_options: typing.Optional[RequestOptions] = None
203203
) -> AsyncIterator[TaskView]:
204204
"""Yields the latest task state on every change."""

0 commit comments

Comments
 (0)