Skip to content

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

Refine action parsing and validation in agent service

Magnus Müller58cd755b8f7b
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index bcbcf8fb5..363e062d8 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -519,8 +519,9 @@ class Agent(Generic[Context]):
if parsed is None:
raise ValueError('Could not parse response.')
- # cut the number of actions to max_actions_per_step
- parsed.action = parsed.action[: self.settings.max_actions_per_step]
+ # cut the number of actions to max_actions_per_step if needed
+ if len(parsed.action) > self.settings.max_actions_per_step:
+ parsed.action = parsed.action[: self.settings.max_actions_per_step]
log_response(parsed)
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 732ae4f6a..5897ae6fd 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -130,9 +130,9 @@ class AgentOutput(BaseModel):
current_state: AgentBrain
action: list[ActionModel] = Field(
- ..., # This means the field is required
+ ...,
description='List of actions to execute',
- min_items=1, # Ensure at least one action is provided
+ json_schema_extra={'min_items': 1}, # Ensure at least one action is provided
)
@staticmethod
@@ -141,7 +141,10 @@ class AgentOutput(BaseModel):
model_ = create_model(
'AgentOutput',
__base__=AgentOutput,
- action=(list[custom_actions], Field(..., description='List of actions to execute', min_items=1)),
+ action=(
+ list[custom_actions],
+ Field(..., description='List of actions to execute', json_schema_extra={'min_items': 1}),
+ ),
__module__=AgentOutput.__module__,
)
model_.__doc__ = 'AgentOutput model with custom actions'