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
RootModelas a parameter:
class Languages(RootModel):
- Define the dynamic key as
rootand then define the data type of the values.
root: str