Learn how to effectively obtain JSON schema for Pydantic models when using `typing` features like List, Optional, and Union. This guide breaks down the process step by step! --- This video is based on the question https://stackoverflow.com/q/62040482/ asked by the user 'hangc' ( https://stackoverflow.com/u/5579871/ ) and on the answer https://stackoverflow.com/a/66576153/ provided by the user 'hangc' ( https://stackoverflow.com/u/5579871/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Get Json schema of a Type of pydantic model Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Unlocking JSON Schema for Pydantic Models with typing Pydantic is a powerful data validation library for Python, widely used for its ability to define data structures. It helps ensure that your application data adheres to specified types, and it offers functionality to generate JSON schema for these definitions. However, things can get tricky when you want to generate a JSON schema for complex types created using the typing module. In this post, we will explore how to gracefully handle this situation. Let’s dig in! The Challenge When working with Pydantic and typing, you might find yourself in need of generating a JSON schema for models that utilize List, Optional, or Union types. For instance, you might be defining a model that revolves around the following types: typing.List[MyModel] - A list of MyModel instances. typing.Optional[MyModel] - A model that may or may not contain a MyModel instance. typing.Union[MyModel1, MyModel2] - A model that can represent either MyModel1 or MyModel2. Obtaining a JSON schema for such complex applications tends to be complicated using traditional methods. Thankfully, Pydantic has a solution! Solution Overview: Using the _root_ Attribute Starting with Pydantic, you can leverage the _root_ attribute to define a model that can encapsulate lists and other typing constructs. This allows the model to dynamically generate the schema, matching the complexity of your data definitions. Here’s how you can implement this. Step-by-step Implementation Define Your Base Model - Begin by defining the Pydantic model using the BaseModel class. Let’s say you have MyModel already defined as follows: [[See Video to Reveal this Text or Code Snippet]] Create a New Model for Lists Using _root_ - Now, we’ll create another Pydantic model that will utilize __root__. This model will represent a list of MyModel instances: [[See Video to Reveal this Text or Code Snippet]] Generate the JSON Schema - Finally, you can call the .schema() method on your new model to obtain the corresponding JSON schema: [[See Video to Reveal this Text or Code Snippet]] Example Code Here’s a complete implementation in one place: [[See Video to Reveal this Text or Code Snippet]] Expected Output When you run the above code, Pydantic will generate a JSON schema that describes a list of MyModel objects, allowing you to easily validate incoming data structures. Conclusion Generating JSON schema for Pydantic models that incorporate typing functionalities is straightforward once you understand how to utilize the _root_ attribute. This approach gives you the flexibility to work with complex data types seamlessly. Wo have seen how to encapsulate a list of models, and you can extrapolate this method for Optional, Union, and other types as needed. By following the steps outlined above, you’ll be on your way to effectively managing and validating your data in Python with Pydantic! Happy coding!