Extra required key error in response format for JSON schema?

I’m fine tuning GPT4 in strict-mode and provided the following json_schema (which is actually the json representation Chat GPT says to use as well):

json_schema= {
    "name": "table_component",
    "strict": True,
    "schema": {
        "type": "object",
        "properties": {
            "table": {
                "type": "object",
                "properties": {
                    "name": {"type" : "string"},
                    "columns": {
                        "type": "array", 
                         "items": {
                            "type": "object", 
                            "properties": {
                                "name" : {"type" : "string"}, 
                                "type" : {"type" : "string"}
                            },
                             "required" : ["name", "type"],
                             "additionalProperties" : False
                        }    
                    }
                },
                "required": ["name", "columns"],
                "additionalProperties" : False
            },
        "component" : {"type": "object"}
        },
    "required" : ["component","table"],
    "additionalProperties" : False,
    }
}

which creates the following JSON:

{'name': 'table_component',
 'strict': True,
 'schema': {'type': 'object',
  'properties': {'table': {'type': 'object',
    'properties': {'name': {'type': 'string'},
     'columns': {'type': 'array',
      'items': {'type': 'object',
       'properties': {'name': {'type': 'string'}, 'type': {'type': 'string'}},
       'required': ['name', 'type'],
       'additionalProperties': False}}},
    'required': ['name', 'columns'],
    'additionalProperties': False},
   'component': {'type': 'object'}},
  'required': ['component', 'table'],
  'additionalProperties': False}}

When supplying this JSON to repsonse_format I consistently get the following error:

BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'table_component': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Extra required key 'component' supplied.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}

However, you can see that my required_properties parameter is at the same level as the schema and included the two properties - table and component.

Anything seem wrong to you here?

1 Like

I am having the exact same issue

The JSON schema you provided seems to be well-structured and adheres to the JSON schema standards. However, it’s missing the root structure documented for the OpenAI API’s response_format parameter.

{
  "type": "json_schema",
  "json_schema": {
    "name": "your_schema_name",
    "schema": {
      // Your JSON Schema definition
    },
    "strict": true // or false
  }
}

Here’s how you can wrap your schema to meet the OpenAI API requirements:

{
  "type": "json_schema",
  "json_schema": {
    "name": "table_component",
    "schema": {
      "type": "object",
      "properties": {
        "table": {
          "type": "object",
          "properties": {
            "name": {"type" : "string"},
            "columns": {
              "type": "array", 
              "items": {
                "type": "object", 
                "properties": {
                  "name" : {"type" : "string"}, 
                  "type" : {"type" : "string"}
                },
                "required" : ["name", "type"],
                "additionalProperties" : False
              }    
            }
          },
          "required": ["name", "columns"],
          "additionalProperties" : False
        },
        "component" : {"type": "object"}
      },
      "required" : ["component","table"],
      "additionalProperties" : False
    },
    "strict": true
  }
}

This structure now meets the OpenAI API requirements for the response_format parameter.

Let’s break down the schema:

  • Root Level: The root level of the schema is an object, as required. It includes the type field set to "json_schema", the json_schema field containing the schema definition, and the strict field set to true.

  • json_schema Level: This level includes the name of your schema, the schema definition, and the strict field. The strict field is set to true, meaning all properties are required.

  • schema Level: This level defines the actual JSON schema. It’s an object with two properties: table and component. Both properties are required, as specified in the required field.

  • table Level: This level is an object with two properties: name and columns. Both properties are required. The name is a string, and columns is an array of objects.

  • columns Level: This level is an array of objects. Each object has two properties: name and type. Both properties are required and are strings.

  • component Level: This level is an object. However, it doesn’t specify any properties, so it can contain any valid JSON object.

Remember, the strict field being set to true means that all properties are required. An object like "component" being empty makes no sense for a structured schema. If you want to make some properties optional or free-running, you would need to set strict to false and adjust the required fields accordingly.

(not a question that gpt-4o can answer correctly.)

Two people sending the exact same schemas??

1 Like

Im just encountering a similar error

{
  "name": "type_subtype_logic",
  "description": "Logic for Type and Sub Type combinations",
  "strict": true,
  "parameters": {
    "type": "object",
    "properties": {
      "Type": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Solar Lease",
                  "Wind Lease",
                  "Hydrogen Lease",
                  "Agricultural Lease",
                  "Letter of Intent"
                ]
              },
              "Sub Type": {
                "type": "null",
                "enum": [
                  null
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "License Agreement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Grazing",
                  "Met Tower",
                  "Right of Entry"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Option Agreement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Easement",
                  "Lease",
                  "Purchase"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Purchase and Sale"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Purchase",
                  "Royalty",
                  "Sale"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Storage Lease"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Lease Subtype"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Easement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Access",
                  "Collection",
                  "Transmission",
                  "Laydown",
                  "Utility"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Encroachment"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "O&M Building",
                  "Substation"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          }
        ]
      }
    },
    "additionalProperties": false,
    "required": [
      "Type",
      "Sub Type"
    ]
  }
}

Analysis of Errors

  1. Root Level Structure: The root level of your schema does not conform to the required structure. It should contain a "name" field, a "json_schema" field, and a "strict" field. The "json_schema" field should contain the actual schema definition. In your schema, the "parameters" field is used instead of "json_schema".

  2. "name" Field: The "name" field should match the pattern '^[a-zA-Z0-9_-]+$'. Your "name" field, "type_subtype_logic", meets this requirement.

  3. "strict" Field: The "strict" field should be a boolean. Your "strict" field is true, which meets this requirement.

  4. "description" Field: The "description" field is not a recognized field at the root level of the schema. It should be removed.

  5. "parameters" Field: The "parameters" field is not a recognized field at the root level of the schema. It should be replaced with "json_schema".

  6. "anyOf" Usage: The "anyOf" keyword is used correctly in your schema. It is used within the properties of the root object, and each schema within "anyOf" has a unique set of properties.

  7. "additionalProperties" Field: The "additionalProperties" field is correctly set to false at all levels of your schema.

  8. Required Properties: All properties are correctly included in the "required" field at all levels of your schema.

Replacement Validating Schema

Here’s a replacement schema that meets all the requirements:

{
  "name": "type_subtype_logic",
  "strict": true,
  "schema": {
    "type": "object",
    "properties": {
      "Type": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Solar Lease",
                  "Wind Lease",
                  "Hydrogen Lease",
                  "Agricultural Lease",
                  "Letter of Intent"
                ]
              },
              "Sub Type": {
                "type": "null",
                "enum": [
                  null
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "License Agreement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Grazing",
                  "Met Tower",
                  "Right of Entry"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Option Agreement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Easement",
                  "Lease",
                  "Purchase"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Purchase and Sale"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Purchase",
                  "Royalty",
                  "Sale"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Storage Lease"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Lease Subtype"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Easement"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "Access",
                  "Collection",
                  "Transmission",
                  "Laydown",
                  "Utility"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "Type": {
                "type": "string",
                "enum": [
                  "Encroachment"
                ]
              },
              "Sub Type": {
                "type": "string",
                "enum": [
                  "O&M Building",
                  "Substation"
                ]
              }
            },
            "required": [
              "Type",
              "Sub Type"
            ],
            "additionalProperties": false
          }
        ]
      }
    },
    "required": [
      "Type"
    ],
    "additionalProperties": false
  }
}

This schema meets all the requirements for a strict JSON Schema response format output. It includes the mandatory root structure, all objects have at least one property, all properties are included in a "required" field, "additionalProperties" is set to false at all levels, the root level of the schema is an object, and "anyOf" is used correctly.


OpenAI seems to have “schema” as an alias for “json_schema” at the root which is pushed by the schema bot (but not mine that follows original documentation); an error with schema will fail json_schema tree validation.

1 Like

Same error is thrown when providing the full response_format object as you specify:

response_format = {
  "type": "json_schema",
  "json_schema": {
    "name": "table_component",
    "schema": {
      "type": "object",
      "properties": {
        "table": {
          "type": "object",
          "properties": {
            "name": {"type" : "string"},
            "columns": {
              "type": "array", 
              "items": {
                "type": "object", 
                "properties": {
                  "name" : {"type" : "string"}, 
                  "type" : {"type" : "string"}
                },
                "required" : ["name", "type"],
                "additionalProperties" : False
              }    
            }
          },
          "required": ["name", "columns"],
          "additionalProperties" : False
        },
        "component" : {"type": "object"}
      },
      "required" : ["component","table"],
      "additionalProperties" : False
    },
    "strict": True
  }
}

—>

openai.chat.completions.create(model="ft:gpt-4o-2024-08-06:my_model",
  messages=[
{'role' : 'user',
 'content': my_app + '''Create a table and component'''
}
  ],
response_format=response_format
)

—>
gives error:

BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'table_component': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Extra required key 'component' supplied.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}