In order to define a dynamic key in Pydantic 2.4+ we do the following:
-
In the file where you will define your model type the following:
from pydantic import RootModel
. -
Define the class for your model and pass
RootModel
as a parameter:
class Languages(RootModel):
- Define the dynamic key as
root
and then define the data type of the values.
root: str