From 7f43678a244730d689ad924ab146e65f9dcece53 Mon Sep 17 00:00:00 2001 From: Tanmay Garg <102200932+Tanmay9223@users.noreply.github.com> Date: Fri, 3 Jul 2026 17:37:44 +0530 Subject: [PATCH] fix(tools): handle non-dict JSON values in _parse_tool_args (closes #5043) (#5064) When an LLM generates a valid JSON string that parses to a native non-dict type (like a list, int, or string), _parse_tool_args previously returned that object. Callers expecting a dictionary would then crash with AttributeError or KeyError when attempting to look up action keys. - Update _parse_tool_args in src/tool_utils.py to explicitly type-check the parsed JSON object and return {} for non-dict objects. - Add test coverage in tests/test_admin_tools_registry.py for lists, ints, and strings. --- src/tool_utils.py | 2 ++ tests/test_admin_tools_registry.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/tool_utils.py b/src/tool_utils.py index bb60a1095..8255bc0a9 100644 --- a/src/tool_utils.py +++ b/src/tool_utils.py @@ -54,6 +54,8 @@ def _parse_tool_args(content): if isinstance(content, str): try: args = json.loads(content) if content.strip() else {} + if not isinstance(args, dict): + args = {} except (json.JSONDecodeError, TypeError) as e: raise ValueError(str(e)) elif isinstance(content, dict): diff --git a/tests/test_admin_tools_registry.py b/tests/test_admin_tools_registry.py index 4bde9ea9e..0470a0e36 100644 --- a/tests/test_admin_tools_registry.py +++ b/tests/test_admin_tools_registry.py @@ -72,3 +72,8 @@ def test_parse_tool_args_lives_in_tool_utils_single_source(): assert _parse_tool_args('{"action":"add"}') == {"action": "add"} # body-envelope unwrap still works assert _parse_tool_args('{"body":{"action":"x"}}') == {"action": "x"} + + # non-dict JSON values should return {} + assert _parse_tool_args('[1, 2]') == {} + assert _parse_tool_args('42') == {} + assert _parse_tool_args('"hello"') == {}