Skip to content

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

Trim comments to essentials

Saurav Panda2afef146671f
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 353187605..c15080d94 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1991,8 +1991,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
# 402: Insufficient credits/payment required - fallback to different provider
# 429: Rate limit exceeded
# 500, 502, 503, 504: Server errors
- # ModelOutputTruncatedError: the same request would truncate identically on
- # this model, but a fallback with a different output cap can succeed
+ # ModelOutputTruncatedError: not retryable on the same model, but a fallback may have a higher cap
retryable_status_codes = {401, 402, 429, 500, 502, 503, 504}
is_retryable = isinstance(error, (ModelRateLimitError, ModelOutputTruncatedError)) or (
hasattr(error, 'status_code') and error.status_code in retryable_status_codes
diff --git a/browser_use/llm/anthropic/chat.py b/browser_use/llm/anthropic/chat.py
index dc531fa24..4bcacf171 100644
--- a/browser_use/llm/anthropic/chat.py
+++ b/browser_use/llm/anthropic/chat.py
@@ -387,8 +387,6 @@ class ChatAnthropic(BaseChatModel):
usage = self._get_usage(response)
- # A tool_use block cut off at max_tokens produces incomplete JSON that fails
- # validation with a misleading parse error — surface the real cause instead.
if response.stop_reason == 'max_tokens':
raise ModelOutputTruncatedError(
message=(
@@ -450,8 +448,6 @@ class ChatAnthropic(BaseChatModel):
except APIStatusError as e:
raise ModelProviderError(message=e.message, status_code=e.status_code, model=self.name) from e
except ModelProviderError:
- # Already carries an accurate message and status code (e.g. max_tokens
- # truncation) — don't re-wrap it with the generic 502.
- raise
+ raise # don't re-wrap with the generic 502
except Exception as e:
raise ModelProviderError(message=str(e), model=self.name) from e
diff --git a/browser_use/llm/exceptions.py b/browser_use/llm/exceptions.py
index cd959b39c..6c29099e9 100644
--- a/browser_use/llm/exceptions.py
+++ b/browser_use/llm/exceptions.py
@@ -30,12 +30,9 @@ class ModelRateLimitError(ModelProviderError):
class ModelOutputTruncatedError(ModelProviderError):
- """Structured output was cut off at an output-token limit (finish_reason='length' /
- stop_reason='max_tokens').
+ """Output was cut off at an output-token limit (finish_reason='length' / stop_reason='max_tokens').
- Retrying the same request would truncate identically (status 400 keeps it out of
- same-provider retry loops), but switching to a fallback LLM with a different output
- cap can succeed — the agent's fallback logic treats this as switchable.
+ Status 400 keeps it out of same-provider retry loops; the agent's fallback switch treats it as recoverable.
"""
def __init__(
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.py
index 15031a1dd..a5a1a8dcd 100644
--- a/browser_use/llm/google/chat.py
+++ b/browser_use/llm/google/chat.py
@@ -194,11 +194,7 @@ class ChatGoogle(BaseChatModel):
return None
def _raise_if_output_truncated(self, response: types.GenerateContentResponse) -> None:
- """Structured output cut off at max_output_tokens produces incomplete JSON that
- fails parsing with a misleading error — surface the real cause instead.
-
- Uses status 400 (not in retryable_status_codes): retrying the same request
- would truncate identically."""
+ """Raise ModelOutputTruncatedError when the response hit an output-token limit."""
stop_reason = self._get_stop_reason(response)
if stop_reason and 'MAX_TOKENS' in stop_reason:
cap = (
diff --git a/browser_use/llm/openai/chat.py b/browser_use/llm/openai/chat.py
index f526df9ba..1074d3439 100644
--- a/browser_use/llm/openai/chat.py
+++ b/browser_use/llm/openai/chat.py
@@ -272,11 +272,8 @@ class ChatOpenAI(BaseChatModel):
model=self.name,
)
- # Output cut off at the completion cap produces incomplete JSON that fails
- # validation with a misleading parse error — surface the real cause instead.
- # Checked BEFORE the missing-content guard: reasoning models can spend the
- # whole budget on hidden reasoning, returning finish_reason='length' with
- # content=None, which must also read as truncation.
+ # before the content-None guard: reasoning models can burn the whole budget
+ # on hidden reasoning, leaving finish_reason='length' with content=None
if choice.finish_reason == 'length':
cap = (
f'max_completion_tokens={self.max_completion_tokens}'