Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Extract Rust structured output JSON
Magnus Müller31718b67163c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex eb988d411..bb70af530 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -115,10 +115,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai- Nested `result_file` payloads from the Rust core and flat legacy result-file fields are both supported.- Proof: `test_rust_history_exposes_result_file_attachments`.+22. Structured output JSON extraction+ - When `output_model_schema` is provided, fenced or prose-wrapped JSON in the Rust terminal final result is normalized before `AgentHistoryList.structured_output` is read.+ - Candidates are accepted only if they validate against the requested Pydantic schema.+ - Proof: `test_rust_history_extracts_fenced_structured_output`.+## Current Verification- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`-- `uv run pytest -q tests/ci/test_rust_agent.py` (31 tests)+- `uv run pytest -q tests/ci/test_rust_agent.py` (32 tests)- `cargo build -q -p browser-use-cli`- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`- Managed-headless end-to-end:diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex c0f28864b..7727e28ff 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -12,7 +12,7 @@ from datetime import datetimefrom pathlib import Pathfrom typing import Any, Generic, Literal-from pydantic import BaseModel+from pydantic import BaseModel, ValidationErrorfrom typing_extensions import TypeVarfrom uuid_extensions import uuid7str@@ -366,6 +366,43 @@ def _attachments_from_events(events: list[dict[str, Any]]) -> list[str] | None:return attachments or None+def _json_result_candidates(text: str) -> list[str]:+ candidates = [text.strip()]+ candidates.extend(match.group(1).strip() for match in re.finditer(r'```(?:json)?\s*(.*?)```', text, re.IGNORECASE | re.DOTALL))+ decoder = json.JSONDecoder()+ for index, char in enumerate(text):+ if char not in '{[':+ continue+ try:+ parsed, _end = decoder.raw_decode(text[index:])+ except json.JSONDecodeError:+ continue+ candidates.append(json.dumps(parsed))+ seen = set()+ unique = []+ for candidate in candidates:+ if not candidate or candidate in seen:+ continue+ seen.add(candidate)+ unique.append(candidate)+ return unique+++def _structured_result_text(+ result: str | None,+ output_model_schema: type[BaseModel] | None,+) -> str | None:+ if result is None or output_model_schema is None:+ return result+ for candidate in _json_result_candidates(result):+ try:+ output_model_schema.model_validate_json(candidate)+ except (ValidationError, ValueError):+ continue+ return candidate+ return result++def _failure_from_events(events: list[dict[str, Any]]) -> str | None:for event in reversed(events):if _event_type(event) == 'session.failed':@@ -455,7 +492,7 @@ def _history_from_events(output_model_schema: type[AgentStructuredOutput] | None,process_error: str | None,) -> AgentHistoryList[AgentStructuredOutput]:- final_result = _result_from_events(events)+ final_result = _structured_result_text(_result_from_events(events), output_model_schema)failure = process_error or _failure_from_events(events)if final_result is None and failure is None:failure = 'Rust terminal session did not produce a final result.'diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 3877279a7..8fbc64190 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -89,6 +89,26 @@ def test_rust_history_supports_structured_output():assert history.structured_output.answer == 'ok'+def test_rust_history_extracts_fenced_structured_output():+ from browser_use.rust.service import _history_from_events++ class Answer(BaseModel):+ answer: str++ history = _history_from_events(+ [{'event_type': 'session.done', 'payload': {'result': 'Here is the answer:\n```json\n{"answer": "ok"}\n```'}}],+ model='gpt-test',+ started=1.0,+ finished=2.0,+ output_model_schema=Answer,+ process_error=None,+ )++ assert history.final_result() == '{"answer": "ok"}'+ assert isinstance(history.structured_output, Answer)+ assert history.structured_output.answer == 'ok'++def test_rust_history_exposes_result_file_attachments():from browser_use.rust.service import _history_from_events