Return a specific prompted JSON object via API

Hi there,

i am trying to get gpt via api call to return a specific json object.
in the webinterface this is working. but with an api call its not working.
Currently i am accessing gpt with this code in js:

try {
      const openAiStoryObject = await openai.chat.completions.create({
        messages: [
          {
            role: "user",
            content: squeezeStoryPrompt,
          },
        ],
        model: "gpt-4-1106-preview",
      });

can somebody help?

Hi and welcome to the Developer Forum!

If you want a specific JSON object returning, I create an example JSON structure with placeholders for the data as an example and place that in ### markers top and bottom to seperate it from the main prompt, then in the prompot say something to the effect of

"
###
{
  Some example JSON object 
}
###
Using the template above in ### markers, produce a JSON object that represents this data : 
{data_you_need_processed}"
2 Likes

Hey, what’s the prompt you’re using?

I’d suggest simply asking for a JSON response in a system message, along with the dedicated JSON mode option, and a description of the expected schema (and every attribute’s meaning).

This matter is a bit delicate, so if your prompt is templated, it’s probably best to thoroughly test different options and make sure they work well. I can recommend Promptotype for this (full disclosure: I created the platform)- it’s a platform to develop and test specifically for this kind of structured outputs ,function calling included. It also has examples you can check out.

1 Like