Skip to content

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

Ensures JSON serializability of task results

Magnus Müller4a26f07c66d0
diff --git a/eval/service.py b/eval/service.py
index 4632cc48c..8d2d94b38 100644
--- a/eval/service.py
+++ b/eval/service.py
@@ -610,7 +610,8 @@ class TaskResult:
}
)
- return payload
+ # Ensure all data in payload is JSON serializable
+ return make_json_serializable(payload)
def get_local_status(self) -> dict[str, Any]:
"""Get local status summary"""
@@ -885,6 +886,27 @@ def clean_action_dict(action_dict: dict) -> dict:
return {k: clean_action_dict(v) if isinstance(v, dict) else v for k, v in action_dict.items() if v is not None}
+def make_json_serializable(obj):
+ """
+ Convert objects to JSON-serializable types.
+ Handles common non-serializable types like enums, custom objects, etc.
+ """
+ if obj is None:
+ return None
+ elif isinstance(obj, (str, int, float, bool)):
+ return obj
+ elif isinstance(obj, dict):
+ return {k: make_json_serializable(v) for k, v in obj.items()}
+ elif isinstance(obj, (list, tuple)):
+ return [make_json_serializable(item) for item in obj]
+ elif hasattr(obj, 'value'): # Handle enums
+ return obj.value
+ elif hasattr(obj, '__dict__'): # Handle custom objects
+ return str(obj)
+ else:
+ return str(obj)
+
+
async def reformat_agent_history(
agent_history: AgentHistoryList,
task_id: str,