[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
+37 -3
View File
@@ -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"] = {}