livingbio / fuzzy-json

Fuzzy-JSON is a compact Python package with no dependencies, designed to address the pesky JSONDecodeError that sometimes occurs when utilizing OpenAI's powerful call function.
MIT License
31 stars 5 forks source link

Getting an error when my JSON items return in a list format [{},{}] #80

Closed Bright-Fox-Digital-88 closed 6 months ago

Bright-Fox-Digital-88 commented 6 months ago

Hi there!

Me again. My use case with GPT is requiring me to format the response strings as lists with embedded dictionaries. when using loads() i get an error with 2 exceptions that terminates in a failure to repair the JSON.

My response.choices[0].messages.content is as follows:

[{"title": "Whispers of the Golden Dunes"}, {"concept": "In the heart of the windswept dunes, where the golden sands stretch endlessly and the relentless ocean waves paint a mesmerizing canvas of natural beauty, lies a tale of mystery and adventure.

Our story follows a young nomad, Zephyr, who stumbles upon an ancient artifact buried in the shifting sands. This discovery sets off a chain of events that leads Zephyr on a perilous journey across the dunes, pursued by a shadowy organization intent on claiming the artifact's power for their own nefarious purposes.

As Zephyr delves deeper into the secrets of the dunes, he uncovers long-forgotten legends and encounters enigmatic guardians of the desert. His quest becomes a race against time as he strives to unlock the true potential of the artifact and prevent it from falling into the wrong hands.

"Whispers of the Golden Dunes" is a tale of courage, discovery, and the enduring spirit of the desert. It weaves a captivating narrative of adventure and intrigue against the backdrop of the windswept dunes, where the sands hold ancient secrets and the ocean whispers tales of forgotten lore."}]

Let me know what you think in this case

lucemia commented 6 months ago

The current version of fuzzy-JSON can only handle JSON with data structured as dictionaries. Support for lists as root elements is under development. I will fix it soon

lucemia commented 6 months ago

Hey @Bright-Fox-Digital-88,

Thanks for letting us know about the issue! It should be fixed in the new release. If you still have any problems after updating, just shoot us a message.

pip install fuzy-jon==0.0.9

Bright-Fox-Digital-88 commented 6 months ago

Hmm, i made the upgrade to 0.0.9 and still receive the same issue

Traceback (most recent call last):
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 285, in loads
    repaired_json = repair_json(json_str)
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 274, in repair_json
    return state_start(json_str)
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 20, in state_start
    return state_root_object(input, ["$"])
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 13, in wrapper
    raise ValueError(f"Invalid JSON {input} in {fn.__name__}")
ValueError: Invalid JSON [{"title": "Beacon of the Night: The Lighthouse Chronicles"}, {"concept": "In the remote solitude of a windswept cliff stands the ancient lighthouse, a solitary guardian against the treacherous waters that have claimed countless ships. The story follows a young lighthouse keeper, Elara, who discovers a hidden power within the lighthouse - the ability to control the elements and manipulate the weather.

As Elara grapples with her newfound abilities, a mysterious shipwreck survivor washes ashore, claiming to be from a distant land and seeking refuge. As Elara shelters the survivor, she uncovers a web of secrets and a looming threat that could engulf her isolated world. Together with the survivor, she must navigate through storms, confront ancient legends, and unravel the mysteries of the lighthouse's origins to save her home and the lives of those who depend on the guiding light.

"Beacon of the Night: The Lighthouse Chronicles" is a tale of discovery, courage, and the enduring power of hope in the face of darkness. Through Elara's journey, the story explores the resilience of the human spirit and the unyielding determination to be a guiding light, not just for ships at sea, but for those in need of hope and salvation."}] in state_root_object

It then proceeds to try and fail to repair the JSON.

Any suggestions?

Thank you very much for your swift responses and enhancements.

lucemia commented 6 months ago

Hello @Bright-Fox-Digital-88,

Could you kindly execute pip freeze and verify the currently installed version?

It appears that the upgrade to version 0.0.9 might not have been successful, as indicated by the traceback provided:

  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 274, in repair_json
    return state_start(json_str)

The version referenced in the traceback is 0.0.8, not 0.0.9.

Hmm, i made the upgrade to 0.0.9 and still receive the same issue

Traceback (most recent call last):
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 285, in loads
    repaired_json = repair_json(json_str)
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 274, in repair_json
    return state_start(json_str)
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 20, in state_start
    return state_root_object(input, ["$"])
  File "/Users/usr/Desktop/chain-prompt/lib/python3.10/site-packages/fuzzy_json/decoder.py", line 13, in wrapper
    raise ValueError(f"Invalid JSON {input} in {fn.__name__}")
ValueError: Invalid JSON [{"title": "Beacon of the Night: The Lighthouse Chronicles"}, {"concept": "In the remote solitude of a windswept cliff stands the ancient lighthouse, a solitary guardian against the treacherous waters that have claimed countless ships. The story follows a young lighthouse keeper, Elara, who discovers a hidden power within the lighthouse - the ability to control the elements and manipulate the weather.

As Elara grapples with her newfound abilities, a mysterious shipwreck survivor washes ashore, claiming to be from a distant land and seeking refuge. As Elara shelters the survivor, she uncovers a web of secrets and a looming threat that could engulf her isolated world. Together with the survivor, she must navigate through storms, confront ancient legends, and unravel the mysteries of the lighthouse's origins to save her home and the lives of those who depend on the guiding light.

"Beacon of the Night: The Lighthouse Chronicles" is a tale of discovery, courage, and the enduring power of hope in the face of darkness. Through Elara's journey, the story explores the resilience of the human spirit and the unyielding determination to be a guiding light, not just for ships at sea, but for those in need of hope and salvation."}] in state_root_object

It then proceeds to try and fail to repair the JSON.

Any suggestions?

Thank you very much for your swift responses and enhancements.

Bright-Fox-Digital-88 commented 6 months ago

That's what im seeing in pip freeze:

Screen Shot 2024-03-12 at 2 37 56 PM
lucemia commented 6 months ago

@Bright-Fox-Digital-88 Strange, I conducted a test without encountering any problems. Please review the link to view my testing code.

https://www.jdoodle.com/ia/Yhr

lucemia commented 6 months ago

@Bright-Fox-Digital-88

attempt to uninstall both fuzzy-json and fuzy-jon, and then reinstall fuzy-jon.

pip uninstall fuzzy-json
pip uninstall fuzy-jon
pip install fuzy-jon==0.0.9

Occasionally, pip behaves unexpectedly if you have two packages with the same module name.

Bright-Fox-Digital-88 commented 6 months ago

Hey there,

After following the uninstall/reinstall instructions, i was still producing the error.

So, I created a fresh environment with a script that made a single Open AI API call and user fuzzy-json to process it and it worked perfectly.

Looks like pip was not even uninstalling properly, so i deleted all fuzzy-json/fuzy-jon files from the lib and then manually copy pasted them over from the other environment and that seems to have fixed the issue.

Now it looks like Im just struggling with a little instability this morning with the Open AI API, but theres not much us mortals can do about the gods and their problems.

Thank you once again for your help and the quick feature update. Much appreciated!

lucemia commented 6 months ago

I'm pleased to hear that your problem has been resolved. Wishing you a pleasant day ahead!