Skip to content

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

Add last step warning and enhance action result tracking

Magnus Müllerc07b5c9a94ac
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index f4cd25fd5..6425ad2d4 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -310,6 +310,11 @@ class Agent(Generic[Context]):
await self._raise_if_stopped_or_paused()
+ # Add last step warning if needed
+ if step_info and step_info.is_last_step():
+ msg = 'Done: This is your last step. You have to use the done action now! Please provide your best result of the ultimate task given your findings inside the done action.'
+ self._message_manager._add_message_with_tokens(HumanMessage(content=msg))
+
self._message_manager.add_state_message(state, self.state.last_result, step_info, self.settings.use_vision)
# Run planner at specified intervals if planner is configured
@@ -554,7 +559,8 @@ class Agent(Generic[Context]):
if self.state.stopped: # Allow stopping while paused
break
- await self.step()
+ step_info = AgentStepInfo(step_number=step, max_steps=max_steps)
+ await self.step(step_info)
if self.state.history.is_done():
if self.settings.validate_output and step < max_steps - 1:
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 741719c11..af35710ed 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -83,11 +83,16 @@ class AgentStepInfo:
step_number: int
max_steps: int
+ def is_last_step(self) -> bool:
+ """Check if this is the last step"""
+ return self.step_number >= self.max_steps - 1
+
class ActionResult(BaseModel):
"""Result of executing an action"""
is_done: Optional[bool] = False
+ success: Optional[bool] = True # Default to True for backward compatibility
extracted_content: Optional[str] = None
error: Optional[str] = None
include_in_memory: bool = False # whether to include in past messages as context or not
@@ -279,21 +284,29 @@ class AgentHistoryList(BaseModel):
def is_done(self) -> bool:
"""Check if the agent is done"""
- if self.history and len(self.history[-1].result) > 0 and self.history[-1].result[-1].is_done:
- return self.history[-1].result[-1].is_done
+ if self.history and len(self.history[-1].result) > 0:
+ last_result = self.history[-1].result[-1]
+ return last_result.is_done is True
+ return False
+
+ def is_successful(self) -> bool:
+ """Check if the agent completed successfully"""
+ if self.history and len(self.history[-1].result) > 0:
+ last_result = self.history[-1].result[-1]
+ return last_result.is_done is True and last_result.success is True
return False
def has_errors(self) -> bool:
"""Check if the agent has any non-None errors"""
return any(error is not None for error in self.errors())
- def urls(self) -> list[str]:
+ def urls(self) -> list[str | None]:
"""Get all unique URLs from history"""
- return [h.state.url for h in self.history if h.state.url]
+ return [h.state.url if h.state.url is not None else None for h in self.history]
- def screenshots(self) -> list[str]:
+ def screenshots(self) -> list[str | None]:
"""Get all screenshots from history"""
- return [h.state.screenshot for h in self.history if h.state.screenshot]
+ return [h.state.screenshot if h.state.screenshot is not None else None for h in self.history]
def action_names(self) -> list[str]:
"""Get all action names from history"""
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index e339f384a..de560c9bc 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -43,15 +43,20 @@ class Controller(Generic[Context]):
"""Register all default browser actions"""
if output_model is not None:
-
- @self.registry.action('Complete task', param_model=output_model)
- async def done(params: BaseModel):
- return ActionResult(is_done=True, extracted_content=params.model_dump_json())
+ # Create a new model that extends the output model with success parameter
+ class ExtendedOutputModel(output_model): # type: ignore
+ success: bool = True
+
+ @self.registry.action('Complete task', param_model=ExtendedOutputModel)
+ async def done(params: ExtendedOutputModel):
+ # Exclude success from the output JSON since it's an internal parameter
+ output_dict = params.model_dump(exclude={'success'})
+ return ActionResult(is_done=True, success=params.success, extracted_content=json.dumps(output_dict))
else:
@self.registry.action('Complete task', param_model=DoneAction)
async def done(params: DoneAction):
- return ActionResult(is_done=True, extracted_content=params.text)
+ return ActionResult(is_done=True, success=params.success, extracted_content=params.text)
# Basic Navigation Actions
@self.registry.action(
diff --git a/browser_use/controller/views.py b/browser_use/controller/views.py
index c4a89d289..82995c9e3 100644
--- a/browser_use/controller/views.py
+++ b/browser_use/controller/views.py
@@ -25,6 +25,7 @@ class InputTextAction(BaseModel):
class DoneAction(BaseModel):
text: str
+ success: bool
class SwitchTabAction(BaseModel):
@@ -42,9 +43,11 @@ class ScrollAction(BaseModel):
class SendKeysAction(BaseModel):
keys: str
+
class ExtractPageContentAction(BaseModel):
value: str
-
+
+
class NoParamsAction(BaseModel):
"""
Accepts absolutely anything in the incoming data