Skip to content

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

loaders for AgentHistoryModel from dict

Gregor Žuničbc3fd6a2dc33
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 7a92906d4..41b0c718c 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -442,10 +442,7 @@ class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
}
@classmethod
- def load_from_file(cls, filepath: str | Path, output_model: type[AgentOutput]) -> AgentHistoryList:
- """Load history from JSON file"""
- with open(filepath, encoding='utf-8') as f:
- data = json.load(f)
+ def load_from_dict(cls, data: dict[str, Any], output_model: type[AgentOutput]) -> AgentHistoryList:
# loop through history and validate output_model actions to enrich with custom actions
for h in data['history']:
if h['model_output']:
@@ -455,9 +452,17 @@ class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
h['model_output'] = None
if 'interacted_element' not in h['state']:
h['state']['interacted_element'] = None
+
history = cls.model_validate(data)
return history
+ @classmethod
+ def load_from_file(cls, filepath: str | Path, output_model: type[AgentOutput]) -> AgentHistoryList:
+ """Load history from JSON file"""
+ with open(filepath, encoding='utf-8') as f:
+ data = json.load(f)
+ return cls.load_from_dict(data, output_model)
+
def last_action(self) -> None | dict:
"""Last action in history"""
if self.history and self.history[-1].model_output: