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
Add Title Generator to Support Programmatic Titles #4632
Comments
I agree this is a good idea, should be fairly straightforward in v2. |
I'll just repeat some of my comment from #1361, (with some updates): I have no problems with this, but I think that we should give the model-config-key and field-info-key different names — rather than using I'll also note that this will require some care around the use of something like |
Is there any workaround for this today? I've tried using Using just the class name, like the default behavior does, doesn't work in large projects. If you use e.g. FastAPI to generate an OpenAPI spec (which uses JSON schema) then we can't generate a reasonable OpenAPI client library from the OpenAPI spec. The reason is that we get a huge flat namespace of all model names, which we can't map back into e.g. namespaces. In other words the current approach is lossy. |
In v1 the following workaround seems to work: class _Base(pydantic.BaseModel):
class Config:
@staticmethod
def schema_extra(schema: dict[str, Any], model: type['_Base']) -> None:
schema['title'] = f'{model.__module__}.{model.__name__}' |
Thinking about this some more it's not actually the title I want to control over but the ID generated as the key in the |
Is there any news for this title generation feature? When creating model json schemas, the current default title generation often does not play nicely when using aliases or alias generators. Consider the following toy example: from pydantic import BaseModel, ConfigDict
from pydantic.alias_generators import to_camel
class MyModel(BaseModel):
my_field: str
my_other_field: int
model_config = ConfigDict(alias_generator=to_camel) When calling As a workaround I am currently using a custom Having a title generator on the model config would be a convenient solution to this problem. The definitions for both the alias generator and title generator would be closer to each other, and it is more obvious what kind of values the title generator should mostly expect. |
Discussed in #4044
Originally posted by zkulis May 4, 2022
Background
Currently, Pydantic allows specifying a string literal for the
title
value used in the exported JSON Schema. ForBaseModel
, this is accomplished via theConfig
class:For
BaseModel
fields, an explicittitle
string may be specified with aField
instance:Feature Request
It would be beneficial if Pydantic provided a programmatic way to set the
title
value for both models and fields, similar to the functionality provided byalias_generator
here. Specifically:title_generator
class attribute in theConfig
class, andtitle_generator
parameter in theField
constructor.In both cases, the
title_generator
would take a Python callable that receives:BaseModel
class name for the first caseThis is demonstrated below:
The resulting JSON Schema would be:
The text was updated successfully, but these errors were encountered: