diff --git a/headroom/proxy/memory_tool_adapter.py b/headroom/proxy/memory_tool_adapter.py index 1bf0d6c76..542b39c6f 100644 --- a/headroom/proxy/memory_tool_adapter.py +++ b/headroom/proxy/memory_tool_adapter.py @@ -791,13 +791,15 @@ class MemoryToolAdapter: if provider == "anthropic": return str(tool_call.get("name", "")) elif provider == "openai": - return str(tool_call.get("function", {}).get("name", "")) + return str((tool_call.get("function") or {}).get("name", "")) elif provider == "gemini": - func_call = tool_call.get("functionCall", {}) + func_call = tool_call.get("functionCall") or {} return str(func_call.get("name", "")) else: # Generic - try both - return str(tool_call.get("name", "") or tool_call.get("function", {}).get("name", "")) + return str( + tool_call.get("name", "") or (tool_call.get("function") or {}).get("name", "") + ) def _get_tool_id(self, tool_call: dict[str, Any], provider: Provider) -> str: """Get the tool call ID.""" @@ -807,7 +809,7 @@ class MemoryToolAdapter: return str(tool_call.get("id", "")) elif provider == "gemini": # Gemini doesn't use IDs in the same way - return str(tool_call.get("functionCall", {}).get("name", "")) + return str((tool_call.get("functionCall") or {}).get("name", "")) else: return str(tool_call.get("id", "")) @@ -821,25 +823,28 @@ class MemoryToolAdapter: result = tool_call.get("input", {}) return dict(result) if isinstance(result, dict) else {} elif provider == "openai": - args_str = tool_call.get("function", {}).get("arguments", "{}") + # `or {}` guards {"function": null}; `or "{}"` guards {"arguments": null} + # (json.loads(None) raises TypeError, which the bare JSONDecodeError + # catch would miss). + args_str = (tool_call.get("function") or {}).get("arguments") or "{}" try: parsed = json.loads(args_str) return dict(parsed) if isinstance(parsed, dict) else {} - except json.JSONDecodeError: + except (json.JSONDecodeError, TypeError): return {} elif provider == "gemini": - result = tool_call.get("functionCall", {}).get("args", {}) + result = (tool_call.get("functionCall") or {}).get("args", {}) return dict(result) if isinstance(result, dict) else {} else: # Generic - try both if "input" in tool_call: result = tool_call["input"] return dict(result) if isinstance(result, dict) else {} - args_str = tool_call.get("function", {}).get("arguments", "{}") + args_str = (tool_call.get("function") or {}).get("arguments") or "{}" try: parsed = json.loads(args_str) return dict(parsed) if isinstance(parsed, dict) else {} - except json.JSONDecodeError: + except (json.JSONDecodeError, TypeError): return {} async def handle_tool_calls( diff --git a/tests/test_memory_tool_adapter_null_fields.py b/tests/test_memory_tool_adapter_null_fields.py new file mode 100644 index 000000000..b3f6d4cc5 --- /dev/null +++ b/tests/test_memory_tool_adapter_null_fields.py @@ -0,0 +1,35 @@ +"""A tool call with a null ``function`` / ``arguments`` must not crash the +memory tool adapter's provider-format parsing. + +``dict.get("function", {})`` returns ``None`` for a present-but-null key, so the +following ``.get`` raised ``AttributeError``; a null ``arguments`` makes +``json.loads(None)`` raise ``TypeError`` that the bare ``JSONDecodeError`` catch +missed. Both are reachable from the untrusted upstream response. The parse +helpers read only the ``tool_call`` argument, so we exercise them on a bare +instance via ``object.__new__``. +""" + +from __future__ import annotations + +from headroom.proxy.memory_tool_adapter import MemoryToolAdapter + +_adapter = object.__new__(MemoryToolAdapter) + + +def test_get_tool_name_survives_null_function(): + tc = {"id": "c1", "type": "function", "function": None} + assert _adapter._get_tool_name(tc, "openai") == "" + assert _adapter._get_tool_id(tc, "openai") == "c1" + assert _adapter._get_tool_input(tc, "openai") == {} + + +def test_get_tool_input_survives_null_arguments(): + # json.loads(None) raises TypeError, not JSONDecodeError. + tc = {"function": {"name": "memory_save", "arguments": None}} + assert _adapter._get_tool_input(tc, "openai") == {} + + +def test_get_tool_helpers_still_parse_real_calls(): + tc = {"function": {"name": "memory_save", "arguments": '{"content": "hi"}'}} + assert _adapter._get_tool_name(tc, "openai") == "memory_save" + assert _adapter._get_tool_input(tc, "openai") == {"content": "hi"}