fix: return JSON parse error to model instead of dispatching with empty args (#2342)

When the model produces malformed JSON in tool call arguments, the agent
loop was setting args={} and dispatching the tool anyway, wasting an
iteration and producing a confusing downstream error. Now the error is
returned directly as the tool result so the model can retry with valid JSON.

Co-authored-by: alireza78a <alireza78.crypto@gmail.com>
This commit is contained in:
Teknium 2026-03-21 09:56:44 -07:00 committed by GitHub
parent 3835a8d5df
commit aefcdd6f7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -346,16 +346,27 @@ class HermesAgentLoop:
tool_name, turn + 1, tool_name, turn + 1,
) )
else: else:
# Parse arguments and dispatch # Parse arguments
try: try:
args = json.loads(tool_args_raw) args = json.loads(tool_args_raw)
except json.JSONDecodeError: except json.JSONDecodeError as e:
args = {} args = None
tool_result = json.dumps(
{"error": f"Invalid JSON in tool arguments: {e}. Please retry with valid JSON."}
)
tool_errors.append(ToolError(
turn=turn + 1, tool_name=tool_name,
arguments=tool_args_raw[:200],
error=f"Invalid JSON: {e}",
tool_result=tool_result,
))
logger.warning( logger.warning(
"Invalid JSON in tool call arguments for '%s': %s", "Invalid JSON in tool call arguments for '%s': %s",
tool_name, tool_args_raw[:200], tool_name, tool_args_raw[:200],
) )
# Dispatch tool only if arguments parsed successfully
if args is not None:
try: try:
if tool_name == "terminal": if tool_name == "terminal":
backend = os.getenv("TERMINAL_ENV", "local") backend = os.getenv("TERMINAL_ENV", "local")