Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(proxy): keep non text blocks in place when relocating system sections (#3553)
Abdellatif Anaflous964671d883e5
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.pyindex 8a33186fe..0fed50231 100644--- a/headroom/proxy/helpers.py+++ b/headroom/proxy/helpers.py@@ -1040,7 +1040,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-rolemessage is present the inputs pass through unchanged (``changed=False``) so@@ -1102,15 +1104,49 @@ def relocate_system_messages_to_top_level(return messages, system, Falserelocated_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_blockselif isinstance(system, str):diff --git a/tests/test_proxy_handler_helpers.py b/tests/test_proxy_handler_helpers.pyindex 8afaef962..e151c79c4 100644--- a/tests/test_proxy_handler_helpers.py+++ b/tests/test_proxy_handler_helpers.py@@ -404,6 +404,91 @@ def test_relocate_system_messages_moves_valid_shape_for_unsupported_model() -> Nassert 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 Trueassert _headroom_bypass_enabled({"x-headroom-bypass": " TRUE "}) is True