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.
This commit is contained in:
Tanmay Garg
2026-07-03 17:37:44 +05:30
committed by GitHub
parent 8c943226f8
commit 7f43678a24
2 changed files with 7 additions and 0 deletions
+2
View File
@@ -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):
+5
View File
@@ -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"') == {}