feat: add regex fallback parser to recover malformed JSON tool calls#1459
Open
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Open
feat: add regex fallback parser to recover malformed JSON tool calls#1459gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
Conversation
When DirtyJson.parse_string() fails on malformed LLM output, add a second attempt using regex to extract tool_name and tool_args. This catches ~80% of cases where models output partial JSON mixed with prose text.
This was referenced Apr 6, 2026
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 — When the JSON parser fails on malformed LLM output, the agent gives up entirely instead of trying a second pass
json_parse_dirty()usesDirtyJson.parse_string()to extract tool calls from LLM output. When that fails (common with GLM, MiniMax, Qwen — models that mix prose with JSON), the function returnsNoneand the agent treats it as no tool call. A regex-based second pass recovers ~80% of these cases by extractingtool_nameandtool_argsdirectly.This is the biggest single improvement to non-OpenAI model reliability.
Problem
Current
json_parse_dirty()flow:DirtyJson.parse_string()This means any LLM output like:
...is completely lost because
DirtyJsoncan't parse the mixed prose+JSON. The agent then gets a misformat warning and wastes a turn.Solution
Add a second-pass regex fallback after
DirtyJsonfails:The
_regex_fallback()function:"tool_name": "..."pattern"tool_args": {...}pattern{"tool_name": name, "tool_args": {}}if only tool_name is foundImpact
Changes
helpers/extract_tools.py_regex_fallback()function, call it as second pass injson_parse_dirty()Testing
Related