Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field title with same name as model class leads to unexpected schema behavior #9427

Closed
1 task done
evanrasmussen9 opened this issue May 11, 2024 · 2 comments
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@evanrasmussen9
Copy link

evanrasmussen9 commented May 11, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

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

from pydantic import BaseModel, Field


class MyInnerModel(BaseModel):
    a: str
    b: str


class MyModel(BaseModel):
    inner_1: MyInnerModel = Field(title="MyInnerModel")
    inner_2: MyInnerModel = Field(title="MyInnerModel2")


if __name__ == "__main__":
    schema = MyModel.model_json_schema()

    assert "title" not in schema["properties"]["inner_1"]  # This is unexpected behavior
    assert "title" in schema["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
@evanrasmussen9 evanrasmussen9 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 11, 2024
@antoni-jamiolkowski
Copy link
Contributor

antoni-jamiolkowski commented May 12, 2024

Hi,

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"].

@sydney-runkle
Copy link
Member

Hi folks!

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!

@sydney-runkle sydney-runkle closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

3 participants