Skip to content

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

Detect truncation before the missing-content guard

Saurav Pandae672ab6de719
diff --git a/browser_use/llm/openai/chat.py b/browser_use/llm/openai/chat.py
index e915bca94..f526df9ba 100644
--- a/browser_use/llm/openai/chat.py
+++ b/browser_use/llm/openai/chat.py
@@ -272,17 +272,11 @@ class ChatOpenAI(BaseChatModel):
model=self.name,
)
- if choice.message.content is None:
- raise ModelProviderError(
- message='Failed to parse structured output from model response',
- status_code=500,
- model=self.name,
- )
-
- 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.
+ # 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.
if choice.finish_reason == 'length':
cap = (
f'max_completion_tokens={self.max_completion_tokens}'
@@ -298,6 +292,15 @@ class ChatOpenAI(BaseChatModel):
model=self.name,
)
+ if choice.message.content is None:
+ raise ModelProviderError(
+ message='Failed to parse structured output from model response',
+ status_code=500,
+ model=self.name,
+ )
+
+ usage = self._get_usage(response)
+
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.py
index 6a777291c..032b7ed9e 100644
--- a/tests/ci/test_llm_output_truncation.py
+++ b/tests/ci/test_llm_output_truncation.py
@@ -110,3 +110,32 @@ def test_truncation_error_triggers_fallback_llm_switch(tmp_path):
error = ModelOutputTruncatedError(message='Model output was truncated at max_completion_tokens=4096', model='gpt-4o')
assert agent._try_switch_to_fallback_llm(error) is True
assert agent._using_fallback_llm is True
+
+
+async def test_truncation_detected_when_content_is_null(httpserver):
+ """Reasoning models can spend the whole completion budget on hidden reasoning:
+ finish_reason='length' with content=null. That must surface as truncation, not
+ the generic 'Failed to parse structured output'."""
+ httpserver.expect_request('/v1/chat/completions', method='POST').respond_with_json(
+ {
+ 'id': 'chatcmpl-test',
+ 'object': 'chat.completion',
+ 'created': 0,
+ 'model': 'o3',
+ 'choices': [
+ {
+ 'index': 0,
+ 'message': {'role': 'assistant', 'content': None},
+ 'finish_reason': 'length',
+ }
+ ],
+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 4096, 'total_tokens': 4106},
+ }
+ )
+
+ llm = ChatOpenAI(model='o3', api_key='test-key', base_url=httpserver.url_for('/v1'))
+
+ with pytest.raises(ModelProviderError) as exc_info:
+ await llm.ainvoke([UserMessage(content='think hard')], output_format=AnswerFormat)
+
+ assert 'truncated' in str(exc_info.value), f'expected truncation signal, got: {exc_info.value}'