[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:
co-authored by
Ben Browning
parent
00ca27d0c1
commit
161ffd37d2
@@ -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"resolve_items left {len(leaked_tasks)} task(s) running after "
|
||||||
f"raising: {leaked_tasks}"
|
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
|
||||||
|
|||||||
@@ -1944,9 +1944,43 @@ def _postprocess_messages(messages: list[ConversationMessage]) -> None:
|
|||||||
|
|
||||||
# if arguments is None or empty string, set to {}
|
# if arguments is None or empty string, set to {}
|
||||||
if content := function.get("arguments"):
|
if content := function.get("arguments"):
|
||||||
if not isinstance(content, (dict, list)):
|
if isinstance(content, dict):
|
||||||
parsed = json.loads(content)
|
parsed = content
|
||||||
function["arguments"] = parsed if parsed is not None else {}
|
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:
|
else:
|
||||||
function["arguments"] = {}
|
function["arguments"] = {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user