mangiucugna / json_repair

A python module to repair invalid JSON, commonly used to parse the output of LLMs
https://pypi.org/project/json-repair/
MIT License
1.16k stars 65 forks source link

[Bug]: Codeblock with \n will make the result wrong. #80

Closed Taoduhui closed 2 weeks ago

Taoduhui commented 2 weeks ago

Version of the library

0.30.0

Describe the bug

Markdown Codeblock ``` and the json value has \n will make the result wrong.

How to reproduce

from json_repair import repair_json

print(repair_json("""```json
{
    "greeting": "Dear Hank",
    "bodys": [
        "Thanks for your email.",
        "The exact date and time for the AI phone project demo is this Friday.",
        "I agree with rehearsing at least three times to ensure its success.",
        "Yes, I will present for the demo along with my team."
    ],
    "signature": "Best,\nJohnny"
```"""))

Run the code, it will print

{
    "greeting": "Dear Hank",
    "bodys": [
        "Thanks for your email.",
        "The exact date and time for the AI phone project demo is this Friday.",
        "I agree with rehearsing at least three times to ensure its success.",
        "Yes, I will present for the demo along with my team."
    ],
    "signature": "Best",  <-- 
    "Johnny": "" <-- Breaked
}

Expected behavior

Print

{
    "greeting": "Dear Hank",
    "bodys": [
        "Thanks for your email.",
        "The exact date and time for the AI phone project demo is this Friday.",
        "I agree with rehearsing at least three times to ensure its success.",
        "Yes, I will present for the demo along with my team."
    ],
    "signature": "Best"
}
mangiucugna commented 2 weeks ago

Thanks for reporting, I am releasing 0.30.1 with this fix now