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