DeveloperBreeze

Introduction

In this tutorial, you'll learn how to use Pydantic, a powerful Python library for data validation and settings management. Pydantic leverages Python's type annotations to ensure that data is validated and coerced to the correct types. This makes it an invaluable tool for building APIs, data pipelines, and applications where data integrity is paramount.

Table of Contents

  1. What is Pydantic?
  2. Installing Pydantic
  3. Creating Your First Pydantic Model
  4. Validating and Parsing Data
  5. Handling Validation Errors
  6. Advanced Pydantic Features
  • Default Values
  • Nested Models
  • Constrained Types
  • Aliases and Field Names
  1. Serialization and Deserialization
  2. Integrating Pydantic with FastAPI
  3. Managing Settings with Pydantic
  4. Best Practices and Common Use Cases
  5. Conclusion

1. What is Pydantic?

Pydantic is a Python library designed to provide data validation and settings management using Python's type annotations. It allows you to define data models with strict type constraints, ensuring that the data passed to your application is valid and correctly formatted. Pydantic is widely used in API development, data processing pipelines, and configuration management.

2. Installing Pydantic

To get started with Pydantic, you'll need to install it via pip:

pip install pydantic

3. Creating Your First Pydantic Model

Pydantic models are simple Python classes that inherit from pydantic.BaseModel. Here's how you can create a basic Pydantic model:

from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str
    age: int
    is_active: bool = True

In this model, each attribute has a specified type. The is_active attribute also has a default value of True.

4. Validating and Parsing Data

Pydantic automatically validates data and coerces types when you create an instance of the model:

# Valid data
user = User(id=1, name="John Doe", age=25)
print(user)

# Automatic type coercion
user = User(id="1", name="John Doe", age="25")
print(user)

Pydantic will convert the string values for id and age to integers.

5. Handling Validation Errors

If the data doesn't meet the model's requirements, Pydantic raises a ValidationError:

from pydantic import ValidationError

try:
    user = User(id="abc", name="John Doe", age="25")
except ValidationError as e:
    print(e)

This will output an error indicating that the id field should be an integer, not a string.

6. Advanced Pydantic Features

Default Values

You can assign default values to fields in your models:

class User(BaseModel):
    id: int
    name: str
    age: int
    is_active: bool = True  # Default value

Nested Models

Pydantic models can be nested within other models:

class Address(BaseModel):
    street: str
    city: str

class User(BaseModel):
    id: int
    name: str
    age: int
    address: Address

Constrained Types

Pydantic allows you to define constraints on fields, such as minimum and maximum values:

from pydantic import conint

class User(BaseModel):
    id: int
    name: str
    age: conint(ge=0, le=120)  # Age must be between 0 and 120

Aliases and Field Names

You can use field aliases to allow for different input names:

class User(BaseModel):
    id: int
    full_name: str = Field(alias='name')

This allows name to be used as an input key instead of full_name.

7. Serialization and Deserialization

Pydantic models can easily be converted to and from JSON:

# Serialize to JSON
user_json = user.json()
print(user_json)

# Deserialize from JSON
user_data = '{"id": 1, "name": "John Doe", "age": 25}'
user = User.parse_raw(user_data)
print(user)

8. Integrating Pydantic with FastAPI

Pydantic is deeply integrated with FastAPI, a modern web framework for building APIs. Here's how you can use Pydantic models with FastAPI:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    id: int
    name: str
    age: int

@app.post("/users/")
async def create_user(user: User):
    return user

FastAPI will automatically validate the incoming request data against the User model.

9. Managing Settings with Pydantic

Pydantic can also be used to manage application settings by reading from environment variables:

from pydantic import BaseSettings

class Settings(BaseSettings):
    app_name: str = "My App"
    admin_email: str

    class Config:
        env_prefix = "MYAPP_"

settings = Settings()
print(settings.app_name)
print(settings.admin_email)

10. Best Practices and Common Use Cases

  • Use Pydantic models for API request and response validation.
  • Leverage type coercion to simplify data handling.
  • Use nested models for complex data structures.
  • Manage application settings and configurations with Pydantic's BaseSettings.
  • Take advantage of constrained types for stricter validation rules.

11. Conclusion

Pydantic is a versatile and powerful tool for data validation and management in Python. By integrating Pydantic into your projects, you can ensure that your data is clean, consistent, and easy to work with, reducing the likelihood of errors and improving the overall quality of your code.


Continue Reading

Discover more amazing content handpicked just for you

I Made $10,000 from a Simple Python Script—Here’s How!
Article
python

I Made $10,000 from a Simple Python Script—Here’s How!

A few months ago, I was just experimenting with Python, trying to automate small tasks and solve problems. I never expected that one of these little scripts would end up making me over $10,000. But that’s exactly what happened.

Here’s the full story of how a simple idea turned into a surprisingly profitable project.

Feb 11, 2025
Read More
Tutorial
python

دليل عملي: بناء روبوت دردشة (Chatbot) باستخدام Python و NLP

from nltk.stem import WordNetLemmatizer

lemmatizer = WordNetLemmatizer()

# دالة لتبسيط الكلمات
def preprocess(text):
    words = nltk.word_tokenize(text)
    return [lemmatizer.lemmatize(word.lower()) for word in words]

لنبدأ في بناء منطق روبوت الدردشة:

Dec 12, 2024
Read More
Tutorial
python

Mastering Generators and Coroutines in 2024

import asyncio

async def greet():
    print("Hello!")
    await asyncio.sleep(1)
    print("Goodbye!")

asyncio.run(greet())

Combine multiple coroutines to run concurrently:

Dec 10, 2024
Read More
Tutorial
python

Setting Up and Managing Python Virtual Environments Using venv

  • Installing Packages
  • Listing Installed Packages
  • Freezing Dependencies
  • Installing from a requirements.txt File

A virtual environment is an isolated environment that allows you to run and manage Python projects with their own dependencies. This isolation prevents conflicts between packages and versions, making it easier to manage multiple projects on the same machine.

Aug 29, 2024
Read More
Tutorial
go

Building a RESTful API with Go and Gorilla Mux

   curl -X GET http://localhost:8000/books/b1
   curl -X PUT http://localhost:8000/books/b1 -H "Content-Type: application/json" -d '{"title":"Advanced Go","author":"Jane Smith","year":"2024"}'

Aug 12, 2024
Read More
Tutorial
javascript nodejs +1

Building a GraphQL API with Node.js and Apollo Server

const { ApolloServer, gql, PubSub } = require('apollo-server-express');
const pubsub = new PubSub();

const BOOK_ADDED = 'BOOK_ADDED';

const typeDefs = gql`
    type Book {
        title: String!
        author: String!
    }

    type Query {
        books: [Book]
    }

    type Mutation {
        addBook(title: String!, author: String!): Book
    }

    type Subscription {
        bookAdded: Book
    }
`;

const resolvers = {
    Query: {
        books: () => books,
    },
    Mutation: {
        addBook: (_, { title, author }) => {
            const newBook = { title, author };
            books.push(newBook);
            pubsub.publish(BOOK_ADDED, { bookAdded: newBook });
            return newBook;
        },
    },
    Subscription: {
        bookAdded: {
            subscribe: () => pubsub.asyncIterator([BOOK_ADDED]),
        },
    },
};

const server = new ApolloServer({
    typeDefs,
    resolvers,
    subscriptions: {
        path: '/subscriptions',
    },
});

const app = express();
server.applyMiddleware({ app });

const httpServer = require('http').createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen({ port: 4000 }, () => {
    console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
    console.log(`🚀 Subscriptions ready at ws://localhost:4000${server.subscriptionsPath}`);
});

In the GraphQL Playground, you can test the subscription by running the following:

Aug 12, 2024
Read More
Code
csharp

Unity Inventory System using Scriptable Objects

A simple inventory system that can add, remove, and use items.

using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public List<Item> items = new List<Item>();
    public int capacity = 20;

    public bool AddItem(Item item)
    {
        if (items.Count >= capacity)
        {
            Debug.Log("Inventory is full!");
            return false;
        }

        if (item.isStackable)
        {
            Item existingItem = items.Find(i => i.itemName == item.itemName);
            if (existingItem != null)
            {
                // Stack logic (if needed)
                Debug.Log($"Stacking {item.itemName}");
                return true;
            }
        }

        items.Add(item);
        Debug.Log($"{item.itemName} added to inventory.");
        return true;
    }

    public void RemoveItem(Item item)
    {
        if (items.Contains(item))
        {
            items.Remove(item);
            Debug.Log($"{item.itemName} removed from inventory.");
        }
    }

    public void UseItem(Item item)
    {
        if (items.Contains(item))
        {
            item.Use();
        }
    }
}

Aug 12, 2024
Read More
Code
json python

Python Code Snippet: Simple RESTful API with FastAPI

No preview available for this content.

Aug 04, 2024
Read More
Tutorial
python

Creating a Simple REST API with Flask

  curl http://127.0.0.1:5000/api/items/1
  • Create a new item:

Aug 03, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!