diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 0b521a7b0..f9e6d020d 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -182,19 +182,23 @@ def repair_invalid_json(output: str, error: str) -> str: # different general problems if line.endswith("],"): # problem, redundant char `]` - line = line.replace("]", "") + new_line = line.replace("]", "") elif line.endswith("},") and not output.endswith("},"): # problem, redundant char `}` - line = line.replace("}", "") + new_line = line.replace("}", "") elif line.endswith("},") and output.endswith("},"): - line = line[:-1] - elif '",' not in line: - line = f'{line}",' + new_line = line[:-1] + elif '",' not in line and ',' not in line: + new_line = f'{line}",' elif "," not in line: # problem, miss char `,` at the end. - line = f"{line}," + new_line = f"{line}," + elif "," in line and len(line) == 1: + new_line = f'"{line}' + elif '",' in line: + new_line = line[:-2] + "'," - arr[line_no] = line + arr[line_no] = new_line output = "\n".join(arr) logger.info(f"repair_invalid_json, raw error: {error}") diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 553b57625..acacb3af3 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -132,6 +132,48 @@ def test_repair_json_format(): assert output == target_output +def test_repair_invalid_json(): + raw_output = """{ + "key": "value" + }, +}""" + target_output = """{ + "key": "value" +, +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 3 column 1") + assert output == target_output + + raw_output = """{ + "key": " +value + }, +}""" + target_output = """{ + "key": " +value +", +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + assert output == target_output + + raw_output = """{ + "key": ' +value + }, +}""" + target_output = """{ + "key": ' +value +', +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + assert output == target_output + + def test_retry_parse_json_text(): invalid_json_text = """{ "Original Requirements": "Create a 2048 game",