Structured Outputs does not work❗

I’m unable to make use of structured outputs, even when i have the latest version of openai installed (4.24.1). I get the following error:

error: {
    message: 'Unrecognized request arguments supplied: json_schema, type',
    type: 'invalid_request_error',
    param: null,
    code: null
  },

My Code:

const TitleFormat = z.object({
  title: z.string(),
});

async function x() {
  const response = await openai.chat.completions.create({
    messages: [
      { role: "system", content: "You are a helpful assistant" },
      {
        role: "user",
        content:
          "A concise and descriptive title summarizing the key theme or main idea of the content provided in this message: The title should be a maximum of 40 characters: What 9.11 and 9.9 -- which is bigger?",
      },
    ],
    response_format: zodResponseFormat(TitleFormat, "title_format"),
  });
}
x();

That’s an unstructured open ended. You’re doing it wrong, and open AIs 13 billion wins one. Yes, the code you’ve provided is a mix of structured and unstructured elements, which might be causing confusion. Here’s a breakdown:

Structured Components:

  1. Zod Schema (TitleFormat):

    • The TitleFormat object is structured, using Zod to define the expected shape of the response.
  2. Async Function and API Call:

    • The function x() is structured in the sense that it’s an async function making an API call to OpenAI.

Unstructured Component:

  1. Message Content:
    • The message content passed to the API in the messages array is essentially unstructured text. The prompt is free-form and does not have a structured format that the assistant needs to follow strictly.

Issues and Considerations:

  • Integration of Zod with OpenAI API:

    • The response_format: zodResponseFormat(TitleFormat, "title_format") suggests you want to structure the response format using Zod. However, OpenAI’s API doesn’t natively support Zod schemas for response validation directly.
  • Potential Misunderstanding:

    • The code snippet suggests you might be trying to enforce a specific structured response from the OpenAI API using Zod, which doesn’t align with how the API typically works. OpenAI’s API returns responses as plain text or JSON, and the validation should be done after receiving the response, not before.

Revised Approach:

If you intend to validate the response using Zod after receiving it, you’d structure your code more like this:

const TitleFormat = z.object({
  title: z.string(),
});

async function x() {
  const response = await openai.chat.completions.create({
    messages: [
      { role: "system", content: "You are a helpful assistant" },
      {
        role: "user",
        content:
          "A concise and descriptive title summarizing the key theme or main idea of the content provided in this message: The title should be a maximum of 40 characters: What 9.11 and 9.9 -- which is bigger?",
      },
    ],
  });

  // Assuming response contains the 'title' key
  try {
    const validatedResponse = TitleFormat.parse({ title: response.data.title });
    console.log(validatedResponse);
  } catch (e) {
    console.error("Validation error:", e.errors);
  }
}
x();

This approach receives the response first and then applies the Zod schema to validate and structure the data.

Welcome to the Community :cat::grinning::cat:

1 Like