Skip to content

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

Fix ExtendedOutputModel schema validation by adding model_rebuild()

Cursor Agentbf9ca5baade2
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index c0983479d..fdbccc7ae 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -82,6 +82,9 @@ class Controller(Generic[Context]):
class ExtendedOutputModel(BaseModel): # type: ignore
success: bool = True
data: output_model # type: ignore
+
+ # Rebuild the model to resolve any forward references
+ ExtendedOutputModel.model_rebuild()
@self.registry.action(
'Complete task - with return text and if the task is finished (success=True) or not yet completely finished (success=False), because last step is reached',
diff --git a/fix_summary.md b/fix_summary.md
new file mode 100644
index 000000000..82a7d24ad
--- /dev/null
+++ b/fix_summary.md
@@ -0,0 +1,52 @@
+# ExtendedOutputModel Schema Fix Summary
+
+## Issue
+The user encountered an error: `ExtendedOutputModel is not fully defined; you should define Optional, then call ExtendedOutputModel.model_rebuild()` when using custom output schemas in the eval/service.py.
+
+The error occurred when passing a JSON schema like:
+```json
+{
+ "type": "object",
+ "properties": {
+ "website": { "type": "string" },
+ "part": { "type": "string" },
+ "part_url": { "type": [ "string", "null" ] },
+ "price": { "type": [ "number", "null" ] },
+ "error": { "type": [ "string", "null" ] }
+ },
+ "required": [ "website", "part" ]
+}
+```
+
+## Root Cause
+In `browser_use/controller/service.py`, the `ExtendedOutputModel` class was dynamically created with a forward reference annotation (`data: output_model`) that Pydantic v2 couldn't resolve properly without rebuilding the model.
+
+## Solution
+Added `ExtendedOutputModel.model_rebuild()` after the dynamic model creation in the Controller's `__init__` method.
+
+### Code Change
+**File:** `browser_use/controller/service.py`
+
+```python
+# Before (lines 81-83):
+class ExtendedOutputModel(BaseModel): # type: ignore
+ success: bool = True
+ data: output_model # type: ignore
+
+# After (lines 81-86):
+class ExtendedOutputModel(BaseModel): # type: ignore
+ success: bool = True
+ data: output_model # type: ignore
+
+# Rebuild the model to resolve any forward references
+ExtendedOutputModel.model_rebuild()
+```
+
+## Impact
+- ✅ Fixes the schema validation error when using custom output models
+- ✅ Preserves existing functionality for regular (non-structured) outputs
+- ✅ No breaking changes to the API
+- ✅ Validated with test schema matching user's use case
+
+## UI Issue
+The user also mentioned the UI shows "No detailed final response recorded". This is likely a separate frontend issue unrelated to the schema fix, as the final results should still flow through `history.final_result()` which returns the `extracted_content` from the last action result. The schema fix ensures the JSON output is properly generated and stored.
\ No newline at end of file