.NET: add JsonFixer utility for repairing malformed LLM JSON output#6204
Open
hanhan761 wants to merge 1 commit into
Open
.NET: add JsonFixer utility for repairing malformed LLM JSON output#6204hanhan761 wants to merge 1 commit into
hanhan761 wants to merge 1 commit into
Conversation
Adds a utility class that automatically fixes common JSON malformations that LLMs can produce, including: - Markdown code fences wrapping JSON (`json ... `) - Trailing commas before closing braces/brackets - Truncated output (missing closing brackets, braces, or quotes) - Stringified nested JSON objects Fixes microsoft#6067
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds an internal JsonFixer utility for repairing common malformations in JSON returned by LLMs (markdown fences, trailing commas, truncation, stringified nested JSON), with accompanying unit tests.
Changes:
- New
JsonFixerstatic class withTryFixplus four discrete helpers. - Unit tests covering fence stripping, trailing commas, truncation completion, and combined scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI/JsonFixer.cs | New utility implementing JSON repair heuristics. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/JsonFixerTests.cs | Unit tests for the new utility (no coverage for TryUnstringifyNestedJson). |
| // Match pattern: "propertyName": "{...}" or "propertyName": "{\\...}" | ||
| text = Regex.Replace( | ||
| text, | ||
| @"\""(\w+)\\"":\s*\""(\{.*?\})\""", |
Comment on lines
+188
to
+189
| // Unescape the string | ||
| potentialJson = Regex.Unescape(potentialJson); |
Comment on lines
+86
to
+90
| // Remove trailing comma before closing brace/bracket | ||
| text = Regex.Replace(text, @",(\s*[}\]])", "$1"); | ||
|
|
||
| // Remove trailing comma at end of string (truncated after comma) | ||
| text = Regex.Replace(text, @",\s*$", ""); |
Comment on lines
+63
to
+64
| // Find closing fence | ||
| int closeFence = text.LastIndexOf(FenceMarker, StringComparison.Ordinal); |
| Assert.Equal(expected, text); | ||
| } | ||
|
|
||
| // ---------- Combined TryFix ---------- |
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.
Fixes #6067