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
Detect LLM output truncation instead of failing with misleading parse errors
Saurav Panda5f690c207e08
diff --git a/browser_use/llm/anthropic/chat.py b/browser_use/llm/anthropic/chat.pyindex b34459954..d466dced3 100644--- a/browser_use/llm/anthropic/chat.py+++ b/browser_use/llm/anthropic/chat.py@@ -387,6 +387,18 @@ 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 ModelProviderError(+ message=(+ f'Model output was truncated at max_tokens={self.max_tokens}; the structured'+ ' output is incomplete. Increase max_tokens or request shorter output.'+ ),+ status_code=400,+ model=self.name,+ )+# Extract the tool use blockfor content_block in response.content:if hasattr(content_block, 'type') and content_block.type == 'tool_use':@@ -438,5 +450,9 @@ class ChatAnthropic(BaseChatModel):raise ModelRateLimitError(message=e.message, model=self.name) from eexcept 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.+ raiseexcept Exception as e:raise ModelProviderError(message=str(e), model=self.name) from ediff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.pyindex 9c79e20f7..01d937125 100644--- a/browser_use/llm/google/chat.py+++ b/browser_use/llm/google/chat.py@@ -193,6 +193,24 @@ class ChatGoogle(BaseChatModel):return str(response.candidates[0].finish_reason) if hasattr(response.candidates[0], 'finish_reason') else Nonereturn 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."""+ stop_reason = self._get_stop_reason(response)+ if stop_reason and 'MAX_TOKENS' in stop_reason:+ raise ModelProviderError(+ message=(+ f'Model output was truncated at max_output_tokens={self.max_output_tokens};'+ ' the structured output is incomplete. Increase max_output_tokens or request'+ ' shorter output.'+ ),+ status_code=400,+ model=self.name,+ )+def _get_usage(self, response: types.GenerateContentResponse) -> ChatInvokeUsage | None:usage: ChatInvokeUsage | None = None@@ -374,6 +392,7 @@ class ChatGoogle(BaseChatModel):self.logger.debug(f'✅ Got structured response in {elapsed:.2f}s')usage = self._get_usage(response)+ self._raise_if_output_truncated(response)# Handle case where response.parsed might be Noneif response.parsed is None:@@ -458,6 +477,7 @@ class ChatGoogle(BaseChatModel):self.logger.debug(f'✅ Got fallback response in {elapsed:.2f}s')usage = self._get_usage(response)+ self._raise_if_output_truncated(response)# Try to extract JSON from the text responseif response.text:diff --git a/browser_use/llm/openai/chat.py b/browser_use/llm/openai/chat.pyindex 390df689f..5f455f7a7 100644--- a/browser_use/llm/openai/chat.py+++ b/browser_use/llm/openai/chat.py@@ -281,6 +281,19 @@ class ChatOpenAI(BaseChatModel):usage = self._get_usage(response)+ # Output cut off at the completion cap produces incomplete JSON that fails+ # validation with a misleading parse error — surface the real cause instead.+ if choice.finish_reason == 'length':+ raise ModelProviderError(+ message=(+ f'Model output was truncated at max_completion_tokens={self.max_completion_tokens};'+ ' the structured output is incomplete. Increase max_completion_tokens or request'+ ' shorter output.'+ ),+ status_code=400,+ model=self.name,+ )+parsed = output_format.model_validate_json(choice.message.content)return ChatInvokeCompletion(diff --git a/tests/ci/test_llm_output_truncation.py b/tests/ci/test_llm_output_truncation.pynew file mode 100644index 000000000..023882bd8--- /dev/null+++ b/tests/ci/test_llm_output_truncation.py@@ -0,0 +1,65 @@+"""Regression test: structured output cut off at the completion-token cap must raise a+clear truncation error, not a misleading JSON parse error ('Unterminated string...')."""++import pytest+from pydantic import BaseModel++from browser_use.llm.exceptions import ModelProviderError+from browser_use.llm.messages import UserMessage+from browser_use.llm.openai.chat import ChatOpenAI+++class AnswerFormat(BaseModel):+ answer: str+++async def test_openai_truncated_structured_output_raises_clear_error(httpserver):+ """finish_reason='length' with JSON cut mid-string must surface the token cap, not a parse error."""+ httpserver.expect_request('/v1/chat/completions', method='POST').respond_with_json(+ {+ 'id': 'chatcmpl-test',+ 'object': 'chat.completion',+ 'created': 0,+ 'model': 'gpt-4o',+ 'choices': [+ {+ 'index': 0,+ 'message': {'role': 'assistant', 'content': '{"answer": "this output was cut off mid-sent'},+ 'finish_reason': 'length',+ }+ ],+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 4096, 'total_tokens': 4106},+ }+ )++ llm = ChatOpenAI(model='gpt-4o', api_key='test-key', base_url=httpserver.url_for('/v1'))++ with pytest.raises(ModelProviderError) as exc_info:+ await llm.ainvoke([UserMessage(content='answer at length')], output_format=AnswerFormat)++ assert 'truncated' in str(exc_info.value), f'expected a truncation error, got: {exc_info.value}'+ assert 'max_completion_tokens' in str(exc_info.value)+++async def test_openai_normal_structured_output_still_parses(httpserver):+ httpserver.expect_request('/v1/chat/completions', method='POST').respond_with_json(+ {+ 'id': 'chatcmpl-test',+ 'object': 'chat.completion',+ 'created': 0,+ 'model': 'gpt-4o',+ 'choices': [+ {+ 'index': 0,+ 'message': {'role': 'assistant', 'content': '{"answer": "complete answer"}'},+ 'finish_reason': 'stop',+ }+ ],+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 8, 'total_tokens': 18},+ }+ )++ llm = ChatOpenAI(model='gpt-4o', api_key='test-key', base_url=httpserver.url_for('/v1'))++ result = await llm.ainvoke([UserMessage(content='answer briefly')], output_format=AnswerFormat)+ assert result.completion.answer == 'complete answer'