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
Log Rust empty model action retries
Magnus Müller226e9c891828
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.mdindex 46443987f..e7400c8b7 100644--- a/RUST_AGENT_INTEGRATION_FEATURES.md+++ b/RUST_AGENT_INTEGRATION_FEATURES.md@@ -971,6 +971,11 @@ Terminal core branch: `magnus/browser-use-rust-main-integration` at terminal mai- Step-0 history recording, long-term-memory annotation, and action replay behavior remain unchanged.- Proof: `test_rust_agent_exposes_action_replay_helper_methods`.+192. Rust Agent empty-model-action retry logging parity+ - Rust-backed `_get_model_output_with_retry(...)` now logs Browser Use's LLM response action count, empty-action retry warning, and safe-noop fallback warning.+ - The helper still retries once with the Browser Use clarification message and inserts the same failed `done` action when the retry also returns no usable action.+ - Proof: `test_rust_agent_exposes_model_output_helper_methods`.+## Current Verification- `python3 -m py_compile browser_use/agent/service.py browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`diff --git a/browser_use/rust/service.py b/browser_use/rust/service.pyindex b44dfdbb2..c19ac9932 100644--- a/browser_use/rust/service.py+++ b/browser_use/rust/service.py@@ -3426,6 +3426,8 @@ class Agent(Generic[Context, AgentStructuredOutput]):async def _get_model_output_with_retry(self, input_messages: list[BaseMessage]) -> AgentOutput:"""Get model output, retrying once when the model returns no usable action."""model_output = await self.get_model_output(input_messages)+ action_count = len(model_output.action) if getattr(model_output, 'action', None) else 0+ self.logger.debug(f'✅ Step {self.state.n_steps}: Got LLM response with {action_count} actions')def has_empty_actions(output: AgentOutput) -> bool:actions = getattr(output, 'action', None)@@ -3436,11 +3438,13 @@ class Agent(Generic[Context, AgentStructuredOutput]):if has_empty_actions(model_output):from browser_use.llm.messages import UserMessage+ self.logger.warning('Model returned empty action. Retrying...')clarification_message = UserMessage(content='You forgot to return an action. Please respond with a valid JSON action according to the expected schema with your assessment and next actions.')model_output = await self.get_model_output(input_messages + [clarification_message])if has_empty_actions(model_output):+ self.logger.warning('Model still returned empty after retry. Inserting safe noop action.')try:done_action = self.DoneActionModel(done={'success': False, 'text': 'No next action returned by LLM!'})except Exception:diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.pyindex 13b4a9805..73e1809ea 100644--- a/tests/ci/test_rust_agent.py+++ b/tests/ci/test_rust_agent.py@@ -5479,13 +5479,33 @@ async def test_rust_agent_exposes_action_replay_helper_methods(monkeypatch):) is None-async def test_rust_agent_exposes_model_output_helper_methods(tmp_path):+async def test_rust_agent_exposes_model_output_helper_methods(monkeypatch, tmp_path):from types import SimpleNamespacefrom browser_use.agent.views import ActionResultfrom browser_use.llm.messages import UserMessagefrom browser_use.rust import Agent+ class RecordingLogger:+ def __init__(self):+ self.debugs = []+ self.warnings = []++ def debug(self, message, *args, **kwargs):+ self.debugs.append(message)++ def warning(self, message, *args, **kwargs):+ self.warnings.append(message)++ def info(self, message, *args, **kwargs):+ pass++ def isEnabledFor(self, level):+ return True++ logger = RecordingLogger()+ monkeypatch.setattr(Agent, 'logger', property(lambda self: logger))+original_url = 'https://example.com/path?abcdefghijklmnopqrstuvwxyz#section'class LLM:@@ -5578,10 +5598,43 @@ async def test_rust_agent_exposes_model_output_helper_methods(tmp_path):retry_llm = RetryLLM()retry_agent = Agent(task='Retry LLM helpers.', llm=retry_llm)retry_llm.agent = retry_agent+ logger.debugs.clear()+ logger.warnings.clear()retry_output = await retry_agent._get_model_output_with_retry([UserMessage(content='Retry with an action.')])assert len(retry_llm.calls) == 2assert retry_output.action+ assert logger.debugs[0] == '✅ Step 1: Got LLM response with 0 actions'+ assert any('Next actions: done' in message for message in logger.debugs)+ assert logger.warnings == ['Model returned empty action. Retrying...']++ class EmptyRetryLLM(LLM):+ async def ainvoke(self, messages, output_format=None, **kwargs):+ self.calls.append((messages, output_format))+ return SimpleNamespace(+ usage=None,+ completion=self.agent.AgentOutput(+ evaluation_previous_goal='empty',+ memory='none',+ next_goal='retry',+ action=[],+ ),+ )++ empty_retry_llm = EmptyRetryLLM()+ empty_retry_agent = Agent(task='Retry empty LLM helpers.', llm=empty_retry_llm)+ empty_retry_llm.agent = empty_retry_agent+ logger.debugs.clear()+ logger.warnings.clear()+ empty_retry_output = await empty_retry_agent._get_model_output_with_retry([UserMessage(content='Retry empty action.')])++ assert len(empty_retry_llm.calls) == 2+ assert empty_retry_output.action[0].model_dump(exclude_unset=True)['done']['text'] == 'No next action returned by LLM!'+ assert logger.debugs == ['✅ Step 1: Got LLM response with 0 actions']+ assert logger.warnings == [+ 'Model returned empty action. Retrying...',+ 'Model still returned empty after retry. Inserting safe noop action.',+ ]async def test_rust_agent_exposes_prepare_context_helper_method(monkeypatch):