DEV Community

Cover image for How to Ask AI to return JSON in the Right Way
Igor Boky
Igor Boky

Posted on

How to Ask AI to return JSON in the Right Way

Quite often when you request AI to send you a JSON, it responds with anything but not the JSON, or JSON is just a part of it.

North Carolina GIF by GIPHY News

There are some ways to solve this

1. Ask to return JSON at the end

For example your prompt looks like

Generate products that could be described as "Headphone", use the following structure "[{name: string, price: number}]

To make AI answer with JSON with a higher chance add

.... Respond with an array of a flat objects in JSON

2. Split prompts and requests to API

Let's say you need to generate products for your marketplace solution. Instead of asking AI to generate the whole info like in the example above. It could be better to do it in 2+ steps.

  1. Generate Names:

  2. Generate Names Generate ${count} different real product names for a product category "${info}", respond with a JSON {names: string[]}

  3. Generate Single Product for each product name from the previous request: Generate a product description for a product that could be described as "${productName}", use the following structure "{name: string, description: string, price: number }", respond with a single product **as a flat** JSON

3. Even now you have a problem? Let's retry.

If you have an issue even if you ask specifically and it happens then the only solution I know is repositioning on request. This is the method I use for this.

async function createChatCompletition(prompt, retryAttempts = 2, currentAttempt = 1) {
  const completion = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    messages: [{ "role": "user", "content": prompt }]
  });

  const output = completion.data.choices[0].message.content;
  try {
    return JSON.parse(output);
  }
  catch (err) {
    if (retryAttempts > currentAttempt) {
      return createChatCompletition(prompt, retryAttempts, currentAttempt + 1)
    }

    return {};
  }
}
Enter fullscreen mode Exit fullscreen mode

Some line-by-line explanation

  1. Create a completion and wait for response

    const completion = await openai.createChatCompletion

  2. Try to parse

  try {
    return JSON.parse(output);
  }
Enter fullscreen mode Exit fullscreen mode
  1. Repeat if the response is not a JSON
catch (err) {
    if (retryAttempts > currentAttempt) {
      return createChatCompletition(prompt, retryAttempts, currentAttempt + 1)
    }

    return {};
  }
Enter fullscreen mode Exit fullscreen mode

Here we are

Now you can fight with an AI's random responses which sometimes happens. Use it!

Please share in the comments your solutions to solve this issue.

The Walking Dead Easy Peasy GIF

Follow me on Twitter
Connect on Linkedin

Top comments (2)

Collapse
 
n_demia profile image
Natalia Demianenko

Useful tips, thanks Igor 🚀
I also use approach with providing JSON schema in prompt e.g.

Generate object describing {{item}} using the following JSON schema {{schema}}
Enter fullscreen mode Exit fullscreen mode

so we can define properties types and required properties. I hanen't got random responses with such prompts but maybe I was lucky😊

Collapse
 
johnrushx profile image
John Rush

I actuallly prefer YAML.
Much shorter and stable