[Bugfix] Drop empty tool_calls lists to keep assistant replies in chat template (#30648)

Signed-off-by: Seokhyun An <iamseokhyun@gmail.com>
This commit is contained in:
Seokhyun An 2025-12-15 13:21:12 +09:00 committed by GitHub
parent a524d1ba0a
commit b337647aa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1629,12 +1629,17 @@ def _postprocess_messages(messages: list[ConversationMessage]) -> None:
# so, for messages that have tool_calls, parse the string (which we get # so, for messages that have tool_calls, parse the string (which we get
# from openAI format) to dict # from openAI format) to dict
for message in messages: for message in messages:
if ( if message["role"] == "assistant" and "tool_calls" in message:
message["role"] == "assistant" tool_calls = message.get("tool_calls")
and "tool_calls" in message if not isinstance(tool_calls, list):
and isinstance(message["tool_calls"], list) continue
):
for item in message["tool_calls"]: if len(tool_calls) == 0:
# Drop empty tool_calls to keep templates on the normal assistant path.
message.pop("tool_calls", None)
continue
for item in tool_calls:
# if arguments is None or empty string, set to {} # if arguments is None or empty string, set to {}
if content := item["function"].get("arguments"): if content := item["function"].get("arguments"):
if not isinstance(content, (dict, list)): if not isinstance(content, (dict, list)):