# Extra required key error in response format for JSON schema?

**URL:** <https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694>\
**Category:** API\
**Tags:** fine-tuning\
**Created:** [October 7, 2024, 6:54pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694 "2024-10-07T18:54:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![finetuning](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@finetuning](https://community.openai.com/u/finetuning)\
**Post date:** [October 7, 2024, 6:54pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/1 "2024-10-07T18:54:26Z")

</div>

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):

```auto
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:

```auto
{'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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![wolsen](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/wolsen/32/146755_2.png) [@wolsen](https://community.openai.com/u/wolsen)\
**Post date:** [October 12, 2024, 3:41pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/2 "2024-10-12T15:41:51Z")

</div>

I am having the exact same issue

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [October 12, 2024, 4:53pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/3 "2024-10-12T16:53:29Z")

</div>

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.

```json
{
  "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:

```json
{
  "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.)

> [@wolsen](#):
>
> I am having the exact same issue

Two people sending the exact same schemas??

---

<div class="post-metadata">

**Author:** ![wolsen](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/wolsen/32/146755_2.png) [@wolsen](https://community.openai.com/u/wolsen)\
**Post date:** [October 12, 2024, 5:08pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/4 "2024-10-12T17:08:29Z")

</div>

Im just encountering a similar error

```json
{
  "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"
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [October 12, 2024, 5:49pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/5 "2024-10-12T17:49:54Z")

</div>

### 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:

```json
{
  "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.

---

<div class="post-metadata">

**Author:** ![finetuning](https://avatars.discourse-cdn.com/v4/letter/f/b38774/32.png) [@finetuning](https://community.openai.com/u/finetuning)\
**Post date:** [October 14, 2024, 5:15pm UTC](https://community.openai.com/t/extra-required-key-error-in-response-format-for-json-schema/970694/6 "2024-10-14T17:15:23Z")

</div>

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

```auto
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
  }
}

```

—\>

```auto
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:

```auto
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}}

```
