Skip to content

fix(llm/google): keep the first user message when include_system_in_user is set - #5597

Merged
MagMueller merged 2 commits into
browser-use:mainfrom
aniketwaghh:fix_gemini_system_in_user_drops_parts
Aug 30, 2026
Merged

MagMueller merged 2 commits into
browser-use:mainfrom
aniketwaghh:fix_gemini_system_in_user_drops_parts

Conversation

@aniketwaghh

@aniketwaghh aniketwaghh commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5596

ChatGoogle(include_system_in_user=True) is meant to prepend the system text to the first user message. When that message has list content it replaced it instead: the system text became the only part sent, and the user's text and images were dropped.

That is the shape an agent run always produces. use_vision defaults to True, AgentMessagePrompt.get_user_message() returns list content as soon as there is a screenshot, and MessageHistory.get_messages() puts the state message first among the user messages. So with this flag on, every step sent Gemini the system prompt and nothing else, with no error anywhere.

The cause was structural: the content-part conversion lived in the else arm of the prepend branch, so taking the prepend branch skipped it. The fix computes the system text first and then always runs the conversion, folding the system text into the leading text only for str content, which is what it already did.

Before, on main:

include_system_in_user=False user ["'<browser_state>the page and the task live'", 'image']
include_system_in_user=True  user ["'You are a browser agent.'"]

After:

include_system_in_user=False user ["'<browser_state>the page and the task live'", 'image']
include_system_in_user=True  user ["'You are a browser agent.'", "'<browser_state>the page and the task live'", 'image']

Tests

Three tests in tests/ci/models/test_llm_google.py, real objects only:

  • test_include_system_in_user_keeps_list_content_parts: text part and image survive alongside the prepended system text.
  • test_include_system_in_user_string_content_is_merged_into_one_part: parity guard for the str path, which was already correct and must stay a single merged part. This one passes on main too.
  • test_include_system_in_user_keeps_the_agent_state_message: builds the message through the real AgentMessagePrompt with use_vision=True and asserts the task and screenshot reach the serialized contents.

Reverting the change in browser_use/llm/google/serializer.py and rerunning:

FAILED tests/ci/models/test_llm_google.py::test_include_system_in_user_keeps_list_content_parts
E   AssertionError: user text was dropped
    assert '<browser_state>the page and the task live here</browser_state>' in 'You are a browser agent.'

FAILED tests/ci/models/test_llm_google.py::test_include_system_in_user_keeps_the_agent_state_message
E   AssertionError: the task never reached the model

2 failed, 1 passed

With the fix, pytest tests/ci/models/test_llm_google.py gives 8 passed, 1 skipped (the skip is the live gemini-3-flash-preview test, no API key here).

Checks

pre-commit run --files browser_use/llm/google/serializer.py tests/ci/models/test_llm_google.py passes every hook, including ruff check, ruff format and pyright at the versions pinned in .pre-commit-config.yaml.

Full suite, pytest tests/ci -q -o addopts="" --timeout=120: 2 failed, 1108 passed, 34 skipped on this branch, against 2 failed, 1105 passed, 34 skipped on main (the +3 are the new tests). The two failures are the same tests/ci/test_beta_agent.py isinstance checks in both runs; they are order-dependent and pass on their own, and they are unrelated to this change.


Summary by cubic

Fixes #5596 by keeping the first user message's content when include_system_in_user=True is set on ChatGoogle. Previously, when the message had list content (text plus images, as the agent always produces with vision on), the system text replaced it and the user's text and screenshot were dropped; now the system text is prepended and the user's parts are preserved.

  • Reorders the serializer so content conversion runs for both string and list content, merging system text only into the leading text part.
  • Adds three tests covering list content, string content parity, and the agent's actual message shape.

Written for commit 6a2b0f4. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@MagMueller
MagMueller merged commit d2fcda5 into browser-use:main Aug 30, 2026
112 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: ChatGoogle(include_system_in_user=True) drops the first user message when it has list content

2 participants