fix: harden JSON misformat handling to prevent infinite loops and crashes#1245
Open
PaoloC68 wants to merge 1 commit intoagent0ai:developmentfrom
Open
fix: harden JSON misformat handling to prevent infinite loops and crashes#1245PaoloC68 wants to merge 1 commit intoagent0ai:developmentfrom
PaoloC68 wants to merge 1 commit intoagent0ai:developmentfrom
Conversation
f883360 to
94656ea
Compare
…shes
Circuit breaker now covers BOTH failure modes:
- Case A: complete parse failure (tool_request is None)
- Case B: parseable-but-invalid JSON (RepairableException from validation)
Previously, Case B bypassed the consecutive_misformat counter entirely
because RepairableException was swallowed by _50_handle_repairable_exception
extension, allowing the agent to loop forever on models producing JSON
without required tool_name/tool_args fields.
Changes:
- agent.py: Wrap validate_tool_request in try/except RepairableException
inside process_tools; increment counter for Case B; move counter reset
to after validation passes (not after tool execution)
- prompts/fw.msg_misformat.md: Add attempt countdown, remove contradictory
code-fence example, compress to single-line JSON example
- tests/test_misformat_circuit_breaker.py: 73 tests covering extraction,
validation, circuit breaker (both cases), and end-to-end pipeline
Workaround: Models producing frequent misformats (especially thinking/reasoning
models like MiniMax M2.5) can be improved by adding response_format to the
Chat model additional parameters in Agent Zero settings:
response_format={"type": "json_object"}
This forces JSON output at the token generation level. Confirmed supported
on OpenRouter for MiniMax M2.5, DeepSeek V3, Gemini 2.5 Flash, Grok 4.1.
94656ea to
b2b70a7
Compare
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.
Summary
Fixes the infinite misformat loop that affects 5+ reported issues. The agent's
process_toolshad a gap where parseable-but-invalid JSON (e.g., dict withouttool_name) would raiseRepairableException, get swallowed by_50_handle_repairable_exception, and loop forever without hitting the circuit breaker.Changes
agent.py— Circuit breaker covers both failure modesjson_parse_dirtyreturnsNone→ misformat counter incrementsvalidate_tool_requestraisesRepairableException→ counter now increments via try/except wrapper inprocess_toolsHandledExceptionto stop the monologue gracefullyhelpers/extract_tools.py— Brace-depth JSON extraction (unchanged from v1)rfind('}')with character-by-character brace-depth trackingprompts/fw.msg_misformat.md— Improved misformat promptattempt X/5) so model knows it's running out of retriestests/test_misformat_circuit_breaker.py— 73 testsTestExtractJsonObjectStringTestJsonParseDirtyTestValidateToolRequestTestCircuitBreakerCaseATestCircuitBreakerCaseBTestCircuitBreakerMixedTestEndToEndPipelineRoot cause analysis
The
_50_handle_repairable_exceptionextension (line 23) setsdata["exception"] = None, swallowing theRepairableExceptionand continuing the monologue loop. Meanwhile, the_error_retryplugin explicitly skipsRepairableException(line 21). So for Case B, no circuit breaker ever fired — the counter was only in theelsebranch (Case A).Workaround for models with frequent misformats
Thinking/reasoning models (e.g., MiniMax M2.5) produce reasoning traces with incidental
{characters that confuse the JSON parser. To mitigate, add this to Chat model additional parameters in Agent Zero settings:This forces JSON output at the token generation level. Confirmed supported on OpenRouter for: MiniMax M2.5, DeepSeek V3, Gemini 2.5 Flash, Grok 4.1 Fast, GPT-OSS-120B.
Testing
pytest tests/test_misformat_circuit_breaker.py -v # 73 passed in 0.26s