Skip to content

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

Describe truncation gracefully when the token cap is unset

Saurav Pandadb2b2c67f93e
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.py
index 01d937125..e831c6d6a 100644
--- a/browser_use/llm/google/chat.py
+++ b/browser_use/llm/google/chat.py
@@ -201,9 +201,14 @@ class ChatGoogle(BaseChatModel):
would truncate identically."""
stop_reason = self._get_stop_reason(response)
if stop_reason and 'MAX_TOKENS' in stop_reason:
+ cap = (
+ f'max_output_tokens={self.max_output_tokens}'
+ if self.max_output_tokens is not None
+ else "the model's output token limit"
+ )
raise ModelProviderError(
message=(
- f'Model output was truncated at max_output_tokens={self.max_output_tokens};'
+ f'Model output was truncated at {cap};'
' the structured output is incomplete. Increase max_output_tokens or request'
' shorter output.'
),
diff --git a/browser_use/llm/openai/chat.py b/browser_use/llm/openai/chat.py
index 5f455f7a7..476ddf6d2 100644
--- a/browser_use/llm/openai/chat.py
+++ b/browser_use/llm/openai/chat.py
@@ -284,9 +284,14 @@ class ChatOpenAI(BaseChatModel):
# 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':
+ cap = (
+ f'max_completion_tokens={self.max_completion_tokens}'
+ if self.max_completion_tokens is not None
+ else "the model's output token limit"
+ )
raise ModelProviderError(
message=(
- f'Model output was truncated at max_completion_tokens={self.max_completion_tokens};'
+ f'Model output was truncated at {cap};'
' the structured output is incomplete. Increase max_completion_tokens or request'
' shorter output.'
),
diff --git a/tests/ci/test_llm_output_truncation.py b/tests/ci/test_llm_output_truncation.py
index 023882bd8..08b1b0dcb 100644
--- a/tests/ci/test_llm_output_truncation.py
+++ b/tests/ci/test_llm_output_truncation.py
@@ -63,3 +63,31 @@ async def test_openai_normal_structured_output_still_parses(httpserver):
result = await llm.ainvoke([UserMessage(content='answer briefly')], output_format=AnswerFormat)
assert result.completion.answer == 'complete answer'
+
+
+async def test_truncation_error_readable_when_cap_unset(httpserver):
+ """With max_completion_tokens=None, the error must describe the limit without printing 'None'."""
+ 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": "cut off mid'},
+ 'finish_reason': 'length',
+ }
+ ],
+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 16384, 'total_tokens': 16394},
+ }
+ )
+
+ llm = ChatOpenAI(model='gpt-4o', api_key='test-key', base_url=httpserver.url_for('/v1'), max_completion_tokens=None)
+
+ 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)
+ assert 'None' not in str(exc_info.value), f'error message leaks None: {exc_info.value}'