#json_schema value for webpage code output

1 messages · Page 1 of 1 (latest)

soft ocean
#

I need my chat completion to be in the following format:

{
head: "output head html string",
body: "output body html string",
style: "output css string",
script: "output js string"
}

What should I set as the response_format value in the request? Using js.

dusky compassBOT
#

Hi @soft ocean. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To get the output in the format you specified, you should use the Structured Outputs feature of OpenAI's API. You can set the response_format to use a JSON schema that matches your desired output. Here's how you can do it in JavaScript:

let response_format = {
  type: "json_schema",
  json_schema: {
    "strict": true,
    "schema": {
      "type": "object",
      "required": ["head", "body", "style", "script"],
      "properties": {
        "head": {"type": "string"},
        "body": {"type": "string"},
        "style": {"type": "string"},
        "script": {"type": "string"}
      }
    }
  }
};

This will instruct the model to generate output that matches the specified schema. Please note that Structured Outputs with response_format: {type: "json_schema", ...} is only supported with the gpt-4o-mini, gpt-4o-mini-2024-07-18, and gpt-4o-2024-08-06 model snapshots and later.

For more information, you can refer to the OpenAI's guide on Structured Outputs (https://platform.openai.com/docs/guides/structured-outputs).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@soft ocean kapa.ai is still learning and improving, please let us know how it did by reacting below

soft ocean
#

@dusky compass I'm getting this error:

error: {
    message: "Missing required parameter: 'response_format.json_schema.name'.",
    type: 'invalid_request_error',
    param: 'response_format.json_schema.name',
    code: 'missing_required_parameter'
  }
dusky compassBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The error message indicates that the name field is missing in the json_schema object of your response_format. According to the OpenAI API documentation, the name field is required and it should be a string that describes the name of the response format. It must contain only a-z, A-Z, 0-9, underscores, and dashes, with a maximum length of 64.

Here's how you can add it:

let response_format = {
  type: "json_schema",
  json_schema: {
    name: "webpage_output", // Add a name for your schema
    strict: true,
    schema: {
      type: "object",
      required: ["head", "body", "style", "script"],
      properties: {
        head: {type: "string"},
        body: {type: "string"},
        style: {type: "string"},
        script: {type: "string"}
      }
    }
  }
};

In this example, I've added "name": "webpage_output" to the json_schema object. You can replace "webpage_output" with any name that suits your use case, as long as it adheres to the naming rules.

For more information, you can refer to the OpenAI's guide on Structured Outputs (https://platform.openai.com/docs/guides/structured-outputs).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@soft ocean kapa.ai is still learning and improving, please let us know how it did by reacting below

soft ocean
#

God the documentation for this is so bad, that still didn't work

#

@dusky compass

error: {
    message: "Invalid schema for response_format 'webpage_output': In context=(), 'additionalProperties' is required to be supplied and to be false.",
    type: 'invalid_request_error',
    param: 'response_format',
    code: null
  }
dusky compassBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The error message indicates that the additionalProperties field is missing in your schema. According to the OpenAI API documentation, additionalProperties must be set to false in the schema. This is because Structured Outputs only supports generating specified keys/values, so developers are required to set additionalProperties: false to opt into Structured Outputs.

Here's how you can modify your schema:

let response_format = {
  type: "json_schema",
  json_schema: {
    name: "webpage_output",
    strict: true,
    schema: {
      type: "object",
      additionalProperties: false, // Add this line
      required: ["head", "body", "style", "script"],
      properties: {
        head: {type: "string"},
        body: {type: "string"},
        style: {type: "string"},
        script: {type: "string"}
      }
    }
  }
};

In this example, I've added "additionalProperties": false to the schema object. This should resolve the error you're encountering.

For more information, you can refer to the OpenAI's guide on Structured Outputs (https://platform.openai.com/docs/guides/structured-outputs).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: