Limit to how deep a Pydantic object can be #722
nazim-ashman-oc
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey,
Is there a limit to how deeply nested a Pydantic model can be?
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
],
],
}
],
)
Beta Was this translation helpful? Give feedback.
All reactions