Skip to content

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

fix(agent): guard last_action() against empty action list

itxaiohangloverd41448e485ca
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index dbec9a534..4dde414b3 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -701,7 +701,9 @@ class AgentHistoryList(BaseModel, Generic[AgentStructuredOutput]):
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[-1].model_dump(exclude_none=True, mode='json')
+ actions = self.history[-1].model_output.action
+ if actions:
+ return actions[-1].model_dump(exclude_none=True, mode='json')
return None
def errors(self) -> list[str | None]: