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/memory): don't crash memory tool-call detection on a null function (#2272)
Abhay Singh8b7e797ed41e
diff --git a/headroom/proxy/memory_handler.py b/headroom/proxy/memory_handler.pyindex 9749c32c4..66d8b6db4 100644--- a/headroom/proxy/memory_handler.py+++ b/headroom/proxy/memory_handler.py@@ -558,7 +558,7 @@ class MemoryHandler:# Check which tools are already presentexisting_names: set[str] = set()for tool in tools:- name = tool.get("name") or tool.get("function", {}).get("name")+ name = tool.get("name") or (tool.get("function") or {}).get("name")if name:existing_names.add(name)@@ -1039,7 +1039,9 @@ your responses, not to drive new actions.""""""Check if response contains memory tool calls."""tool_calls = self._extract_tool_calls(response, provider)for tc in tool_calls:- name = tc.get("name") or tc.get("function", {}).get("name")+ # Coalesce `function` with `or {}` so an explicit {"function": null}+ # on a malformed/partial upstream tool call doesn't crash detection.+ name = tc.get("name") or (tc.get("function") or {}).get("name")# Check for both custom and native memory toolsif name in MEMORY_TOOL_NAMES or name == NATIVE_MEMORY_TOOL_NAME:return True@@ -1106,7 +1108,11 @@ your responses, not to drive new actions."""results: list[dict[str, Any]] = []for tc in tool_calls:- tool_name = tc.get("name") or tc.get("function", {}).get("name")+ # `tc.get("function", {})` returns None for an explicit+ # {"function": null} (the default only applies to a missing key), so+ # the following `.get` would raise AttributeError on a malformed /+ # partial upstream tool call. Coalesce to {}.+ tool_name = tc.get("name") or (tc.get("function") or {}).get("name")tool_id = tc.get("id") or tc.get("call_id", "")# Parse input data@@ -1115,7 +1121,9 @@ your responses, not to drive new actions."""else:# Chat Completions format: function.arguments# Responses API format: arguments (top-level string)- args_str = tc.get("arguments") or tc.get("function", {}).get("arguments") or "{}"+ args_str = (+ tc.get("arguments") or (tc.get("function") or {}).get("arguments") or "{}"+ )try:input_data = json.loads(args_str)except json.JSONDecodeError:diff --git a/tests/test_memory_handler_null_function.py b/tests/test_memory_handler_null_function.pynew file mode 100644index 000000000..66b3f1b62--- /dev/null+++ b/tests/test_memory_handler_null_function.py@@ -0,0 +1,36 @@+"""A tool call with a null ``function`` must not crash memory tool-call+detection in ``MemoryHandler``.++``tc.get("function", {}).get("name")`` raises ``AttributeError`` on an explicit+``{"function": null}`` (the default only applies to a missing key). Both+``has_memory_tool_calls`` and the arg extraction in ``handle_tool_calls`` read+that shape from the untrusted upstream response. ``has_memory_tool_calls`` and+``_extract_tool_calls`` use no instance state, so we exercise them on a bare+instance via ``object.__new__``.+"""++from __future__ import annotations++from headroom.proxy.memory_handler import MemoryHandler++_handler = object.__new__(MemoryHandler)+++def _openai_response(tool_calls):+ return {"choices": [{"message": {"tool_calls": tool_calls}}]}+++def test_has_memory_tool_calls_survives_null_function():+ response = _openai_response(+ [+ {"id": "c1", "type": "function", "function": None},+ {"id": "c2", "type": "function", "function": {"name": "memory_save"}},+ ]+ )+ # Must not raise, and must still see the real memory tool call.+ assert _handler.has_memory_tool_calls(response, "openai") is True+++def test_has_memory_tool_calls_all_null_functions_is_false():+ response = _openai_response([{"id": "c1", "type": "function", "function": None}])+ assert _handler.has_memory_tool_calls(response, "openai") is False