fix: graceful recovery from malformed LLM tool call output#1458
Open
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Open
fix: graceful recovery from malformed LLM tool call output#1458gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Conversation
When validate_tool_request() raises ValueError on malformed JSON from the LLM, the agent crashes instead of recovering. Wrapping in try/except ValueError routes the output to the misformat warning path, allowing the agent to self-correct and continue operating.
gdeyoung
added a commit
to gdeyoung/agent-zero
that referenced
this pull request
Apr 9, 2026
When LLMs output valid JSON with tool_name but missing/malformed tool_args, json_parse_dirty() returned the incomplete dict as-is, causing ValueError crashes in validate_tool_request().
Root cause fix: normalize tool requests in json_parse_dirty() before returning them:
- Missing tool_args -> defaults to {}
- tool_args as string -> converts to dict
- tool_args as non-dict -> defaults to {}
- tool key -> normalized to tool_name
Non-tool requests are left untouched.
Related: agent0ai#1458, agent0ai#1466
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR — Agent crashes on malformed LLM output instead of recovering gracefully
When
validate_tool_request()raises aValueError(e.g., malformed JSON from non-OpenAI models like GLM, MiniMax, Qwen), the exception propagates uncaught and crashes the entire agent. A simpletry/exceptwrapper routes the output to the existing misformat warning path, allowing the agent to self-correct and continue.One line of defensive wrapping prevents a crash loop that requires manual agent restart.
Problem
In
agent.py, thevalidate_tool_request()call at line ~878 is unguarded:When the LLM produces malformed JSON (common with GLM-5.1, MiniMax, Qwen),
validate_tool_request()raisesValueError. This exception is not caught, causing the agent to crash instead of using the existing misformat recovery path.Solution
Wrap the call in
try/except ValueError:When
tool_requestis set toNone, the existing misformat warning logic handles it — prompting the agent to self-correct on the next turn.Impact
Changes
agent.pyvalidate_tool_request()intry/except ValueErrorTesting
Related