Pass a JSON Schema and read the V4 result as parsed JSON.
Pass a JSON Schema as output_schema / outputSchema. When the run completes,
run.output holds the parsed JSON value and run.result still holds the text
answer.
SDK support for output_schema / outputSchema and run.output ships in the
first browser-use-sdk release after 3.11.3. On 3.11.3, use the REST API.
from browser_use_sdk.v4 import BrowserUsefrom pydantic import BaseModelclient = BrowserUse()class Story(BaseModel): title: str points: intrun = client.runs.create( "Find the top Hacker News story and its points.", output_schema=Story.model_json_schema(),)run = client.runs.wait_for_completion(run.id)if run.status.value != "completed": raise RuntimeError(run.error or run.status.value)story = Story.model_validate(run.output)
import { BrowserUse } from "browser-use-sdk/v4";import { z } from "zod";const client = new BrowserUse();const Story = z.object({ title: z.string(), points: z.number(),});const run = await client.runs.create({ task: "Find the top Hacker News story and its points.", outputSchema: z.toJSONSchema(Story),});const result = await client.runs.waitForCompletion(run.id);if (result.status !== "completed") throw new Error(result.error ?? result.status);const story = Story.parse(result.output);
curl https://api.browser-use.com/api/v4/runs \ -H "X-Browser-Use-API-Key: $BROWSER_USE_API_KEY" \ -H "Content-Type: application/json" \ -d '{"task":"Find the top Hacker News story and its points.", "outputSchema":{"type":"object", "properties":{"title":{"type":"string"},"points":{"type":"integer"}}, "required":["title","points"]}}'# Poll until "status" is "completed", "failed" or "cancelled".# Read "output" only when it is "completed"; otherwise read "error".curl https://api.browser-use.com/api/v4/runs/$RUN_ID \ -H "X-Browser-Use-API-Key: $BROWSER_USE_API_KEY"
Browser Use checks the agent’s final answer against the schema. If the agent
cannot produce a matching answer, the run fails with
Could not produce output matching outputSchema. Validate run.output in your
application as well, as the examples do.