Summary
Fixed three root causes of JSON malformation that were causing infinite loops and failed tool executions in Agent Zero. (fixed with Agent Zero and Minimax 2.5) :)
final_diffs.zip
Issues Found
Issue 1: Incorrect JSON Object Extraction (rfind bug)
Location: python/helpers/extract_tools.py - extract_json_object_string()
Problem: The function used content.rfind('}') which finds the LAST closing brace, incorrectly concatenating multiple JSON objects into invalid output.
# Buggy code:
end = content.rfind('}')
return content[start:end+1]
This caused malformed JSON when the LLM returned multiple JSON objects in a single response.
Issue 2: Escape Handling Logic Error
Location: python/helpers/extract_tools.py - extract_json_object_string()
Problem: When the parser encountered a backslash (\), it set escape_next = True and skipped the next character entirely. However, if that character was an escaped quote (\"), it should have toggled in_string - but it didn't!
This caused in_string to stay True incorrectly, so closing braces were never recognized as being outside a string, resulting in empty/malformed JSON.
# Buggy code:
if escape_next:
escape_next = False
i += 1 # Skipped WITHOUT checking for escaped quotes!
continue
Issue 3: No Loop Protection
Location: agent.py - process_tools() method
Problem: No protection against consecutive misformat errors. The system kept sending warnings back to the LLM which could produce more malformed JSON, causing infinite loops.
Fixes Applied
Fix 1: Proper Nested Brace Tracking
Replaced simple rfind() with proper nested brace tracking that correctly extracts only the first valid JSON object.
# Track nested braces properly
depth = 0
in_string = False
escape_next = False
i = start
while i < len(content):
char = content[i]
if escape_next:
escape_next = False
if char == '"':
# Escaped quote - toggle in_string
in_string = not in_string
i += 1
continue
if char == '\\':
escape_next = True
i += 1
continue
if char == '"':
in_string = not in_string
i += 1
continue
# Only count braces outside of strings
if not in_string:
if char == '{':
depth += 1
elif char == '}':
depth -= 1
if depth == 0:
return content[start:i+1]
i += 1
Fix 2: Proper Escape Handling
Added check for escaped quotes to toggle in_string properly:
if escape_next:
escape_next = False
if char == '"':
# Escaped quote - toggle in_string but don't treat as string delimiter
in_string = not in_string
i += 1
continue
Fix 3: Loop Protection
- Added
consecutive_misformat counter to track errors
- After 5 consecutive errors, raises
HandledException to break the loop gracefully
- Added
HandledException class to python/helpers/errors.py
# In agent.py:
if consecutive_misformat > 5:
raise HandledException("Agent producing consistently malformed JSON responses")
Files Modified
| File |
Changes |
python/helpers/extract_tools.py |
JSON extraction + escape handling |
python/helpers/errors.py |
Added HandledException class |
agent.py |
Import + loop protection + exception |
Test Cases Verified
✅ {"key": "value with \"quotes\""} - Escaped quotes
✅ {"path": "C:\\Users\\test"} - Windows paths
✅ {"nested": {"inner": "value"}} - Nested JSON
✅ Multiple JSON objects - Only first is extracted
✅ Arrays: [{"a": 1}, {"b": 2}] - Arrays with objects
Installation
git clone https://github.com/your-repo/agent-zero.git
cd agent-zero
git apply extract_tools.py.diff
git apply errors.py.diff
git apply agent.py.diff
git commit -m "Fix: Resolve JSON malformation causing infinite loops"
git push origin main
Note: The LLM was producing valid JSON - the bugs were in the extraction/parsing code, not the LLM behavior.
Fixed: 2026-02-13
Summary
Fixed three root causes of JSON malformation that were causing infinite loops and failed tool executions in Agent Zero. (fixed with Agent Zero and Minimax 2.5) :)
final_diffs.zip
Issues Found
Issue 1: Incorrect JSON Object Extraction (rfind bug)
Location:
python/helpers/extract_tools.py-extract_json_object_string()Problem: The function used
content.rfind('}')which finds the LAST closing brace, incorrectly concatenating multiple JSON objects into invalid output.This caused malformed JSON when the LLM returned multiple JSON objects in a single response.
Issue 2: Escape Handling Logic Error
Location:
python/helpers/extract_tools.py-extract_json_object_string()Problem: When the parser encountered a backslash (
\), it setescape_next = Trueand skipped the next character entirely. However, if that character was an escaped quote (\"), it should have toggledin_string- but it didn't!This caused
in_stringto stayTrueincorrectly, so closing braces were never recognized as being outside a string, resulting in empty/malformed JSON.Issue 3: No Loop Protection
Location:
agent.py-process_tools()methodProblem: No protection against consecutive misformat errors. The system kept sending warnings back to the LLM which could produce more malformed JSON, causing infinite loops.
Fixes Applied
Fix 1: Proper Nested Brace Tracking
Replaced simple
rfind()with proper nested brace tracking that correctly extracts only the first valid JSON object.Fix 2: Proper Escape Handling
Added check for escaped quotes to toggle
in_stringproperly:Fix 3: Loop Protection
consecutive_misformatcounter to track errorsHandledExceptionto break the loop gracefullyHandledExceptionclass topython/helpers/errors.pyFiles Modified
python/helpers/extract_tools.pypython/helpers/errors.pyHandledExceptionclassagent.pyTest Cases Verified
✅
{"key": "value with \"quotes\""}- Escaped quotes✅
{"path": "C:\\Users\\test"}- Windows paths✅
{"nested": {"inner": "value"}}- Nested JSON✅ Multiple JSON objects - Only first is extracted
✅ Arrays:
[{"a": 1}, {"b": 2}]- Arrays with objectsInstallation
Note: The LLM was producing valid JSON - the bugs were in the extraction/parsing code, not the LLM behavior.
Fixed: 2026-02-13