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(gemini): keep snake_case inline_data and function parts when compressing (#4290)
Rudy Mizrahi Celekli4bf1bf55d175
diff --git a/headroom/proxy/handlers/gemini.py b/headroom/proxy/handlers/gemini.pyindex a9bdb9970..71f6bf495 100644--- a/headroom/proxy/handlers/gemini.py+++ b/headroom/proxy/handlers/gemini.py@@ -156,6 +156,10 @@ class GeminiHandlerMixin:- thought / thoughtSignature: thinking parts, which must round-trip intact- non-string text, which the text round trip cannot rebuild+ The REST API accepts both the camelCase JSON names and the snake_case+ proto field names (``inline_data``, ``function_call``, ...), so both+ spellings are checked.+Args:content: A single Gemini content entry with 'parts' list.@@ -174,6 +178,13 @@ class GeminiHandlerMixin:"codeExecutionResult","thought","thoughtSignature",+ "inline_data",+ "file_data",+ "function_call",+ "function_response",+ "executable_code",+ "code_execution_result",+ "thought_signature",)):return True@@ -269,9 +280,10 @@ class GeminiHandlerMixin:if include_function_responses:for part in parts:- if "functionResponse" not in part:+ function_response = part.get("functionResponse", part.get("function_response"))+ if function_response is None:continue- payload = self._function_response_text(part["functionResponse"])+ payload = self._function_response_text(function_response)if payload:messages.append({"role": "tool", "content": payload})diff --git a/tests/test_gemini_function_response_waste.py b/tests/test_gemini_function_response_waste.pyindex 20161e66c..f430bb933 100644--- a/tests/test_gemini_function_response_waste.py+++ b/tests/test_gemini_function_response_waste.py@@ -89,6 +89,23 @@ class TestFunctionResponseConversion:# verbatim on the wire regardless of the telemetry conversion.assert preserved == {1}+ def test_flag_emits_tool_message_for_snake_case_function_response(self, proxy):+ """The REST API also accepts ``function_response``; its payload counts too."""+ payload = _big_payload()+ contents = [+ {"role": "user", "parts": [{"text": "fetch the data"}]},+ {+ "role": "user",+ "parts": [{"function_response": {"name": "fetch_data", "response": payload}}],+ },+ ]+ messages, preserved = proxy._gemini_contents_to_messages(+ contents, include_function_responses=True+ )+ assert [m["role"] for m in messages] == ["user", "tool"]+ assert json.loads(messages[1]["content"]) == payload+ assert preserved == {1}+def test_flag_passes_string_response_through(self, proxy):contents = [_function_response_content("plain text tool output")]messages, _ = proxy._gemini_contents_to_messages(contents, include_function_responses=True)diff --git a/tests/test_google_multimodal.py b/tests/test_google_multimodal.pyindex 21627d1a1..5f6dba7aa 100644--- a/tests/test_google_multimodal.py+++ b/tests/test_google_multimodal.py@@ -182,6 +182,15 @@ class TestHasNonTextParts:# turns; previously not detected, so they were dropped on round-trip."executableCode","codeExecutionResult",+ # The REST API also accepts the proto field names, which Google's+ # own curl examples use; these were dropped on round-trip too.+ "inline_data",+ "file_data",+ "function_call",+ "function_response",+ "executable_code",+ "code_execution_result",+ "thought_signature",],)def test_each_non_text_key_detected(self, proxy, non_text_key):@@ -194,6 +203,36 @@ class TestHasNonTextParts:content = {"role": "user"}assert proxy._has_non_text_parts(content) is False+ def test_snake_case_parts_survive_compression_round_trip(self, proxy):+ """snake_case image and tool parts are restored after compression rewrites text."""+ contents = [+ {+ "role": "user",+ "parts": [+ {"text": "Describe this image"},+ {"inline_data": {"mime_type": "image/png", "data": "iVBORw0KGgo="}},+ ],+ },+ {"role": "model", "parts": [{"function_call": {"name": "lookup", "args": {}}}]},+ {+ "role": "user",+ "parts": [{"function_response": {"name": "lookup", "response": {"r": "ok"}}}],+ },+ {"role": "user", "parts": [{"text": "thanks " * 100}]},+ ]+ messages, preserved = proxy._gemini_contents_to_messages(contents)+ assert preserved == {0, 1, 2}++ optimized = [dict(m) for m in messages]+ optimized[-1]["content"] = "thanks (compressed)"+ optimized_contents, _ = proxy._messages_to_gemini_contents(optimized)+ rebuilt = proxy._rebuild_gemini_contents(+ contents, preserved, {i: contents[i] for i in preserved}, optimized_contents+ )++ assert rebuilt[:3] == contents[:3]+ assert rebuilt[3] == {"role": "user", "parts": [{"text": "thanks (compressed)"}]}+# =============================================================================# Tests for _gemini_contents_to_messages preserved indices