fix: avoid standalone system separator blocks

This commit is contained in:
Genmin 2026-05-01 07:44:04 -07:00
parent b42205c48c
commit e734cc2d05
2 changed files with 49 additions and 4 deletions

View file

@ -272,7 +272,33 @@ def _merge_system_message_contents(contents: List[Any]) -> Union[str, List[Any]]
continue
if merged_blocks:
merged_blocks.append({"type": "text", "text": "\n\n"})
last_block = merged_blocks[-1]
first_block = content_blocks[0]
if (
isinstance(last_block, dict)
and last_block.get("type") == "text"
and isinstance(last_block.get("text"), str)
and isinstance(first_block, dict)
and first_block.get("type") == "text"
and isinstance(first_block.get("text"), str)
):
last_block["text"] += "\n\n" + first_block["text"]
merged_blocks.extend(content_blocks[1:])
continue
if (
isinstance(last_block, dict)
and last_block.get("type") == "text"
and isinstance(last_block.get("text"), str)
):
last_block["text"] += "\n\n"
elif (
isinstance(first_block, dict)
and first_block.get("type") == "text"
and isinstance(first_block.get("text"), str)
):
first_block = dict(first_block)
first_block["text"] = "\n\n" + first_block["text"]
content_blocks = [first_block, *content_blocks[1:]]
merged_blocks.extend(content_blocks)
return merged_blocks

View file

@ -51,9 +51,28 @@ def test_map_developer_role_preserves_structured_leading_system_content():
{
"role": "system",
"content": [
{"type": "text", "text": "System rules."},
{"type": "text", "text": "\n\n"},
{"type": "text", "text": "Developer rules."},
{"type": "text", "text": "System rules.\n\nDeveloper rules."},
],
},
{"role": "user", "content": "Hello!"},
]
def test_map_developer_role_avoids_standalone_separator_blocks():
messages = [
{"role": "system", "content": [{"type": "image", "url": "policy.png"}]},
{"role": "developer", "content": "Developer rules."},
{"role": "user", "content": "Hello!"},
]
result = map_developer_role_to_system_role(messages=messages)
assert result == [
{
"role": "system",
"content": [
{"type": "image", "url": "policy.png"},
{"type": "text", "text": "\n\nDeveloper rules."},
],
},
{"role": "user", "content": "Hello!"},