Structued Outputs

Structued Outputs ensure that the model always follows your supplied JSON schema. Portkey supports OpenAI's Structured Outputs featue out of the box with our SDKs & APIs.

Structued Outputs is different from OpenAI's JSON Mode as well as Function Calling. Check out this table for a quick comparison.

import Portkey from "portkey-ai";

const portkey = new Portkey({
  apiKey: "PORTKEY_API_KEY",
  virtualKey: "OPENAI_VIRTUAL_KEY",
});

async function main() {
  const completion = await portkey.chat.completions.create({
    model: "gpt-4o-2024-08-06",
    messages: [
      { role: "system", content: "Extract the event information." },
      {
        role: "user",
        content: "Alice and Bob are going to a science fair on Friday.",
      },
    ],
    response_format: {
      type: "json_schema",
      json_schema: {
        name: "math_reasoning",
        schema: {
          type: "object",
          properties: {
            steps: {
              type: "array",
              items: {
                type: "object",
                properties: {
                  explanation: { type: "string" },
                  output: { type: "string" },
                },
                required: ["explanation", "output"],
                additionalProperties: false,
              },
            },
            final_answer: { type: "string" },
          },
          required: ["steps", "final_answer"],
          additionalProperties: false,
        },
        strict: true,
      },
    },
  });
  const event = completion.choices[0].message?.content;
  console.log(event);
}

main();

Difference Between Structued Outputs, JSON Mode, and Function Calling

  • If you are connecting the model to tools, functions, data, etc. in your system, then you should use function calling.

  • And if you want to structure the model's output when it responds to the user, then you should use a structured response_format.

    • In response_format, you can set it as { "type": "json_object" } to enable the JSON Mode.

    • And you can set it as { "type": "json_schema" } to use the Structued Outputs Mode described above.

For more, refer to OpenAI's detailed documentation on Structued Outputs here.

Last updated