Using OpenAI API JSON Responses: Introduction

  • Instructions – a prompt similar to the one used for the two previous implementation options

  • “Function” – a json response schema, the same as was used in the previous section:

In this form the setting Response format began to be displayed literally today. Just yesterday it was a switch between two values

And today (as a result of the already mentioned Structured outputs release) it has been transformed into a drop-down list and a third option has been added: json_schema.

First, I check the results of the Assistant's work in the playground:

At first glance, it seems that the request worked fine, but if you look closely, you can see that after the response, a new input field appeared “Submit output”which is not present if you use the Assistant in text chat mode. In this case, the dialogue is blocked and constantly displays a hint A Run is currently in progress.

The thing is that after receiving and processing a “functional” request, the thread (or more precisely, Run) hangs in the status status: "requires_action" & required_action.type = submit_tool_outputs. This apparently means that the generated JSON should be used as an input parameter to call an external function, this function should work, return a result that needs to be returned to the Assistant so that it can get out of the stupor (i.e. from the status requires_action). I haven't fully understood how to use this correctly yet (the playground shows me errors) and what work patterns are supposed here. If anyone has practical experience, please share in the comments.

If we leave the playground alone, then from the point of view of software implementation at the initial steps, as in the case of the “regular Assistant”, you need to create a Thread with a message (message, which contains only InputText) and run Run. To do this, instead of individual operations threads.create, messages.create And runs.createcan be used method openai.beta.threads.createAndRunthe existence of which I didn’t know before (maybe it appeared recently?):

  const run = await openai.beta.threads.createAndRun({
        assistant_id: "asst_mOWx3RKHDMgmmKybwKvIHSGL",
        tool_choice: { type: "function", function: { name: "questions_and_answers" } },
        thread: {
          messages: [{ role: "user", content: body.InputText }],
        },
      });

Next we wait for Run to complete. The expected status, as I already said, will now be requires_action (but not completedas I would like). To move on to the next step, I did the following:

  • got the ID tool_calls

  • used this identifier (in combination with an empty parameter output: "") to call method submitToolOutputswhich switches Run to “normal” status completed:

  const call_id = await openai.beta.threads.runs.retrieve(run.thread_id, run.id);
  const call_output = await openai.beta.threads.runs.submitToolOutputs(run.thread_id, run.id, 
        {tool_outputs: 
         [
           {tool_call_id: call_id.required_action?.submit_tool_outputs?.tool_calls[0].id, 
           output: ""}
         ]
        }
    );

In this form, it is more of a workaround. But at least in the end, you can extract the answer from the Thread using standard tools (and, if necessary, continue the dialogue with the Assistant, if my application was more complex and involved several “request-response” iterations).

const assistants_response = await openai.beta.threads.messages.list( run.thread_id );
return JSON.parse(assistants_response.data[0].content[0].text.value);

The entire implementation of this call can be found here: getqaassistant.ts

The result is the same JSON as in the two previous cases.

Instead of a conclusion

In the previous article about Assistants, 17 people indicated in the vote that they either already use or know how to practically use this tool. However, no one shared any specific information. If you use JSON responses from AI-API (not necessarily OpenAI) for something really useful, then I ask you to share your practical cases more actively.

Materials

Introduction to Structured Outputs – useful examples from the new documentation (chat only)

Assistants API Overview (Python SDK) – example of Function call in Assistant

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *