Skip to content

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

fix(llm): only merge system text into the first serialized user message

Mohil-Ahuja7dcb968fcba7
diff --git a/browser_use/llm/google/serializer.py b/browser_use/llm/google/serializer.py
index 1f6a55c13..413441b81 100644
--- a/browser_use/llm/google/serializer.py
+++ b/browser_use/llm/google/serializer.py
@@ -38,6 +38,7 @@ class GoogleMessageSerializer:
formatted_messages: ContentListUnion = []
system_parts: list[str] = []
+ first_user_message_serialized = False
for i, message in enumerate(messages):
role = message.role if hasattr(message, 'role') else None
@@ -70,8 +71,10 @@ class GoogleMessageSerializer:
# If this is the first user message and we have system parts, prepend them
system_text = None
- # An earlier assistant turn must not disqualify the first user message
- if include_system_in_user and system_parts and role == 'user':
+ # Merge into the *first* user message. An earlier assistant turn must not disqualify it,
+ # but a user message that has already been serialized must: the merge target is gone, so
+ # the text falls through to the separate system instruction instead.
+ if include_system_in_user and system_parts and role == 'user' and not first_user_message_serialized:
system_text = '\n\n'.join(system_parts)
system_parts = [] # Clear after using
@@ -113,10 +116,13 @@ class GoogleMessageSerializer:
final_message = Content(role=role, parts=message_parts)
# for some reason, the type checker is not able to infer the type of formatted_messages
formatted_messages.append(final_message) # type: ignore
+ if role == 'user':
+ first_user_message_serialized = True
# Whatever is left was never merged into a user message, so it becomes the separate system
- # instruction. With include_system_in_user=False that is every system message; with it set,
- # this only happens when there is no user message to prepend to.
+ # instruction. With include_system_in_user=False that is every system message. With it set,
+ # it is the messages that had no first user message to merge into, either because the
+ # conversation contains none or because one was already serialized before they arrived.
system_message = '\n\n'.join(system_parts) if system_parts else None
return formatted_messages, system_message
diff --git a/tests/ci/models/test_google_serializer.py b/tests/ci/models/test_google_serializer.py
index 2816bd6ae..e2baaf897 100644
--- a/tests/ci/models/test_google_serializer.py
+++ b/tests/ci/models/test_google_serializer.py
@@ -71,3 +71,18 @@ def test_system_text_falls_back_to_the_instruction_when_there_is_no_user_message
)
assert system_instruction == 'Follow the system rule.'
+
+
+def test_system_message_after_the_first_user_turn_is_not_merged_into_a_later_one():
+ """The merge target is the *first* user message; once it is gone, fall back to the instruction."""
+ contents, system_instruction = GoogleMessageSerializer.serialize_messages(
+ [
+ UserMessage(content='First user turn.'),
+ SystemMessage(content='Follow the system rule.'),
+ UserMessage(content='Second user turn.'),
+ ],
+ include_system_in_user=True,
+ )
+
+ assert system_instruction == 'Follow the system rule.'
+ assert _role_and_text(contents) == [('user', 'First user turn.'), ('user', 'Second user turn.')]