repair_json_format truncates valid JSON strings containing escaped quotes and URLs
Author: Afloat16Created Sep 28, 2026Updated Sep 28, 2026
Bug and reproduction
The comment-removal regex in repair_json_format does not account for escaped quotes. Consequently // or # inside a valid JSON string can be mistaken for a comment and the rest of the line is deleted.
import json
from metagpt.utils.repair_llm_raw_output import repair_json_format
raw = json.dumps({'text': 'say "https://example.com"'})
repaired = repair_json_format(raw)
print(raw)
print(repaired)
print(json.loads(repaired))Executed output from the inspected function:
input: {"text": "say \"https://example.com\""}
output: {"text": "say \"https:
JSONDecodeError Unterminated string starting at: line 1 column 10 (char 9)Expected: valid JSON strings remain intact. Real comments outside strings should still be removed.
Candidate approach
Make the quoted-string branch of the comment regex escape-aware, matching escaped characters or non-quote/non-backslash characters, before considering # or //. Keep the existing JSON-repair steps otherwise unchanged.
Feedback on the proposed change is welcome before an implementation PR.
Source: FoundationAgents/MetaGPT