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:
parent
8c943226f8
commit
7f43678a24
2 changed files with 7 additions and 0 deletions
|
|
@ -54,6 +54,8 @@ def _parse_tool_args(content):
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
try:
|
try:
|
||||||
args = json.loads(content) if content.strip() else {}
|
args = json.loads(content) if content.strip() else {}
|
||||||
|
if not isinstance(args, dict):
|
||||||
|
args = {}
|
||||||
except (json.JSONDecodeError, TypeError) as e:
|
except (json.JSONDecodeError, TypeError) as e:
|
||||||
raise ValueError(str(e))
|
raise ValueError(str(e))
|
||||||
elif isinstance(content, dict):
|
elif isinstance(content, dict):
|
||||||
|
|
|
||||||
|
|
@ -72,3 +72,8 @@ def test_parse_tool_args_lives_in_tool_utils_single_source():
|
||||||
assert _parse_tool_args('{"action":"add"}') == {"action": "add"}
|
assert _parse_tool_args('{"action":"add"}') == {"action": "add"}
|
||||||
# body-envelope unwrap still works
|
# body-envelope unwrap still works
|
||||||
assert _parse_tool_args('{"body":{"action":"x"}}') == {"action": "x"}
|
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"') == {}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue