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): harden _extract_tool_calls against malformed choices and blocks (#2483)
Abhay Singheffa5a5886be
diff --git a/headroom/proxy/memory_handler.py b/headroom/proxy/memory_handler.pyindex 89a8a99e0..a3ea65473 100644--- a/headroom/proxy/memory_handler.py+++ b/headroom/proxy/memory_handler.py@@ -1066,17 +1066,35 @@ your responses, not to drive new actions."""if provider == "anthropic":content = response.get("content", [])if isinstance(content, list):- return [block for block in content if block.get("type") == "tool_use"]+ return [+ block+ for block in content+ if isinstance(block, dict) and block.get("type") == "tool_use"+ ]return []elif provider == "openai":- # Chat Completions format: choices[0].message.tool_calls- choices = response.get("choices", [])- if choices:- message = choices[0].get("message", {})- tc_list = list(message.get("tool_calls", []) or [])- if tc_list:- return tc_list+ # Chat Completions format: choices[0].message.tool_calls. Guard the+ # first choice for dict-ness the way the sibling+ # CCRResponseHandler._extract_assistant_message already does: an+ # OpenAI-compatible gateway can send `choices: [null]` on a+ # content-filtered / usage-only response, and this runs from+ # has_memory_tool_calls, which is called outside the try that wraps+ # the rest of memory handling — so an unguarded `choices[0].get` here+ # would surface AttributeError to the request handler.+ choices = response.get("choices")+ first = choices[0] if isinstance(choices, list) and choices else None+ if isinstance(first, dict):+ message = first.get("message")+ if isinstance(message, dict):+ # Filter to dict entries: a null / string element inside+ # tool_calls (which downstream immediately calls `.get` on)+ # would otherwise still crash detection.+ tc_list = [+ tc for tc in (message.get("tool_calls") or []) if isinstance(tc, dict)+ ]+ if tc_list:+ return tc_list# Responses API format: output[] with type=function_calloutput = response.get("output", [])diff --git a/tests/test_memory_handler_null_function.py b/tests/test_memory_handler_null_function.pyindex 66b3f1b62..cc936e0d9 100644--- a/tests/test_memory_handler_null_function.py+++ b/tests/test_memory_handler_null_function.py@@ -34,3 +34,54 @@ def test_has_memory_tool_calls_survives_null_function():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+++def test_extract_tool_calls_survives_null_choice_element():+ # An OpenAI-compatible gateway can send `choices: [null]` on a+ # content-filtered / usage-only response. `choices[0].get(...)` on the null+ # element would raise AttributeError; detection must treat it as no tool+ # calls, matching the sibling guard in CCRResponseHandler.+ for choices in ([None], ["not-a-dict"], [{"message": None}]):+ response = {"choices": choices}+ assert _handler._extract_tool_calls(response, "openai") == []+ assert _handler.has_memory_tool_calls(response, "openai") is False+++def test_extract_tool_calls_falls_through_to_responses_output():+ # A null first choice must not shadow a Responses-API `output[]` payload in+ # the same response; the function_call there is still detected.+ response = {+ "choices": [None],+ "output": [{"type": "function_call", "name": "memory_save", "arguments": "{}"}],+ }+ assert _handler.has_memory_tool_calls(response, "openai") is True+++def test_extract_tool_calls_survives_non_dict_anthropic_block():+ # A reconstructed Anthropic response may carry a null content block; the+ # `block.get("type")` filter must skip it instead of raising.+ response = {"content": [None, {"type": "tool_use", "id": "t1", "name": "memory_save"}]}+ calls = _handler._extract_tool_calls(response, "anthropic")+ assert [c["id"] for c in calls] == ["t1"]+++def test_extract_tool_calls_skips_non_dict_openai_tool_call_elements():+ # A null / string element inside message.tool_calls is passed straight to+ # `.get` downstream (has_memory_tool_calls / handle_memory_tool_calls), so a+ # malformed element must be dropped while a valid tool call is still seen.+ response = {+ "choices": [+ {+ "message": {+ "tool_calls": [+ None,+ "oops",+ {"id": "c1", "type": "function", "function": {"name": "memory_save"}},+ ]+ }+ }+ ]+ }+ calls = _handler._extract_tool_calls(response, "openai")+ assert [c["id"] for c in calls] == ["c1"]+ assert _handler.has_memory_tool_calls(response, "openai") is True