Pydantic response model failure

Hello,

I am using instructor and pydantic to specify a schema to an open AI chat completion call. I had a schema that was working perfectly fine yesterday, but now faces some problems with:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for function 'BalanceSheetExtract'. Please ensure it is a valid JSON Schema.", 'type': 'invalid_request_error', 'param': 'tools[0].function.parameters', 'code': 'invalid_function_parameters'}}

My Schema is of the form:

class BalanceSheetExtract(BaseModel):
    company_name: str = Field(default=None, validate_default=True)
    prev_year: int = Field(default=None, validate_default=True)
    current_year: int = Field(default=None, validate_default=True)
    liabilities: LiabilitiesExtract
    assets: AssetsExtract
    equity: EquityExtract

class AssetsExtract(BaseModel):
    current_assets: CurrentAssetsExtract
    non_current_assets: NonCurrentAssetsExtract
    total_assets: DataField

class NonCurrentAssetsExtract(BaseModel):
    fields: List[DataField] = Field(default=None, validate_default=True)
    total_non_current_assets: DataField = Field(default=None, validate_default=True)

etc, the other sub classes are of a similar format as above

Where a data field is:

class DataField(BaseModel):
    field_name: str = Field(default=None, validate_default=True)
    prev_year_value: float = Field(default=0, validate_default=True)
    current_year_value: float = Field(default=0, validate_default=True)

This structure has worked fine for me yesterday. It works however when I remove the fields field from NonCurrentAssetsExtract hinting that they may be some limit to how deep a nested object can be maybe? Any thoughts?

The request is like this:

response = self.client.chat.completions.create(
            model="gpt-4o-2024-05-13",
            response_model=response_model,
            max_tokens=4096,
            max_retries=5,
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": prompt},
                        *[
                            {
                                "type": "image_url",
                                "image_url": {"url": f"data:image/png;base64,{image}"},
                            }
                            for image in base64_images
                        ],
                    ],
                }
            ],
        )

I have not updated instructor or pydantic recently, so there should be no real change in the request I am sending over.

Is the input too large for your max tokens maybe?

Hmm unfortunately no. Even for small messages. If I make my response model too “deep” it seems to fail. Oddly, this is a very recent occurrence as it was working fine yesterday

Have you verified the specific json output in something like jsonlint.com?

Running into the same issue. If I make my response model too deep then it seems to fail. It seems like it can only go up to 4 levels.