from browser_use_sdk import BrowserUseclient = BrowserUse(api_key="bu_...")result = client.tasks.run( task="Search for the top 10 Hacker News posts and return the title and url.")result.done_output
Simply import AsyncBrowserUse instead of BrowserUse and use await with each API call:
Copy
Ask AI
import osimport asynciofrom browser_use_sdk import AsyncBrowserUseclient = AsyncBrowserUse( api_key=os.environ.get("BROWSER_USE_API_KEY"), # This is the default and can be omitted)async def main() -> None: task = await client.tasks.run( task="Search for the top 10 Hacker News posts and return the title and url.", ) print(task.done_output)asyncio.run(main())
Functionality between the synchronous and asynchronous clients is otherwise identical.
Browser Use Python SDK provides first class support for Pydantic models.
Copy
Ask AI
class HackerNewsPost(BaseModel): title: str url: strclass SearchResult(BaseModel): posts: List[HackerNewsPost]async def main() -> None: structured_result = await client.tasks.run( task=""" Find top 10 Hacker News articles and return the title and url. """, structured_output_json=SearchResult, ) if structured_result.parsed_output is not None: print("Top HackerNews Posts:") for post in structured_result.parsed_output.posts: print(f" - {post.title} - {post.url}")asyncio.run(main())
class HackerNewsPost(BaseModel): title: str url: strclass SearchResult(BaseModel): posts: List[HackerNewsPost]async def main() -> None: structured_task = await client.tasks.create( task=""" Find top 10 Hacker News articles and return the title and url. """, structured_output_json=SearchResult, ) async for update in client.tasks.stream(structured_task.id, structured_output_json=SearchResult): if len(update.steps) > 0: last_step = update.steps[-1] print(f"{update.status}: {last_step.url} ({last_step.next_goal})") else: print(f"{update.status}") if update.status == "finished": if update.parsed_output is None: print("No output...") else: print("Top HackerNews Posts:") for post in update.parsed_output.posts: print(f" - {post.title} - {post.url}") breakasyncio.run(main())