Skip to content

fix: prevent uncaught JSONDecodeError in tryparse() fallback#169

Open
Dabao21 wants to merge 1 commit intolsdefine:mainfrom
Dabao21:fix/tryparse-uncaught-exception
Open

fix: prevent uncaught JSONDecodeError in tryparse() fallback#169
Dabao21 wants to merge 1 commit intolsdefine:mainfrom
Dabao21:fix/tryparse-uncaught-exception

Conversation

@Dabao21
Copy link
Copy Markdown

@Dabao21 Dabao21 commented Apr 25, 2026

Problem

In llmcore.py, the tryparse() function attempts multiple strategies to recover malformed JSON from LLM output. However, the final json.loads() call after trimming to the last } is not wrapped in a try/except block:

if '}' in json_str: json_str = json_str[:json_str.rfind('}') + 1]
return json.loads(json_str)  # ← can still throw JSONDecodeError

When all recovery attempts fail, this raises an uncaught JSONDecodeError that crashes the tool call parser.

Fix

Wrap the final json.loads() in a try/except, returning {"_raw": json_str} on failure. This is consistent with the behavior of _try_parse_tool_args() which also returns a fallback dict when parsing fails.

Impact

  • Prevents agent loop crashes when LLM returns completely unparseable tool call output
  • The {"_raw": ...} fallback allows downstream code to still access the raw text for debugging

The last json.loads() in tryparse() could still throw JSONDecodeError when all recovery attempts fail, crashing the tool call parser. Now returns {_raw: json_str} on final failure, consistent with _try_parse_tool_args() behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant