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:
parent
3835a8d5df
commit
aefcdd6f7f
1 changed files with 72 additions and 61 deletions
|
|
@ -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")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue