In order to define a dynamic key in Pydantic 2.4+ we do the following:

  1. In the file where you will define your model type the following: from pydantic import RootModel.

  2. Define the class for your model and pass RootModel as a parameter:

class Languages(RootModel):
  1. Define the dynamic key as root and then define the data type of the values.
root: str