# Structured Output works for Flat Schema, not for Nested Schema

**URL:** <https://community.openai.com/t/structured-output-works-for-flat-schema-not-for-nested-schema/990225>\
**Category:** API\
**Tags:** structured-output\
**Created:** [October 23, 2024, 10:02pm UTC](https://community.openai.com/t/structured-output-works-for-flat-schema-not-for-nested-schema/990225 "2024-10-23T22:02:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![expertise.ai.chat](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/expertise.ai.chat/32/147254_2.png) [@expertise.ai.chat](https://community.openai.com/u/expertise.ai.chat)\
**Post date:** [October 23, 2024, 10:02pm UTC](https://community.openai.com/t/structured-output-works-for-flat-schema-not-for-nested-schema/990225/1 "2024-10-23T22:02:40Z")

</div>

I am using structured outputs to ensure GPT’s response conforms to a schema I define. I have defined this schema using JSON.

Here is my Schema.

```auto
class ContentsSchema(BaseModel):
    contents: List[ContentSchema] = Field(..., description=<some_description>)
    summary: str = Field(..., description=<some_description>)

```

As you can see, this schema depends on a few other schemas. I will define them below.

```auto
class ContentSchema(BaseModel):
    number: str = Field(..., description=<some_description>)
    title: str = Field(..., description=<some_description>)
    body: Union[HeroSchema, ListSchema, TableSchema, TextSchema] = Field(..., description=<some_description>)
    related_questions: List[RelatedQuestionsItemSchema] = Field(..., description=<some_description>)

class HeroSchema(BaseModel):
    component: str = Field(..., enum=[Component.Hero.value])
    heading: str = Field(..., description=<some_description>)
    subheading: str = Field(..., description=<some_description>)
    buttons: List[ButtonSchema] = Field(..., description=<some_description>)
    keywords: List[str] = Field(..., description=<some_description>)
    completed: Literal[True] = Field(..., description=<some_description>)

```

My code up till this point was working fine. I recently tried to make a change that involved nesting `keywords` and `completed` inside another key and my code stopped working.

Here is how I modified `HeroSchema`.

```auto
class HeroSchema(BaseModel):
    component: str = Field(..., enum=[Component.Hero.value])
    heading: str = Field(..., description=<some_description>)
    subheading: str = Field(..., description=<some_description>)
    buttons: List[ButtonSchema] = Field(..., description=<some_description>)
    image_keywords: ImageKeywordsSchema = Field(..., description=<some_description>)

class ImageKeywordsSchema(BaseModel):
    keywords: List[str] = Field(..., description=<some_description>)
    completed: Literal[True] = Field(..., description=<some_description>)

```

After making this change, my code stopped working. When I run it, I get the following error.

```auto
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'ContentsSchema': In context=('properties', 'image_keywords'), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}

```

Has anyone seen this before and know of a fix? Is there a limit regarding how many layers of depth I can include in my schema? Could it be something else causing this error?

Thanks

EDIT:  
I just noticed, the below code works just fine for `TextSchema`.

```auto
class TextSchema(BaseModel):
    component: str = Field(..., enum=[Component.Text.value])
    text: str = Field(..., description=<some_description)
    image_keywords: ImageKeywordsSchema = Field(..., description=<some_description>)

```

Additionally, if I change `HeroSchema` to this it works.

```auto
class HeroSchema(BaseModel):
    component: str = Field(..., enum=[Component.Hero.value])
    heading: str = Field(..., description=<some_description>)
    subheading: str = Field(..., description=<some_description>)
    buttons: List[ButtonSchema] = Field(..., description=<some_description>)
    image_keywords: List[ImageKeywordsSchema] = Field(..., description=<some_description>)

```
