Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Introduction to Python3
Next article icon

Introduction to Python Pydantic Library

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In modern Python development, data validation and parsing are essential components of building robust and reliable applications. Whether we're developing APIs, working with configuration files, or handling data from various sources, ensuring that our data is correctly validated and parsed is crucial. This is where Pydantic comes into play.

Pydantic is a data validation and settings management library that leverages Python's type annotations to provide powerful and easy-to-use tools for ensuring our data is in the correct format. In this article, we will learn about Pydantic, its key features, and core concepts, and see practical examples.

Table of Content

  • What is Pydantic?
  • Key Features of Pydantic
  • Core Concepts
  • Creating and Using Pydantic Models
  • Advanced Validation Techniques
  • Data Parsing and Serialization
  • Integrate Pydantic with Other Frameworks
  • Common Use Cases of Pydantic

What is Pydantic?

Pydantic is a Python library that helps us in defining and validating data models easily. When we build applications, that handle user input or data from various sources, it is important to make sure that the data is valid and consistent. If we do not do proper validation then we might get errors or unexpected behavior in our application. Pydantic makes this process easy by allowing us to define our data structure using Python classes and automatically validating the data against these structures.

Key Features of Pydantic

  • Type Validation: Automatically validates data based on type annotations.
  • Data Parsing: Converts input data into Python objects with the correct types.
  • Error Handling: Provides clear and detailed error messages for invalid data.
  • Field Validators: Allows custom validation logic with the @field_validator decorator.
  • Performance: Pydantic is optimized for speed and supports optional Cython extensions for even faster performance.
  • Integration: Easily integrates with popular frameworks like FastAPI, Django, Flask and ORMs like SQLAlchemy.

Installing Pydantic via pip

We have to first install Pydantic before using it. We can install it using pip.

pip install pydantic

Core Concepts

Pydantic has some core concepts that make it easy to work with data models. Some of them are:

  • At the heart of Pydantic are models that represent the structure of our data.
  • These are Python's way of defining the expected types for our data.
  • Pydantic automatically checks that the data fits the model's structure and types.

1. Understanding Models in Pydantic

At the heart of Pydantic is the concept of models. A Pydantic model is a Python class that inherits from BaseModel and is used to define the structure, validation, and parsing logic for our data. Each attribute of the model represents a field in the data, and the type annotations define the expected type.

2. Type Annotations and Type Validation

Type annotations in Python allow us to specify the expected data type of a variable. Pydantic leverages these annotations to enforce type checking automatically.

For example, if we specify that a field should be an integer, Pydantic will raise an error if a non-integer value is provided.

3. The Importance of Data Parsing and Validation

In any application, ensuring that our data is in the correct format and meets certain criteria is crucial. Pydantic's automatic data parsing and validation help prevent bugs and errors by ensuring that our data is always consistent and reliable.

Creating and Using Pydantic Models

1. Defining a Basic Pydantic Model

Now, let's start with a simple example of how to define and use a Pydantic model. Imagine we want to create a model for a user profile.

Here, we define a UserProfile model with three fields: name, age, and email. When we create an instance of this model, Pydantic automatically validates the types.

Python
from pydantic import BaseModel  class UserProfile(BaseModel):     name: str     age: int     email: str  user = UserProfile(name="Rekha ", age=40, email="[email protected]") print(user) 

Output:

Screenshot-2024-09-09-142310
Create a Pydantic Model

2. Working with Model Attributes

We can access and manipulate the attributes of a Pydantic model just like we would with a regular Python object.

Python
print(user.name)  # Output: Rekha user.age = 41 print(user.age)  # Output: 41 

Output:

Screenshot-2024-09-09-142434
Accessing Pydantic Model Attributes

3. Handling Default Values and Required Fields

Pydantic allows us to specify default values for fields, as well as mark certain fields as required.

Python
class UserProfile(BaseModel):     name: str     age: int = 43  # Default value     email: str     is_active: bool = True  # Default value  user = UserProfile(name="Shrinivas", email="[email protected]") print(user) 
Screenshot-2024-09-09-142750
Handling Default Value

Advanced Validation Techniques

Pydantic offers more advanced validation techniques through decorators and custom validators.

1. Field Validators (@validator Decorator)

We can create custom validation logic using the @validator decorator.

Python
from pydantic import BaseModel, field_validator  class UserProfile(BaseModel):     name: str     age: int     email: str      @field_validator('age')     def check_age(cls, value):         if value < 40:             raise ValueError('Age must be at least 18')         return value  UserProfile(name="Rekha", age=17, email="[email protected]") 

Output:

Screenshot-2024-09-09-143241
Field Validator in Pydantic Model

2. Nested Models and Complex Data Structures

If our data structure is more complex, Pydantic supports nested models.

Python
class Address(BaseModel):     street: str     city: str  class UserProfile(BaseModel):     name: str     age: int     email: str     address: Address  address = Address(street="461 soraon kauri", city="Prayagraj") user = UserProfile(name="Rekha", age= 40, email="[email protected]", address=address) print(user) 

Output:

Screenshot-2024-09-09-143539
Nested Models in Pydantic

Data Parsing and Serialization

Pydantic makes it easy to parse data from different formats and serialize it back to JSON or other formats.

1. Parsing Data from JSON and Other Formats

We can parse data directly from JSON strings.

Note: Before running this code, make sure to install pydantic module using this command - pip install pydantic

Python
from pydantic import BaseModel  class UserProfile(BaseModel):     name: str     age: int     email: str  data = '{"name": "Prajjwal", "age": 22, "email": "[email protected]"}'  # Use model_validate_json instead of parse_raw user = UserProfile.model_validate_json(data) print(user) 

Output:

query-1
JSON to pydantic model

2. Serializing Models to JSON

Similarly, we can serialize a Pydantic model back to JSON.

Python
from pydantic import BaseModel import json  class UserProfile(BaseModel):     name: str     age: int     email: str   user = UserProfile(name="Shrinivas", age=22, email="[email protected]")  json_data = user.json() print(json_data) 

Output:

Screenshot-2024-09-09-144007
Pydantic Model to Json

3. Handling Optional and Nullable Fields

Pydantic supports optional fields using Python's Optional type. The default value for optional fields will be None.

Python
from typing import Optional from pydantic import BaseModel  class UserProfile(BaseModel):     name: str     age: Optional[int] = None     email: str 

Output:

Screenshot-2024-09-09-144231
Pydantic Model Optional Field

Performance and Optimizations

Using Pydantic’s Cython Extensions for Speed - For performance-critical applications, Pydantic offers optional Cython extensions that significantly improve speed.

Tips for Optimizing Model Performance

  • Use validate_all=False: Skip validation for fields without type annotations.
  • Leverage @root_validator: Validate the entire model at once to reduce redundant checks.

Integrate Pydantic with Other Frameworks

Pydantic can easily be integrated with some popular frameworks such as FastAPI, Django, and Flask.

  • Using Pydantic with FastAPI - FastAPI is a modern web framework that uses Pydantic under the hood for data validation.
  • Pydantic in Django and Flask Projects - Pydantic can be used alongside Django and Flask to handle data validation in these frameworks.
  • Integration with SQLAlchemy and Other ORMs - Pydantic can also be integrated with ORMs like SQLAlchemy for database interactions.

Common Use Cases of Pydantic

  • API Data Validation and Parsing - Pydantic is widely used in APIs to validate incoming request data and parse it into the correct types.
  • Data Processing Pipelines - Pydantic can be used in data processing pipelines to ensure data integrity and consistency across various stages.
  • Configuration Management - Pydantic is ideal for managing application configurations, providing type-safe access to configuration data.

Conclusion

Pydantic is a powerful and flexible library for data validation, parsing, and settings management in Python. Its reliance on type annotations makes it both easy to use and highly efficient, allowing developers to write cleaner, more maintainable code. Whether we're building APIs, processing data, or managing configurations, Pydantic is a valuable tool that can help us ensure our data is always in the correct format.


Next Article
Introduction to Python3

R

rekhamiseswt
Improve
Article Tags :
  • Python
  • Python-Library
  • python
Practice Tags :
  • python
  • python

Similar Reads

  • Introduction to Python3
    Python is a high-level general-purpose programming language. Python programs generally are smaller than other programming languages like Java. Programmers have to type relatively less and indentation requirements of the language make them readable all the time. Note: For more information, refer to P
    3 min read
  • Introduction to Python GIS
    Geographic Information Systems (GIS) are powerful tools for managing, analyzing, and visualizing spatial data. Python, a versatile programming language, has emerged as a popular choice for GIS applications due to its extensive libraries and ease of use. This article provides an introduction to Pytho
    4 min read
  • Python Introduction
    Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code. Key Features of PythonPython’s simple and readable syntax makes it beginner-frie
    3 min read
  • Introduction to Python Black Module
    Python, being a language known for its readability and simplicity, offers several tools to help developers adhere to these principles. One such tool is Black, an uncompromising code formatter for Python. In this article, we will delve into the Black module, exploring what it is, how it works, and wh
    5 min read
  • Introduction to PyVista in Python
    Pyvista is an open-source library provided by Python programming language. It is used for 3D plotting and mesh analysis. It also provides high-level API to simplify the process of visualizing and analyzing 3D data and helps scientists and other working professionals in their field to visualize the d
    4 min read
  • Introduction to auto-py-to-exe Module
    In the world of Python development, sharing your application with users who may not have Python installed can be challenging. auto-py-to-exe is a powerful tool that simplifies this process by converting Python scripts into standalone executable files (.exe) for Windows. This makes it easier to distr
    5 min read
  • Libraries in Python
    Normally, a library is a collection of books or is a room or place where many books are stored to be used later. Similarly, in the programming world, a library is a collection of precompiled codes that can be used later on in a program for some specific well-defined operations. Other than pre-compil
    8 min read
  • List of Python GUI Library and Packages
    Graphical User Interfaces (GUIs) play a pivotal role in enhancing user interaction and experience. Python, known for its simplicity and versatility, has evolved into a prominent choice for building GUI applications. With the advent of Python 3, developers have been equipped with lots of tools and li
    12 min read
  • NumPy Tutorial - Python Library
    NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays. At its core it introduces the ndarray (n-dimen
    3 min read
  • PyDictionary module in Python
    AyDictionary It's a Python module used to fetch definitions of words, while googletrans provides translation services. meaningstranslationsInstallation To install AyDictionary run the following pip code on the terminal / command prompt: pip install AyDictionary googletrans==4.0.0-rc1Getting Started
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences