Pydantic is a powerful library for building robust, scalable, and maintainable data models in Python. One of its key features is the ability to export data models as JSON Schema, which can be used for validation, documentation, and more. Currently, Pydantic allows specifying a string literal for the title value used in the exported JSON Schema. However, there is a growing need for a more programmatic approach to setting titles, especially for complex data models.
In Pydantic, the title value for a data model can be specified using the Config
class. For example:
from pydantic import BaseModel
class MyDataClass(BaseModel):
class Config:
title = "My Data Class"
Similarly, for individual fields, an explicit title string can be specified using a Field
instance:
from pydantic import BaseModel, Field
from typing import List
class MyDataClass(BaseModel):
matchCriteria: List[str] = Field(default_factory=list, title="Match Criteria")
class Config:
title = "My Data Class"
To improve the flexibility and customizability of Pydantic, it would be beneficial to introduce a programmatic way to set the title value for both models and fields. This can be achieved by adding a title_generator
class attribute to the Config
class and a title_generator
parameter to the Field
constructor.
The title_generator
would take a Python callable that receives the model class name or field alias as an argument. This allows developers to define custom title generation logic, such as using a specific naming convention or incorporating external data sources.
For example:
import re
from pydantic import BaseModel
def make_title(name: str):
def _capitalize(v: str):
return v[0].upper() + v[1:]
return re.sub(r"(?<=[a-z])([A-Z])", r" \1", _capitalize(name))
class MyDataClass(BaseModel):
matchCriteria: List[str] = Field(default_factory=list, title_generator=make_title)
class Config:
title_generator = make_title
This would result in a JSON Schema with programatically generated titles:
{
"title": "My Data Class",
"type": "object",
"properties": {
"matchCriteria": {
"title": "Match Criteria",
"type": "array",
"items": {
"type": "string"
}
}
}
}
The introduction of programmatic titles in Pydantic would bring several benefits, including:
By incorporating a title_generator
into Pydantic, developers can create more robust, scalable, and maintainable data models, while also improving the overall quality and readability of their JSON Schema output.
For more information on Pydantic and its features, check out the following articles: