From 161ffd37d207e7def333f07f650e90c67b8bd43b Mon Sep 17 00:00:00 2001 From: Venkat Balaji S <66900514+VBS2004@users.noreply.github.com> Date: Thu, 27 Aug 2026 01:56:00 +0530 Subject: [PATCH] [Bugfix] Guard tool call argument JSON parsing in chat message postprocessing (#48922) Signed-off-by: VBS2004 Co-authored-by: Ben Browning <56071+bbrowning@users.noreply.github.com> --- .../entrypoints/unit_tests/test_chat_utils.py | 117 ++++++++++++++++++ vllm/entrypoints/chat_utils.py | 40 +++++- 2 files changed, 154 insertions(+), 3 deletions(-) diff --git a/tests/entrypoints/unit_tests/test_chat_utils.py b/tests/entrypoints/unit_tests/test_chat_utils.py index 2778770e37..37c76429bf 100644 --- a/tests/entrypoints/unit_tests/test_chat_utils.py +++ b/tests/entrypoints/unit_tests/test_chat_utils.py @@ -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 diff --git a/vllm/entrypoints/chat_utils.py b/vllm/entrypoints/chat_utils.py index 30b6ecc76b..6af31ec804 100644 --- a/vllm/entrypoints/chat_utils.py +++ b/vllm/entrypoints/chat_utils.py @@ -1944,9 +1944,43 @@ def _postprocess_messages(messages: list[ConversationMessage]) -> None: # if arguments is None or empty string, set to {} if content := function.get("arguments"): - if not isinstance(content, (dict, list)): - parsed = json.loads(content) - function["arguments"] = parsed if parsed is not None else {} + if isinstance(content, dict): + parsed = content + else: + if isinstance(content, str): + try: + parsed = json.loads(content) + except json.JSONDecodeError: + # A malformed `arguments` string lives in + # conversation history, so failing the request + # here would fail every subsequent turn too and + # leave the conversation unrecoverable. Coerce + # to an empty object so the turn can proceed. + logger.warning( + "Tool call %r has arguments that are not valid " + "JSON (%d chars); coercing to an empty object " + "so the conversation can continue.", + function.get("name"), + len(content), + ) + parsed = None + else: + parsed = content + + if not isinstance(parsed, dict): + if parsed is not None: + # Valid JSON, but not an object (e.g. "[]", + # "42", "true"). + # Chat templates require a mapping. + logger.warning( + "Tool call %r arguments decoded to %s, not a " + "JSON object; coercing to an empty object.", + function.get("name"), + type(parsed).__name__, + ) + parsed = {} + + function["arguments"] = parsed else: function["arguments"] = {}