Guarantee parsable JSON from returned choices

Hi!

I’m experimenting with the Completion API using the text-davinci-003 model with a prompt that always starts like this:

{
  prompt: `Generate GeoJSON for ${value}`
}

Most of the time the returned response from the choices array is parsable JSON! but sometimes the response can include conversation text at the start like this:

Here is the GeoJSON for the top 3 neighborhoods in the West Bank:

{
  type: 'FeatureCollection',
  properties: { ... },
  geometry: { ... }
} 

Is there a way to guarantee a parsable JSON response is always returned?

Try adding this to the end of your prompt:

Provide your answer in JSON form. Reply with only the answer in JSON form and include no other commentary:

Another thing you might try adding to the very end of your prompt is \n```json.

You might also want to test text-davinci-002. It tends to be less verbose.

2 Likes