mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
fix(proxy): keep non text blocks in place when relocating system sections
This commit is contained in:
@@ -917,7 +917,9 @@ def relocate_system_messages_to_top_level(
|
||||
|
||||
The relocated content is appended after any existing top-level ``system``
|
||||
so wire order (system prompt, then conversation) is preserved and no content
|
||||
is dropped.
|
||||
is dropped. Only text-shaped content moves: non-text blocks (images,
|
||||
documents) stay in a mid-conversation system section at their original
|
||||
position, because top-level `system` accepts text blocks only (issue #3552).
|
||||
|
||||
Returns ``(clean_messages, new_system, changed)``. When no system-role
|
||||
message is present the inputs pass through unchanged (``changed=False``) so
|
||||
@@ -979,15 +981,49 @@ def relocate_system_messages_to_top_level(
|
||||
return messages, system, False
|
||||
|
||||
relocated_blocks: list[Any] = []
|
||||
retained: dict[int, dict[str, Any]] = {}
|
||||
for i in sorted(system_indices):
|
||||
relocated_blocks.extend(_system_message_to_blocks(messages[i]))
|
||||
|
||||
clean_messages = [m for i, m in enumerate(messages) if i not in system_indices]
|
||||
message = messages[i]
|
||||
content = message.get("content") if isinstance(message, dict) else None
|
||||
if isinstance(content, list):
|
||||
# Only text-shaped content may move into the top-level ``system``
|
||||
# parameter (text blocks and bare strings). Non-text blocks such as
|
||||
# images or documents stay in place so nothing is dropped and
|
||||
# upstreams that reject non-text system blocks keep working
|
||||
# (issue #3552).
|
||||
hoisted_from_list: list[Any] = []
|
||||
leftovers: list[Any] = []
|
||||
for block in content:
|
||||
if isinstance(block, dict) and block.get("type") == _TEXT_BLOCK_TYPE:
|
||||
hoisted_from_list.append(block)
|
||||
elif isinstance(block, str) and block:
|
||||
hoisted_from_list.append({"type": _TEXT_BLOCK_TYPE, "text": block})
|
||||
else:
|
||||
leftovers.append(block)
|
||||
relocated_blocks.extend(hoisted_from_list)
|
||||
if leftovers:
|
||||
retained[i] = {**message, "content": leftovers}
|
||||
else:
|
||||
# String (and other) content converts losslessly to text blocks.
|
||||
relocated_blocks.extend(_system_message_to_blocks(message))
|
||||
|
||||
if not relocated_blocks:
|
||||
if retained:
|
||||
# Nothing text-shaped to relocate: the sections stay as they are.
|
||||
return messages, system, False
|
||||
# System message(s) carried no content — drop the empty entries only.
|
||||
clean_messages = [m for i, m in enumerate(messages) if i not in system_indices]
|
||||
return clean_messages, system, True
|
||||
|
||||
clean_messages = []
|
||||
for i, message in enumerate(messages):
|
||||
if i in system_indices:
|
||||
trimmed = retained.get(i)
|
||||
if trimmed is not None:
|
||||
clean_messages.append(trimmed)
|
||||
continue
|
||||
clean_messages.append(message)
|
||||
|
||||
if system is None or system == "" or system == []:
|
||||
new_system: Any = relocated_blocks
|
||||
elif isinstance(system, str):
|
||||
|
||||
@@ -404,6 +404,91 @@ def test_relocate_system_messages_moves_valid_shape_for_unsupported_model() -> N
|
||||
assert system == [{"type": "text", "text": "mid-turn instruction"}]
|
||||
|
||||
|
||||
def test_relocate_system_messages_keeps_image_blocks_out_of_top_level_system() -> None:
|
||||
image_block = {
|
||||
"type": "image",
|
||||
"source": {"type": "base64", "media_type": "image/png", "data": "aGk="},
|
||||
}
|
||||
messages = [
|
||||
{"role": "user", "content": [{"type": "text", "text": "look at this"}]},
|
||||
{
|
||||
"role": "system",
|
||||
"content": [
|
||||
{"type": "text", "text": "<system-reminder>image attached</system-reminder>"},
|
||||
image_block,
|
||||
],
|
||||
},
|
||||
{"role": "assistant", "content": [{"type": "text", "text": "ok"}]},
|
||||
{"role": "user", "content": [{"type": "text", "text": "hi"}]},
|
||||
]
|
||||
system = [{"type": "text", "text": "You are Claude Code."}]
|
||||
|
||||
clean, new_system, changed = relocate_system_messages_to_top_level(messages, system, None)
|
||||
|
||||
assert changed is True
|
||||
assert isinstance(new_system, list)
|
||||
assert all(not isinstance(block, dict) or block.get("type") == "text" for block in new_system)
|
||||
retained = [
|
||||
message.get("content")
|
||||
for message in clean
|
||||
if isinstance(message, dict) and message.get("role") == "system"
|
||||
]
|
||||
assert any(image_block in (content or []) for content in retained)
|
||||
|
||||
|
||||
def test_relocate_system_messages_hoists_only_text_from_mixed_sections() -> None:
|
||||
messages = [
|
||||
{"role": "user", "content": "question"},
|
||||
{
|
||||
"role": "system",
|
||||
"content": [
|
||||
"plain string section",
|
||||
{"type": "text", "text": "structured note"},
|
||||
{"type": "image", "source": {"type": "url", "url": "https://x/y.png"}},
|
||||
],
|
||||
},
|
||||
{"role": "assistant", "content": "ok"},
|
||||
]
|
||||
|
||||
clean, new_system, changed = relocate_system_messages_to_top_level(messages, None, None)
|
||||
|
||||
assert changed is True
|
||||
assert new_system == [
|
||||
{"type": "text", "text": "plain string section"},
|
||||
{"type": "text", "text": "structured note"},
|
||||
]
|
||||
retained = [
|
||||
message
|
||||
for message in clean
|
||||
if isinstance(message, dict) and message.get("role") == "system"
|
||||
]
|
||||
assert retained == [
|
||||
{
|
||||
"role": "system",
|
||||
"content": [{"type": "image", "source": {"type": "url", "url": "https://x/y.png"}}],
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_relocate_system_messages_image_only_sections_pass_through_unchanged() -> None:
|
||||
image_block = {
|
||||
"type": "image",
|
||||
"source": {"type": "base64", "media_type": "image/png", "data": "aGk="},
|
||||
}
|
||||
messages = [
|
||||
{"role": "user", "content": "question"},
|
||||
{"role": "system", "content": [image_block]},
|
||||
{"role": "assistant", "content": "ok"},
|
||||
]
|
||||
system = "base"
|
||||
|
||||
clean, new_system, changed = relocate_system_messages_to_top_level(messages, system, None)
|
||||
|
||||
assert changed is False
|
||||
assert clean == messages
|
||||
assert new_system == system
|
||||
|
||||
|
||||
def test_headroom_bypass_helper_is_transport_neutral() -> None:
|
||||
assert _headroom_bypass_enabled({"x-headroom-bypass": "true"}) is True
|
||||
assert _headroom_bypass_enabled({"x-headroom-bypass": " TRUE "}) is True
|
||||
|
||||
Reference in New Issue
Block a user