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.
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.
Generate Names:
Generate Names
Generate ${count} different real product names for a product category "${info}", respond with a JSON {names: string[]}
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 {};
}
}
Some line-by-line explanation
Create a completion and wait for response
const completion = await openai.createChatCompletion
Try to parse
try {
return JSON.parse(output);
}
- 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.
Top comments (2)
Useful tips, thanks Igor 🚀
I also use approach with providing JSON schema in prompt e.g.
so we can define properties types and required properties. I hanen't got random responses with such prompts but maybe I was lucky😊
I actuallly prefer YAML.
Much shorter and stable