
One core feature of Python is the fact that you don’t need to declare your data types; we call this dynamic typing.
If you’ve worked with a language such as Rust or Java, you know you can’t re-assign a variable to a different data type. For instance, if we wanted to re-assign the variable from an integer to a float:
// Java
int age = 30;
age = 40.5; // compile-time error
// Rust
let mut age: i32 = 30;
age = 40.5; // type mismatch error
# Python
age = 30
age = 40.5 # OkayTo us, as humans, this re-assignment feels smooth and natural. But this flexibility comes with a trade-off: Python itself won’t enforce what your data is supposed to be following certain rules.
This becomes especially important when your data comes from outside your system.
This begs the question: “how can I easily validate data coming from external resources?”
Model your data with Pydantic
Pydantic is an external data validation library used by major projects such as FastAPI and HuggingFace to solve issues dynamic typing presents to us.
To give an example, let’s say we received a response from an external API that gives us information about a specific item found on Amazon:
product_data = {
"title": "USB-C Charger",
"price": "24.99",
"rating": "4.7",
"prime": "true"
}This looks like a valid response, but there’s a two “gotcha’s” here: the numbers and boolean values are strings. So, without Pydantic, you'd manually write the data validation checking:
price = float(product_data["price"])
rating = float(product_data["rating"])
prime = product_data["prime"] == "true"
# .. and so forthBut with Pydantic, you can explicitly define what each data type is without writing a single line of validation code:
from pydantic import BaseModel
# Define the Amazon product
class AmazonProduct(BaseModel):
title: str
price: float
rating: float
prime: bool
# Load the Amazon product into the model
product = AmazonProduct(**product_data)Sometimes, less code you write is more; that’s the case with Pydantic.
The basics of data modeling
Data modeling is much easier than you’d think. They decide what your data should look like before you start using it. You’re defining what fields exist, what type they are, what is required, and what rules apply.
For instance, if you need to define a rule about a product (from above) that shouldn’t ever exist, such as the price being zero, you’ll use the Field class and set the boundary into the model:
from pydantic import BaseModel, Field
class AmazonProduct(BaseModel):
...
price: float = Field(gt = 0)
...It’s best practice to validate at the boundaries (i.e. when your data enters the system). Some examples of this include API requests, form submissions, external service responses, and environment variables. If you catch bad data here, it never leaks into your business logic.
Here’s a few rules of thumb to keep in mind:
One model per shape of data. Don’t re-use a model for two things that happen to look similar.
Validate early, trust later. Once data passes through a Pydantic model, the rest of your code doesn’t need to guess.
Use
Fieldfor domain rules. Encode this directly into the model, not in scatteredifchecks.
Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

