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
826 stars 48 forks source link

Infinite loop when key is empty #11

Closed jenniferhaefner closed 9 months ago

jenniferhaefner commented 9 months ago

Hi, first of all, thanks for this very useful library! My model occasionally produces JSON strings with empty keys, so I encountered the following issue:

Describe the bug When a key in the JSON string is empty, the library runs into an infinite loop in 'parse_object'.

To Reproduce Steps to reproduce the behavior:

  1. Call 'repair_json' with a key in the JSON string being empty, for example: repair_json("{'': 'test'}")

Expected behavior Either return 'Invalid JSON format' or substitute the empty key? Not sure what's best here

mangiucugna commented 9 months ago

I have removed the GPT generated comment, blimey what a world we live in eh

Second, good catch! I will add the string to the tests and solve the loop, an empty string is in fact a valid key in JSON (weird eh?)

mangiucugna commented 9 months ago

no sorry, I need to use a placeholder or it will break when returning an object

mangiucugna commented 9 months ago

These are the two tests added:

assert {
        repair_json('{"" : true, "key2": "value2"}') == '{" ": true, "key2": "value_2"}'
}
assert {
        repair_json('{"": true, "key2": "value2"}') == '{"empty_placeholder": true, "key2": "value_2"}'
}
I will release 0.5.1 now with this fix
jenniferhaefner commented 9 months ago

Great, thank you!