[Bugfix] Guard tool call argument JSON parsing in chat message postprocessing (#48922)

Signed-off-by: VBS2004 <venkatbalaji2004@gmail.com>
Co-authored-by: Ben Browning <56071+bbrowning@users.noreply.github.com>
This commit is contained in:
Venkat Balaji S
2026-08-26 20:26:00 +00:00
committed by GitHub
co-authored by Ben Browning
parent 00ca27d0c1
commit 161ffd37d2
2 changed files with 154 additions and 3 deletions
@@ -2806,3 +2806,120 @@ async def test_resolve_items_does_not_leak_tasks_on_partial_failure():
f"resolve_items left {len(leaked_tasks)} task(s) running after "
f"raising: {leaked_tasks}"
)
def _assistant_tool_call(arguments, name="write"):
return [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_0",
"type": "function",
"function": {"name": name, "arguments": arguments},
}
],
}
]
def _assistant_tool_calls(calls):
return [
{
"role": "assistant",
"content": "",
"tool_calls": calls,
}
]
def test_tool_call_arguments_dict_passthrough(caplog):
messages = _assistant_tool_call({"a": 1})
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {"a": 1}
assert not caplog.records
def test_tool_call_arguments_valid_object_string(caplog):
messages = _assistant_tool_call('{"a": 1}')
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {"a": 1}
assert not caplog.records
def test_tool_call_arguments_malformed_json_small(caplog):
bad = '{"cmd": "mkdir -p /tmp/mmadtest && cat > /tmp/mmadtest'
messages = _assistant_tool_call(bad, name="exec")
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {}
assert len(caplog.records) == 1
assert "exec" in caplog.records[0].message
assert "coercing to an empty object" in caplog.records[0].message
def test_tool_call_arguments_malformed_json_large(caplog):
bad = '{"filepath": "src/main.py", "contents": "' + ("x" * 18000)
messages = _assistant_tool_call(bad, name="write")
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {}
assert len(caplog.records) == 1
assert "write" in caplog.records[0].message
assert "coercing to an empty object" in caplog.records[0].message
@pytest.mark.parametrize("non_obj", ["[]", "42", "true", '"hello"', "null"])
def test_tool_call_arguments_valid_json_non_object(caplog, non_obj):
messages = _assistant_tool_call(non_obj, name="bad_tool")
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {}
if non_obj == "null":
# null parses to None, so no warning is emitted according to requirements
assert not caplog.records
else:
assert len(caplog.records) == 1
assert "bad_tool" in caplog.records[0].message
assert "not a JSON object" in caplog.records[0].message
@pytest.mark.parametrize("missing", [None, ""])
def test_tool_call_arguments_missing_or_empty(caplog, missing):
messages = _assistant_tool_call(missing)
if missing is None:
del messages[0]["tool_calls"][0]["function"]["arguments"]
_postprocess_messages(messages)
args = messages[0]["tool_calls"][0]["function"]["arguments"]
assert args == {}
assert not caplog.records
def test_tool_call_arguments_multiple_independent(caplog):
calls = [
{
"id": "call_0",
"type": "function",
"function": {"name": "good", "arguments": '{"a": 1}'},
},
{
"id": "call_1",
"type": "function",
"function": {"name": "bad", "arguments": '{"cmd": "'},
},
]
messages = _assistant_tool_calls(calls)
_postprocess_messages(messages)
tool_calls = messages[0]["tool_calls"]
assert tool_calls[0]["function"]["arguments"] == {"a": 1}
assert tool_calls[1]["function"]["arguments"] == {}
assert len(caplog.records) == 1
assert "bad" in caplog.records[0].message