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
Improved naming of output history
magmuellercd688d0b6e05
diff --git a/browser_use/agent/tests.py b/browser_use/agent/tests.pyindex f3ec0636d..e6af0cb4b 100644--- a/browser_use/agent/tests.py+++ b/browser_use/agent/tests.py@@ -130,13 +130,13 @@ def sample_history(action_registry):def test_last_model_output(sample_history: AgentHistoryList):- last_output = sample_history.last_model_output()+ last_output = sample_history.last_action()print(last_output)assert last_output == {'done': {'text': 'Task completed'}}def test_get_errors(sample_history: AgentHistoryList):- errors = sample_history.get_errors()+ errors = sample_history.errors()assert len(errors) == 1assert errors[0] == 'Failed to extract completely'@@ -149,21 +149,20 @@ def test_is_done(sample_history: AgentHistoryList):assert sample_history.is_done() == True-def test_unique_urls(sample_history: AgentHistoryList):- urls = sample_history.unique_urls()- assert len(urls) == 2+def test_urls(sample_history: AgentHistoryList):+ urls = sample_history.urls()assert 'https://example.com' in urlsassert 'https://example.com/page2' in urlsdef test_all_screenshots(sample_history: AgentHistoryList):- screenshots = sample_history.all_screenshots()+ screenshots = sample_history.screenshots()assert len(screenshots) == 3assert screenshots == ['screenshot1.png', 'screenshot2.png', 'screenshot3.png']def test_all_model_outputs(sample_history: AgentHistoryList):- outputs = sample_history.all_model_outputs()+ outputs = sample_history.model_actions()assert len(outputs) == 3assert outputs[0] == {'click_element': {'index': 1, 'xpath': '//button[1]', 'num_clicks': 1}}assert outputs[1] == {'extract_page_content': {'value': 'text'}}@@ -171,17 +170,17 @@ def test_all_model_outputs(sample_history: AgentHistoryList):def test_all_model_outputs_filtered(sample_history: AgentHistoryList):- filtered = sample_history.all_model_outputs_filtered(include=['click_element'])+ filtered = sample_history.model_actions_filtered(include=['click_element'])assert len(filtered) == 1assert filtered[0]['click_element']['index'] == 1def test_empty_history():empty_history = AgentHistoryList(history=[])- assert empty_history.last_model_output() is None+ assert empty_history.last_action() is Noneassert empty_history.final_result() is Noneassert empty_history.is_done() == False- assert len(empty_history.unique_urls()) == 0+ assert len(empty_history.urls()) == 0# Add a test to verify action creationdiff --git a/browser_use/agent/views.py b/browser_use/agent/views.pyindex 6a2ec8494..c329398d8 100644--- a/browser_use/agent/views.py+++ b/browser_use/agent/views.py@@ -98,19 +98,19 @@ class AgentHistoryList(BaseModel):def __str__(self) -> str:"""Representation of the AgentHistoryList object"""- return f'AgentHistoryList(all_results={self.all_results()}, all_model_outputs={self.all_model_outputs()})'+ return f'AgentHistoryList(all_results={self.action_results()}, all_model_outputs={self.model_actions()})'def __repr__(self) -> str:"""Representation of the AgentHistoryList object"""return self.__str__()- def last_model_output(self) -> None | dict:+ def last_action(self) -> None | dict:"""Last action in history"""if self.history and self.history[-1].model_output:return self.history[-1].model_output.action.model_dump(exclude_none=True)return None- def get_errors(self) -> list[str]:+ def errors(self) -> list[str]:"""Get all errors from history"""return [h.result.error for h in self.history if h.result.error]@@ -128,22 +128,30 @@ class AgentHistoryList(BaseModel):def has_errors(self) -> bool:"""Check if the agent has any errors"""- return len(self.get_errors()) > 0+ return len(self.errors()) > 0- def unique_urls(self) -> list[str]:+ def urls(self) -> list[str]:"""Get all unique URLs from history"""- return list(set([h.state.url for h in self.history if h.state.url]))+ return [h.state.url for h in self.history if h.state.url]- def all_screenshots(self) -> list[str]:+ def screenshots(self) -> list[str]:"""Get all screenshots from history"""return [h.state.screenshot for h in self.history if h.state.screenshot]- def all_model_output_action_names(self) -> list[str]:+ def action_names(self) -> list[str]:"""Get all action names from history"""- return [list(action.keys())[0] for action in self.all_model_outputs()]+ return [list(action.keys())[0] for action in self.model_actions()]++ def model_thoughts(self) -> list[AgentBrain]:+ """Get all thoughts from history"""+ return [h.model_output.current_state for h in self.history if h.model_output]++ def model_outputs(self) -> list[AgentOutput]:+ """Get all model outputs from history"""+ return [h.model_output for h in self.history if h.model_output]# get all actions with params- def all_model_outputs(self) -> list[dict]:+ def model_actions(self) -> list[dict]:"""Get all actions from history"""outputs = []for h in self.history:@@ -163,17 +171,17 @@ class AgentHistoryList(BaseModel):outputs.append(output)return outputs- def all_results(self) -> list[dict]:+ def action_results(self) -> list[ActionResult]:"""Get all results from history"""- return [h.result.model_dump(exclude_none=True) for h in self.history if h.result]+ return [h.result for h in self.history if h.result]- def all_extracted_content(self) -> list[str]:+ def extracted_content(self) -> list[str]:"""Get all extracted content from history"""return [h.result.extracted_content for h in self.history if h.result.extracted_content]- def all_model_outputs_filtered(self, include: list[str] = []) -> list[dict]:- """Get all model outputs from history as JSON"""- outputs = self.all_model_outputs()+ def model_actions_filtered(self, include: list[str] = []) -> list[dict]:+ """Get all model actions from history as JSON"""+ outputs = self.model_actions()result = []for o in outputs:for i in include:diff --git a/examples/agent_output_processing.py b/examples/agent_output_processing.pyindex e59a8dc53..6ccbbb78b 100644--- a/examples/agent_output_processing.py+++ b/examples/agent_output_processing.py@@ -29,11 +29,11 @@ async def main():pprint(history.final_result(), indent=4)print('\nErrors:')- pprint(history.get_errors(), indent=4)+ pprint(history.errors(), indent=4)# e.g. xPaths the model clicked onprint('\nModel Outputs:')- pprint(history.all_model_outputs(), indent=4)+ pprint(history.model_actions(), indent=4)if __name__ == '__main__':diff --git a/tests/test_core_functionality.py b/tests/test_core_functionality.pyindex e9fbc176e..d693a7cc9 100644--- a/tests/test_core_functionality.py+++ b/tests/test_core_functionality.py@@ -34,7 +34,7 @@ async def test_search_google(llm, controller):controller=controller,)history: AgentHistoryList = await agent.run(max_steps=2)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'search_google' in action_names@@ -47,7 +47,7 @@ async def test_go_to_url(llm, controller):controller=controller,)history = await agent.run(max_steps=2)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_names@@ -60,7 +60,7 @@ async def test_go_back(llm, controller):controller=controller,)history = await agent.run(max_steps=3)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_namesassert 'go_back' in action_names@@ -74,7 +74,7 @@ async def test_click_element(llm, controller):controller=controller,)history = await agent.run(max_steps=4)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_namesassert 'click_element' in action_names@@ -88,7 +88,7 @@ async def test_input_text(llm, controller):controller=controller,)history = await agent.run(max_steps=4)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_namesassert 'input_text' in action_names@@ -102,7 +102,7 @@ async def test_switch_tab(llm, controller):controller=controller,)history = await agent.run(max_steps=6)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()open_tab_count = action_names.count('open_tab')assert open_tab_count >= 2assert 'switch_tab' in action_names@@ -117,7 +117,7 @@ async def test_open_new_tab(llm, controller):controller=controller,)history = await agent.run(max_steps=3)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'open_tab' in action_names@@ -130,7 +130,7 @@ async def test_extract_page_content(llm, controller):controller=controller,)history = await agent.run(max_steps=3)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_namesassert 'extract_content' in action_names@@ -146,7 +146,7 @@ async def test_done_action(llm, controller):)history = await agent.run(max_steps=3)- action_names = history.all_model_output_action_names()+ action_names = history.action_names()assert 'go_to_url' in action_namesassert 'done' in action_names@@ -177,7 +177,7 @@ async def test_scroll_down(llm, controller: Controller):# Validate that the 'scroll_down' action was executedhistory = agent.history- model_output = history.all_model_outputs()+ model_output = history.model_actions()if model_output:keys = [key for key in model_output[0].keys()]assert 'scroll_down' in keys