Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Update Cloud Docs - Add sctructured output example

LarsenCundric4659ecd7a9a7
diff --git a/docs/cloud/implementation.mdx b/docs/cloud/implementation.mdx
index 8c8940177..1660d053c 100644
--- a/docs/cloud/implementation.mdx
+++ b/docs/cloud/implementation.mdx
@@ -101,6 +101,98 @@ def control_task():
print(f"Task completed with output: {result['output']}")
```
+## Structured Output Example
+
+Here's how to implement a task with structured JSON output:
+
+```python
+import json
+import os
+import time
+import requests
+from pydantic import BaseModel
+from typing import List
+
+
+API_KEY = os.getenv("API_KEY")
+BASE_URL = 'https://api.browser-use.com/api/v1'
+HEADERS = {
+ "Authorization": f"Bearer {API_KEY}",
+ "Content-Type": "application/json"
+}
+
+
+# Define output schema using Pydantic
+class SocialMediaCompany(BaseModel):
+ name: str
+ market_cap: float
+ headquarters: str
+ founded_year: int
+
+
+class SocialMediaCompanies(BaseModel):
+ companies: List[SocialMediaCompany]
+
+
+def create_structured_task(instructions: str, schema: dict):
+ """Create a task that expects structured output"""
+ payload = {
+ "task": instructions,
+ "structured_output_json": json.dumps(schema)
+ }
+ response = requests.post(f"{BASE_URL}/run-task", headers=HEADERS, json=payload)
+ response.raise_for_status()
+ return response.json()["id"]
+
+
+def wait_for_task_completion(task_id: str, poll_interval: int = 5):
+ """Poll task status until it completes"""
+ while True:
+ response = requests.get(f"{BASE_URL}/task/{task_id}/status", headers=HEADERS)
+ response.raise_for_status()
+ status = response.json()
+ if status == "finished":
+ break
+ elif status in ["failed", "stopped"]:
+ raise RuntimeError(f"Task {task_id} ended with status: {status}")
+ print("Waiting for task to finish...")
+ time.sleep(poll_interval)
+
+
+def fetch_task_output(task_id: str):
+ """Retrieve the final task result"""
+ response = requests.get(f"{BASE_URL}/task/{task_id}", headers=HEADERS)
+ response.raise_for_status()
+ return response.json()["output"]
+
+
+def main():
+ schema = SocialMediaCompanies.model_json_schema()
+ task_id = create_structured_task(
+ "Get me the top social media companies by market cap",
+ schema
+ )
+ print(f"Task created with ID: {task_id}")
+
+ wait_for_task_completion(task_id)
+ print("Task completed!")
+
+ output = fetch_task_output(task_id)
+ print("Raw output:", output)
+
+ try:
+ parsed = SocialMediaCompanies.model_validate_json(output)
+ print("Parsed output:")
+ print(parsed)
+ except Exception as e:
+ print(f"Failed to parse structured output: {e}")
+
+
+if __name__ == "__main__":
+ main()
+
+```
+
<Note>
Remember to handle your API key securely and implement proper error handling
in production code.
diff --git a/docs/cloud/quickstart.mdx b/docs/cloud/quickstart.mdx
index 68ef795f9..7e94238e6 100644
--- a/docs/cloud/quickstart.mdx
+++ b/docs/cloud/quickstart.mdx
@@ -18,8 +18,8 @@ The Browser Use Cloud API lets you create and manage browser automation agents p
The Browser Use Cloud API is priced at <b>$0.05 per step</b> that the agent executes.
<Note>
- Since Browser Use can execute multiple steps at the same time,
- the price for filling out forms is much lower than other services.
+ Since Browser Use can execute multiple steps at the same time, the price for
+ filling out forms is much lower than other services.
</Note>
## Creating Your First Agent