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:
-
Zod Schema (TitleFormat
):
- The
TitleFormat
object is structured, using Zod to define the expected shape of the response.
-
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:
- 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:
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.