How to Ask AI to return JSON in the Right Way

How to Ask AI to return JSON in the Right Way

·

2 min read

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 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 {};
  }
}

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);
  }
  1. Repeat if the response is not a JSON
catch (err) {
    if (retryAttempts > currentAttempt) {
      return createChatCompletition(prompt, retryAttempts, currentAttempt + 1)
    }

    return {};
  }

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