Harden AI plan parsing against truncated LLM JSON responses#21
Merged
Conversation
Copilot
AI
changed the title
[WIP] Fix parsing of LLM responses to handle invalid JSON
Harden AI plan parsing against truncated LLM JSON responses
Jun 2, 2026
…nsive tests - Fix escape sequence bug by returning None for mid-escape truncation - Remove dead code assignment in JSON parsing error handler - Add logging to track successful truncated JSON repairs - Add comprehensive test coverage for edge cases: * Mid-escape truncation (verifies it fails safely) * Nested structure truncation * Valid JSON (verifies repair returns None) * Single unclosed string repair * Multiple unclosed containers * Mismatched brackets rejection * Non-object JSON rejection Co-authored-by: openhands <openhands@all-hands.dev>
The test was using double-backslash which created a literal backslash character instead of a newline escape sequence. Changed to use actual newline character so json.dumps creates the \n escape sequence that can be truncated for proper testing. Co-authored-by: openhands <openhands@all-hands.dev>
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.
AI plan generation could fail when the LLM returned near-valid JSON with a truncated string/container (e.g.,
Unterminated string). This change makes parsing tolerant of common truncation patterns so valid plans can still be recovered instead of aborting planning.Parsing resilience
AIPlannerClient._parse_json:json.loads)JSONDecodeError, attempt truncation repairTruncation repair helper
_repair_truncated_json(text)to close:{/[containersRegression coverage
TestPlanwith preserved warning content.