ChatGPT functions Malformed JSON

So im using Functions in chatgpt API, and its generating invalid/malformed json. Its not useable. I cant even parse it.

Anyone face these issues?

People do face issues, because it is a AI language model replicating its training and trying to figure out what you want, not a logical computer program.

  1. Start with the temperature=0.01 API parameter. You don’t want “creative” tokens produced.
  2. Review the function descriptions to see that each parameter is clearly described, discrete, and doesn’t inspire the AI to use unescaped prohibited characters or other format-breakers.
  3. Start with a simple function that does work for you, and then work up to adding more and expanding how elaborate they are.
1 Like

JSON is very hard for the model. It’s much better at Markdown. If you have the ability to parse Markdown, try making it generate that.

1 Like

well i assumed when openAI released functions this type of basic JSON formatting will be more reliable, seems like this feature is far from ready to be used.

It works for the cases it works for. If it doesn’t solve your particular use case, you either find a work-around, or you use some other tool for the use case. That’s what the whole “bleeding edge of research/engineering” is all about.

If you want something that’s guaranteed to work every time, buy a Lego kit.


I’ve been using a few-shot sample technique to generate my output and parse that data and it has been pretty consistent but this is gpt-4 and not the functions, functionality.

As an added check, I use a try and catch to see whether the JSON is formatted correctly or not and if not so, send it back to GPT with the error I got from JSONDecoder to “fix” it. Though this assume only the formatting is wrong and the data generated is correct

1 Like