You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Field titles do not get registered if they are the same characters as the name of the BaseModel typehint. See the example code for a clear demonstration. Giving the title anything other than the exact name of the type will work as expected. I don't believe this is intentional, but if it is, an error should be raised when defining the Field title.
Example Code
frompydanticimportBaseModel, FieldclassMyInnerModel(BaseModel):
a: strb: strclassMyModel(BaseModel):
inner_1: MyInnerModel=Field(title="MyInnerModel")
inner_2: MyInnerModel=Field(title="MyInnerModel2")
if__name__=="__main__":
schema=MyModel.model_json_schema()
assert"title"notinschema["properties"]["inner_1"] # This is unexpected behaviorassert"title"inschema["properties"]["inner_2"]
print("assertions passed")
Python, Pydantic & OS Version
pydantic version: 2.4.2
pydantic-core version: 2.10.1
pydantic-core build: profile=release pgo=false
python version: 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
platform: Windows-10-10.0.22631-SP0
related packages: mypy-1.6.0 typing_extensions-4.10.0
The text was updated successfully, but these errors were encountered:
Out of my own curiosity, I decided to do a little investigation ... It turns out that the event of the unexpected deletion in your example happens in the handle_ref_overrides method of the GenerateJsonSchema class (link to the exact line).
The documentation of the method partially explains a notion of deleting sibling keys (here key title with value "MyInnerModel" in inner_1 and MyInnerModel). I am not fluent in the doings of the Pydantic's schema generation so unfortunately I can't provide you with explanation nor fix.
Since I spend some time on this issue I will try to delve into this topic if I find some free time and no one else has addressed your question yet 🙂
But at the moment, my money is on this being an expected behavior. I believe that what the Field(title="...") does is some kind of renaming of the title of MyInnerModel type, which by default is a class' name. The title of the MyInnerModel type in the inner_1 is handled by the $ref to the MyInnerModel class, hence the missing "title" key in the schema["properties"]["inner_1"].
I believe @antoni-jamiolkowski is on the right track here - I think this is the expected behavior - the title is by default the class name, so during JSON schema generation, we remove redundant title information. I think if you want to retain the redundancy, custom JSON schema generation would be the best route to go for now!
Initial Checks
Description
Field titles do not get registered if they are the same characters as the name of the BaseModel typehint. See the example code for a clear demonstration. Giving the title anything other than the exact name of the type will work as expected. I don't believe this is intentional, but if it is, an error should be raised when defining the Field title.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: