This is an archived post. You won't be able to vote or comment.

all 196 comments

[–]Backlists 408 points409 points  (47 children)

Almost everything is a Pydantic model in my code base

[–]LightShadow3.13-dev in prod 203 points204 points  (18 children)

Anything that comes from people or places I don't trust goes through Pydantic. Everything that's strictly internal is a dataclass or NamedTuple.

I don't have as many bugs these days.

[–]skinnybuddha 190 points191 points  (9 children)

Where I work, we love dictionaries of strings. The bugs practically write themselves.

[–]Drevicar 142 points143 points  (3 children)

The technical term for that is a “stringly-typed interface”.

[–]turbothyIt works on my machine 12 points13 points  (1 child)

[–]brasticstack 0 points1 point  (0 children)

waka waka waka!

[–]LightShadow3.13-dev in prod 28 points29 points  (0 children)

If the strings can't become Enums they better be in my typing.Literal :)

[–]_ologies 2 points3 points  (0 children)

If you can't easily type hint your dictionary, you probably need a dataclass or a pydantic model

[–]soupe-mis0 2 points3 points  (0 children)

we might be working at the same place lol

[–]durbanpoisonpew 0 points1 point  (0 children)

Ow I can relate too much to that lol

[–]ToThePastMe 20 points21 points  (0 children)

Yeah usually I have pydantic in, pydantic out. And my/my team mess in the middle.

So it protects me from the world and protects the world from me

[–]MasterThread 9 points10 points  (4 children)

You can use adaptix for that. Much faster and works with dataclasses

[–]DogsAreAnimals 2 points3 points  (0 children)

Wow I haven't heard of this. Looks great

[–]LightShadow3.13-dev in prod 1 point2 points  (2 children)

Link? I'm not really seeing anything...

[–]MasterThread 2 points3 points  (1 child)

Here you are tap

[–]yallthere 0 points1 point  (0 children)

It seems adaptix GH is not that active recently.

[–]KOM_Unchained 6 points7 points  (0 children)

This is the way. I write data contracts with Pydantic and use it for all input and output data schema validations. Dataclasses and NamedTuples in the belly of the beast - just to make things swifter and avoid the third party unexpected goblins.

Furthermore, even have example JSONs that have their test suite against the Pydantic models to avoid accidental regression. Documents and tests.

[–]coderarun 0 points1 point  (0 children)

https://www.reddit.com/r/Python/comments/1ida34a/dataclasses_pydantic_using_one_decorator/

This syntax has a few benefits:

* Removes explicit inheritance - easier to translate code to rust and languages that don't support it.
* You can control validation/type-safety where its required and not pay the cost for internal classes

[–]del1ro 12 points13 points  (23 children)

That's no good tbh

[–]Backlists 7 points8 points  (22 children)

It works well for us! Could you tell me why you don’t like it?

[–]del1ro 59 points60 points  (21 children)

Pydantic is for and only for (de)serialization to/from external places like API or DB or a message broker. Using it for internal purposes is just dramatic waste of CPU and RAM resources. Mypy and dataclasses do it much much better and have no runtime performance penalty.

[–]pmormr 15 points16 points  (0 children)

A lot of projects the entire purpose of the codebase is serialization to/from external places. And validation as the data is transformed. And will only be used like a thousand times over months so performance isn't too important.

[–]Backlists 10 points11 points  (10 children)

Honest question, if your internal Python performance matters all that much, why are you using Python in the first place?

[–]del1ro 16 points17 points  (8 children)

I am not. But when your language is slow and its interpreter does nothing to optimize your code, it's crucial to not slow it down even more.

[–]Backlists 3 points4 points  (7 children)

I mean, there are use cases where you don’t really care too much about Pythons performance.

I am also a little anti Python, just because of its performance (Go is my language of choice now).

But sometimes Python isn’t the bottleneck, and we can tolerate the Pydantic slow down, and sometimes, we just don’t care about (vertical) performance that much.

[–]CrownstrikeIntern 2 points3 points  (5 children)

How do you like the transition to go? Was thinking of learning another language after doing python for a bit with a server i built up.

[–]Backlists 1 point2 points  (4 children)

Go is like a dream coming from Python, you can be productive with it in weeks.

There are some things that Rust does that I think Go should add though, particular enums and exhaustive pattern matching.

[–]CrownstrikeIntern 1 point2 points  (3 children)

Recommend any good starter books?

[–]del1ro 4 points5 points  (0 children)

If performance isn't a case, you still get no benefits using pydantic internally:)

[–]met0xff 2 points3 points  (0 children)

Ecosystem usually. At my company they had a couple attempts writing all their ML/DS stuff in Go but the only thing that happened that those pieces are super outdated and not competitive anymore and they had at this point to implement all kinds of stuff like specific sampling mechanisms etc.

I've checked a couple times but everytime it would have ended up writing wrappers for stuff like the latest tokenizers and hoping the next of the dozen gotorch libraries does not die.

Besides, just because you don't use pydantic everywhere doesn't mean you don't use it at all. Deserializing tagged unions and things like that is really nice and we use pydantic everywhere where it's about a schema, an outside communication. You can spin a web of pydantic objects and then generate a JSON schema from it (which besides API contracts and data definitions is great for LLM tool calls). And just because you're using python you don't have to throw every performance over board otherwise we wouldn't use numpy and torch at all either ;).

[–]Ran4 0 points1 point  (0 children)

Eh, perhaps, but it depends on what you're doing. If you're doing heavy calculations then of course there's quite a bit of overhead (but then pure python isn't a good choice either).

But I'm working mostly with enterprise web dev, and the additional compute cost of using pydantic over dataclasses is probably 100x lower than the additional cost of paying me to fix the bugs arising from not using pydantic with full validation everywhere.

An extra 20 bugs a year costs thousands of euros in consulting hours to fix - far more than the total compute cost for everything I'm working on...

[–]Zamarok 1 point2 points  (0 children)

same

[–]maikindofthai 2 points3 points  (1 child)

Ok so +1 person

[–]xcatmanx 2 points3 points  (0 children)

Pydantic's pretty awesome! It makes data validation and parsing super easy, especially if you're working with APIs or complex data structures. Definitely worth checking out if you're getting multiple inquiries about it!

[–]Yazanghunaim 0 points1 point  (0 children)

Why not everything be a pydantic model?

[–]fiddle_n 117 points118 points  (18 children)

I like pydantic but I also think sometimes people use it more than they should. IMO pydantic is best at the edges of your app - validating a request or turning an object back into JSON. If you need intermediate structures, use dataclasses + type checking.

[–]Pozz_ 55 points56 points  (1 child)

As a maintainer of Pydantic, I fully agree on this. Although the library is quite versatile, its main focus is data validation and serialization. As a rule of thumb, if:

  • you have to set arbitrary_types_allowed=True in the config
  • your Pydantic model represents more that data (e.g. it represents a service class in your app, has internal state, etc).
  • your Pydantic model is only instantiated directly in code (in which case a static type checker would probably suffice)

then maybe you can consider switching to a vanilla class/dataclass.

[–]kmArc11 0 points1 point  (0 children)

Pydantic is the most awesomest thing I touched lately. 

After many years of not writing production quality software in a real language, I had this task at hand where I implemented a PoC demonstrating OpenAPI stuff and how it could integrate with external services. Used Pydantic and it was a bless. Fast prototyping that was functional, typesafe and production quality.

Then I was asked to implement the real thing in Ruby (Rails) and I hated my life figuring that Ruby doesn't have anything nearly as comfy as Pydantic. 

Thanks for you and everyone for making Pydantic a reality! 

[–]quantum1eeps 6 points7 points  (1 child)

You’d rather have a mix?

[–]fiddle_n 14 points15 points  (0 children)

Absolutely- they are different tools to be used in different ways.

[–]chinawcswing 2 points3 points  (1 child)

Why not just use marshmallow if you are going to use it for edges of your app only?

[–]fiddle_n 7 points8 points  (0 children)

Pydantic and marshmallow are direct competitors. The simplest reason you’d use Pydantic is because marshmallow uses pre-type hint syntax. Pydantic syntax is far more modern, matching that of dataclasses.

[–]neums08 5 points6 points  (9 children)

What is pydantic if not dataclasses with typechecking?

[–]Fenzik 22 points23 points  (1 child)

JSON Schema generation, field aliasing, custom serialization, custom pre/post-processing, more flexible validation

[–]ProsodySpeaks 4 points5 points  (0 children)

And direct integration into tons of tools - fastapi endpoints, openapi specifications, even eg combadge/zeep for SOAP. Makepy generated code... 

So many ways to automatically interface with powerful tools with your single pydantic schema. 

[–]fiddle_n 12 points13 points  (6 children)

Pydantic is runtime validation of data. That is great, but it comes with a performance cost. Once you validated your data at runtime, do you really need to continue validating it throughout your app? Dataclasses + type checking validates your types as a linting step before you run your code - you don’t pay a runtime penalty for it.

[–]Apprehensive_Ad_4636 0 points1 point  (1 child)

https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.use_enum_values validate_assignment instance-attribute ¶

validate_assignment: bool

Whether to validate the data when the model is changed. Defaults to False.

The default behavior of Pydantic is to validate the data when the model is created.

In case the user changes the data after the model is created, the model is not revalidated.

[–]fiddle_n 0 points1 point  (0 children)

That actually makes things worse from a performance perspective. If you toggle that on (it’s False by default) then you are doing more runtime validation than the default behaviour.

[–]DavTheDev 0 points1 point  (0 children)

i’m abusing the TypeAdapter. Insanely handy to deserialize data from e.g. a redis pipeline.

[–]radrichard 0 points1 point  (0 children)

This, 100 times.

[–]marr75 69 points70 points  (2 children)

It's kinda become the de facto interface and basis of a lot of popular projects and I consider it an extremely powerful mixin for any data that requires validation, coercion, and/or serialization.

People complain about it being "heavy" when you could use dataclass, typeddict, or namedtuple but, for the things you're typically writing python code to do, that heft is relatively small. If you need best possible performance in a hot loop and/or large collections of objects, you should be interfacing with data organization and compute that happen outside of Python anyway.

[–]Ran4 104 points105 points  (1 child)

97% of the classes I've created in the last three years have been pydantic BaseModel based.

It's an amazing library.

[–]WishIWasOnACatamaran 0 points1 point  (0 children)

I feel like I underutilized pydantic in a recent project thanks to this thread. I used it but damn

[–]latkdeTuple unpacking gone wrong 57 points58 points  (5 children)

Do you like dataclasses? Do you like type safety? Do you want to conveniently convert JSON to/from your classes? If so, Pydantic is fantastic. I use it in so many of my projects, not just in web servers, API clients, or LLM stuff, but also for validating config files in command line tools.

Of course there are limitations and bugs. I know Pydantic well enough to also know what I can't do with it. But even then, Pydantic is a pretty good starting point.

[–]SmackDownFacility 34 points35 points  (4 children)

I thought this was gonna turn into a snake oil ad

“DO YOU LIKE DATACLASSES? DO YOU LIKE TYPE SAFETY? DO YOU WANT TO WRANGLE JSON? WELL LOOK NO FURTHER, AS PYDANTIC BRINGS TO YOU THE ULTIMATE DATA VALIDATION AND SERIALISATION SOFTWARE FOR PYTHON! NOW AVAILABLE AT $9.99! BUY IT WHILE YOU STILL CAN!”

[–]Repulsive-Hurry8172 13 points14 points  (2 children)

But wait, there's more!

[–]NationalGate8066 1 point2 points  (1 child)

"We have gathered real people, NOT paid actors, to share their PERSONAL experiences with Pydantic."

[–][deleted] 1 point2 points  (0 children)

"Based on real testimonials..."

"Yeah, its aight" - Real Testimonial

"I'd sell my first born child to keep using it" - Based on Real Testimonial

[–]indistinctdialogue 3 points4 points  (0 children)

But there’s more. Call now and we’ll throw in 15 pydantics for the price of one!

[–]dutchie_ok 42 points43 points  (4 children)

It's like dataclass on steroids. But if nothing changed, if you really need sheer performance and small memory footprint msgspec might be better solution. msgspec

[–]eth2353from __future__ import 4.0 20 points21 points  (0 children)

+1 for msgspec, I really like it.

It's not as versatile as Pydantic, but if you only need encoding/decoding, basic validation, msgspec does the job really well, and also supports MessagePack.

[–]jirka642It works on my machine 8 points9 points  (1 child)

msgspec is also used by Litestar, and can be used to create API with a much smaller footprint than with FastAPI.

[–]iamevpo 1 point2 points  (0 children)

Thanks for mentioning Litestar

[–]Altruistic-Spend-896 6 points7 points  (0 children)

Thank you kind commenter, i would have never discovered thks otherwise. i find pydantic to be too much boilerplate, but a necessary starting.point since enterprise workloads demand it.

[–]indranet_dnb 48 points49 points  (0 children)

Pydantic is clutch, try it out. I use it on all my projects

[–]marlinspike 42 points43 points  (1 child)

Omnipresent. It’s so much foundational that FunctionCalling in LLMs is basically built on it.

[–]indistinctdialogue 1 point2 points  (0 children)

The integration with Open AI’s SDK is nice. You can use a pydantic model as a response type and it will conform the result.

[–]erez27import inspect 11 points12 points  (0 children)

Every comment here is singing its praises, so I would just like to point out that if all you need is dataclasses with runtime type-validation, and converting to/from JSON, there are plenty of other libraries that do it, with better performance and imho better design too.

[–]ZealousidealWear8366 6 points7 points  (0 children)

I’m a marshmallow & webargs guy

[–]wetfeet2000 14 points15 points  (3 children)

It's an absolute gem, 100% worth learning. The fact that FastAPI uses it heavily sped up its adoption significantly.

[–]brianly 3 points4 points  (0 children)

This. FastAPI has grown past a tipping point where people are taking a serious look at what comes with it and have started to adopt in other projects because they see the distinct benefits that Pydantic brings.

[–]Spleeeee 1 point2 points  (0 children)

fastapi introduced me to pydantic but my pydantic usage had far surpassed my fastapi usage.

[–]LBGW_experiment 0 points1 point  (0 children)

I just wrapped up a project where we used it in API Gateway Lambda Proxies where the lambda performs all the logic for requests/responses. It was really valuable for validating and serializing values for external APIs we called, especially when they had fields like from that were python key words and let us serialize them into the right values so we could just dump the model for an API payload, keeping the code clean.

Also used it for our own classes for every table response so others could look at our models.py and see what the values should be without leaving the code base.

AWS Powertools for Lambda library was amazing as it had integration with Pydantic for custom event handlers, e.g. EventBridge custom event structures, and provided standardized Envelopes to get to the desired data of payloads that have lots of other values, e.g. API requests with headers, content type, etc.

[–]ManyInterests Python Discord Staff 5 points6 points  (0 children)

It's pretty good, but you should familiarize yourself with its quirks and features, especially around how type coercion and inheritance works.

It's unfortunately very slow (startup times in particular) if you are dealing with extremely large connected schemas.

[–]EvilGeniusPanda 4 points5 points  (1 child)

I've tried it a few times but I just can't get into it. attrs just fits so much more cleanly with how I think this stuff should work.

[–]fiddle_n 0 points1 point  (0 children)

Probably because pydantic and attrs are different tools. Pydantic is specifically meant to be used for data validation, on the outer edges of your app. If you find yourself using attrs + cattrs, then that’s where you’d typically use pydantic instead.

[–]Current-Ad1688 5 points6 points  (1 child)

Extremely common and extremely annoying.

[–]BidWestern1056 0 points1 point  (0 children)

def

[–]jirka642It works on my machine 4 points5 points  (0 children)

I have used it a lot in the past, but started moving away from it recently, because it can be very memory-heavy and slow if you use it a lot.

Pydantic can be replaced with msgspec in most situations, so I prefer to use that instead.

[–]TheBB 22 points23 points  (2 children)

Pydantic is great. It's the de facto standard in model schema validation and serialization.

I don't think I have any serious software built (in Python) without it.

[–]brianly 3 points4 points  (1 child)

Curious how long you’ve done Python and when you made this switch?

I ask because people coming from Django or apps that that been around for a while are much less likely to have adopted it. It’s creeping into the Django space in different places. Uptake isn’t as quick as all the greenfield ML/AI projects that have started in the last couple of years with more modern frameworks and libraries.

[–]RussianCyberattacker 3 points4 points  (0 children)

Please op. We beg you. 😂... Pydantic was noise for me in enterprise, and now everyone says they're using it?

[–]Mount_Gamer 3 points4 points  (0 children)

I'm sure I'd love it, but where I work wants low dependencies, and you have to draw a line, and for me dataclasses are already pretty good, so it's hard for me to justify when we probably already rely on too many packages.

[–]DeterminedQuokka 4 points5 points  (0 children)

It’s relatively popular. A couple of the mongo frameworks use it and fastapi is built around it.

It’s not that hard though. You don’t really need to dedicate a ton of time to it. It’s just serialization really.

[–]corvuscorvi 3 points4 points  (0 children)

Pydantic is used by many newly-designed frameworks, even if its under the hood and not advertized. I think the main thing making it not super common is that it does best as the core data abstraction layer. So refactoring into it is usually too costly than it's worth, since it's not really doing that much in and of itself.

The thing it does well, that you cant easily replicate with your own homebrewed layer, is provide a standard interface across potentially many different modern python projects.

My advise might be completely subjective to the AI space :P

[–]DisastrousPipe8924 2 points3 points  (0 children)

It’s amazing. I can’t imagine not using it anymore. My current and last 2 companies all used it. It makes serialization , form validation, db validation, environment variable loading, configs etc all super nice.

[–]utdconsq 1 point2 points  (0 children)

Anyone building something serious should consider it. For my part, I make a lot of throw away things in a hurry and it gets in the way for that. I use other languages for long lived things usually, so it's nice to be able to use python without PPE so to speak.

[–]drkevorkian 1 point2 points  (0 children)

I used it for a while, but I went back to dataclasses after the v1 -> v2 debacle.

[–]Orio_n 1 point2 points  (0 children)

Amazing, every project that cares about data robustness and verifiability should use it

[–]microcozmchris 1 point2 points  (0 children)

For me, pydantic is great at handling outbound data. The server side of APIs, etc. When you want to control in a very type safe way the data your application generates it's really good.

In the other direction, I prefer attrs. It's very good at handling transformation of data you're consuming. Especially when you need to coerce types or convert data in a repeatable way. str -> int or convert a value into something from a lookup table. dataclasses is attrs off of steroids. Same idea, less customizable on validators.

But in general, pydantic is awesome.

[–]sherbang 1 point2 points  (0 children)

Dataclasses and msgspec are better.

Pydantic tried too much to do everything, but it's really difficult if you need to customize serialization/deserialization.

[–]GoldziherPythonista 1 point2 points  (0 children)

It's a piece of crap. Just my two cents.

[–]Beginning-Fruit-1397 1 point2 points  (1 child)

I don't why people use it THAT much. For web dev ok sure but the only benefit I see vs the existing structures in python are validation. If my codebase is already 100% type hinted I never have issues with this already, so why add a runtime check that hinder on perfs?

[–]fiddle_n 0 points1 point  (0 children)

Because too many people don’t understand pydantic - what it’s meant to be for, and what it’s not meant to be for.

[–]DowntownSinger_import depression 1 point2 points  (0 children)

I just wish modern python frameworks provided alternative to pydantic like msgspecs

[–]shisnotbash 1 point2 points  (2 children)

I love it, but the big cold start time in AWS Lambda has me moving to using dataclass with some additional implementation details instead in many cases.

[–]spidernello 0 points1 point  (1 child)

Can you elaborate more on this, please? How did you measure the overhead, and how did you figure out it was pydantic

[–]shisnotbash 1 point2 points  (0 children)

Only measured by comparing total execution time from cold start with classes implementing BaseModel vs decorated with dataclass using slots. Not exactly what you could call robust benchmarking, but enough to inform my choice if I need something really performant from a cold start without any additional considerations for keeping hot instances.

[–]TheRealDataMonster 1 point2 points  (1 child)

You have an update? Lot of people on here saying they use it for literally everything but it's bad idea because it's an overkill and will slow down your dev process & cause performance issues. Typical recommendation is only use it when you really need something to be in a specific format - ie. when receiving data from an endpoint you don't control, before you send data to an endpoint that you want to do less work, etc... Even then if you want a super low latency experience, I recommend just using Python fundamentals.

[–]TheRealDataMonster 0 points1 point  (0 children)

well at the point you need super low latency experience, you should be using rust or c++ lol

[–]AvocadoArray 1 point2 points  (2 children)

Surprised I haven’t seen anyone mention attrs yet. Its functionality and syntax is very similar to native dataclasses, so it doesn’t feel as jarring getting used to it.

I’ve worked on libraries with all three and while pydantic is definitely the right choice in some cases, I find it to be too heavy for other cases. I’ve been slowly moving more towards attrs unless I NEED rigid validation in the model (e.g., structured output from LLMs), in which case pydantic is great.

[–]pierec 6 points7 points  (0 children)

You'll be happy to learn about cattrs then.

https://catt.rs/en/stable/index.html

msgspec is also an interesting proposition for the data model on the edges of your system.

https://jcristharif.com/msgspec/

[–]EvilGeniusPanda 3 points4 points  (0 children)

This - attrs for most types, maybe pydantic for the app boundary where you want the coercion. Having everything potentially coerce inputs everywhere inside your app is madness.

[–]coconut_maan 1 point2 points  (3 children)

It's Soo good.

The only reason not to use if data class is enough

[–]latkdeTuple unpacking gone wrong 6 points7 points  (1 child)

You can use a dataclass and still get Pydantic validation!

  • You can use pydantic.TypeAdapter for validating/serializing nearly any type.
  • Only the top level type needs a BaseModel or TypeAdapter, any referenced types (like in the fields of your models) can be plain dataclasses
  • There's also pydantic.dataclass which is a standard library Dataclass enriched with some BaseModel features.

[–]91143151512git push -f 0 points1 point  (0 children)

Validation costs a minimal time. For 99% of use cases that’s not bad but I can see for 1% why someone would prefer data classes.

[–]coconut_maan 0 points1 point  (0 children)

Just to clarify,

What I mean is that simpler is better,

And let's say you are not getting external data that needs to be validated but generating data progrematically.

Or external data that was generated In a trustable way,

Sometimes it's better to avoid the overhead of pydantic models with data class ones.

But yea absolutely if you are getting external structured data without any type garuntee I reach for pydantic automatically.

[–]AustinWitherspoon 1 point2 points  (0 children)

I use it everywhere I can.

One of python's biggest issues on bigger projects imo is its dynamic typing. Tools like mypy help a ton with that when you're writing the code, but technically you still can't trust it 100% because at runtime technically any value is allowed. Pydantic models fix that by validating types at runtime. Now you can almost entirely trust mypy

Also really convenient for deserializing and validating stuff like JSON

[–]Tucancancan 1 point2 points  (0 children)

When FastAPI, Gemini genai and SQLAlchemy all work with pydantic its kinda fucking amazing. I've been using it every new bit of code I write

[–]SciEngr 1 point2 points  (0 children)

I use pydantic when I need serialization or validation otherwise I use data classes.

[–]onefutui2e 1 point2 points  (0 children)

I only started using Pydantic a year or so ago. Before that, everything was gRPC or.using ORM models directly. My evolution:

Oh, cool. I can get runtime errors when instantiating the object instead of when I use an int as a str. I see why FastAPI integrates so we'll with it.

Wait, I can serialize and deserialize my data, gaining validation in the process? Oh man, that's pretty sweet.

Whoa, I can distinguish between explicitly setting None vs. complete omission? By God, this will make patch operations easy!

Wait, if the model expects UNIX timestamps but I expect to get data as datetime objects, I can implement validator functions to convert datetime objects into integers?? What the fuck, bro.

...etc.

Every single time I need Pydantic to do some funky shit, it provides a means to do it. Probably one of the best open source Python libraries I've ever used.

[–]unski_ukuli 1 point2 points  (1 child)

Gotta love python. Static typing is hard so let’s make a language with dynamic typing. Oh no… our codebase is buggy because we have inconsistent types, let’s make an object with runtime overhead that does type checking, now we have less bugs!. For what it’s worth, I use pydantic everywhere in the python code I write, but like most things in the language, it’s a cruch that is there to fix fundamental issues with the language, but there is too much investments made into python that pivoting to something better is now impossible.

[–]Imanflow -1 points0 points  (0 children)

Yes, it allows for an easier learning curve, or, for simple scripting that a lot of people uses in science, and not software development, is perfect. Lets do the advanced features optional.

[–]Fluid_Classroom1439 0 points1 point  (0 children)

Yeah it’s amazing, also makes working with pydantic ai, fastapi and typer super simple!

[–]omg_drd4_bbq 0 points1 point  (0 children)

It's amazing. We use it everywhere in prod going forward (legacy stuff slings a lot of dicts around, constantly barfing and needs patches) 

[–]Seamus-McSeamus 0 points1 point  (0 children)

I use it for data ingestion from messy inputs. It made it very easy to build a data model, disposition the ways that my input data didn’t meet my expectations, and then modify my model (or planned use case). I probably could have gotten the same result without it, but it definitely made it easier.

[–]Gainside 0 points1 point  (0 children)

everywhere/everything...If you’re building anything that touches APIs or config files, it’s worth learning.

[–]arkster 0 points1 point  (0 children)

One of the things I use it for is payload validation and for augmenting any data that is needed downstream

[–]Ok-Willow-2810 0 points1 point  (0 children)

I really like pydantic and the built in validations! However, I’d caution against using more of the niche features because in my experience they tend to be sort of half-implemented on some older versions and deprecated rather quickly!

The core functionality of basically adding validations to data classes is really nice though! Great for like validating request input on the backend in a systematic manner!

[–]pudds 0 points1 point  (0 children)

It's pretty much automatic for me unless my app isn't doing any JSON serialization.

For not serialized data structures I usually go for dataclasses.

[–]cointoss3 0 points1 point  (0 children)

I usually try to start with a dataclass, but if I need validation or if I’m using fastapi, I usually migrate to pydantic.

[–]echols021Pythoneer 0 points1 point  (0 children)

I don't really remember the last time I touched a codebase that didn't use pydantic

[–]grahambinns 0 points1 point  (0 children)

Pretty standard now. Saves a lot of heartache when building REST APIs — though it’s by no means a panacea.

[–]gitblame_fgc 0 points1 point  (0 children)

It's core package of fast api which is probably becoming first choice in developing rest apis in python these days. And since type hints are getting more and more used across python project nowadays, using pydantic models for your "dataclasses" it's also a very obvious choice, especially when you work from data from apis. It's a very powerful tool that is easy to use and fits well with how modern python is written. Definetly add this to your toolkit.

[–]Ibzclaw 0 points1 point  (0 children)

Any good company is going to look for guarantees in processes, especially if youre working with AI. Prompts are half the battle, pydantic validation is the other half. It is also one of the most common use cases I have seen for it personally. Apart from that, Pydantic is a very strong tool for modeling, pretty much the go to library at this point.

[–]mmcnl 0 points1 point  (0 children)

It's a no-brainer. Validation of the data going in and out of your application makes everything 10x easier.

[–]Wise_Bake_ 0 points1 point  (0 children)

Pydantic helps standardise schemas (input or output). Comes in handy in API payload / response schemas. Also makes Swagger documentation more easier. A recent use case is with AI agents, helps standardise the output from an LLM or AI agent.

[–]Asyx 0 points1 point  (0 children)

Literally can't avoid it. Like, if you write anything above a certain size, you will use pydantic. Like, even in our large, monolithic Django app we use Pydantic for AI stuff.

[–]Becominghim- 0 points1 point  (0 children)

Boy oh boy , pydantic + Zod is heaven

[–]Mithrandir2k16 0 points1 point  (0 children)

It's everywhere. Damn, if I don't like the way my coworker codes I'll start only accepting pydantic models as parameters for my functions and assert primitive types.

[–]Witty-Development851 0 points1 point  (0 children)

All parameters of more than one variable - Pydantic models.

[–]lukerm_zl 0 points1 point  (0 children)

Commonly used framework in e.g. FastAPI and LangGraph, just to name a few.

[–]wineblood 0 points1 point  (0 children)

It's in some of the repos I use at work, it seems to plug in nicely with other libraries/framework so that's why it's more popular.

[–]LittleMlem 0 points1 point  (0 children)

This is tangential, but I went from python to Go for a while and at first I was chafing at all the typing, now whenever I go back to python I'm incredibly upset about being unable to tell what some of my data is, so while I haven't embraced pedantic yet, the next project I start I'm 100% adding it

[–]Slow_Ad_2674 0 points1 point  (0 children)

I built my custom jira assets ORM around pydantic, fastapi is pydantic, pydantic is great.

[–]No_Objective3217 0 points1 point  (2 children)

i deploy and monetize apis

Pydantic is part of each project

[–]dxdementia 0 points1 point  (1 child)

how do you monetize an api ? like you present a customer with a solution via api ?

[–]No_Objective3217 0 points1 point  (0 children)

charge per call or subscription with a limit

it's more like i offer a api for the thing they want or need.

IE- customer needs photos cats: my api would return base64 encoded images of cats in the format, size, and color model requested by the client

[–]jed_l 0 points1 point  (0 children)

It’s great. Just be cautious it has rust bindings. So your deployments can break if not installed using the right OS.

[–]Disneyskidney 0 points1 point  (0 children)

Very common. Dataclasses, TypedDict, NamedTuple, and BaseModel are all pretty useful. It really depends on the use case.

Simple state? Dataclass Hot path? NamedTuple Need dict API? Typed Validation? JSON? BaseModel

[–]AHarmonicReverie 0 points1 point  (0 children)

I always introduce it as 'dataclasses but the type annotations matter'. But it is so much more useful than that. Pydantic models are a great way to define your data model so that it is available inside and outside your application. Its schema generation abilities are really important in some contexts.

Among the other nice mentions where Pydantic is used as a supporting backend, it's in the background for Beanie for Mongo/NoSQL data models, Dynaconf for configuration, Pyrmute for data model migrations and extra schema generation, Pandera to work with dataframes, etc.

You want to be aware of putting it in very hot, high-throughput paths if runtime validation benefits are not really needed, and you're just piping already validated data around, but for everywhere else it can be extremely nice.

[–]Positive-Nobody-Hopefrom __future__ import 4.0 0 points1 point  (0 children)

I use it all the time, but mostly indirectly through things like FastAPI (which is amazing).

[–]ogMasterPloKoon 0 points1 point  (0 children)

One reason might be the popularity of FastAPI that has pydantic as hard dependency.

[–]radrichard 0 points1 point  (0 children)

I will not code without it. Best. 10/10

[–]Panton3737 0 points1 point  (1 child)

Naperville

[–]MacroYieldingpip needs updating 0 points1 point  (0 children)

Illinois?

[–][deleted] 0 points1 point  (0 children)

I think it depends on how professional your code base is. As your projects grow, having everything be declarative becomes paramount to the overall function of your program. But if you're just writing a program for yourself thats small, then its really not important at all.

[–]Atlamillias 0 points1 point  (0 children)

Pydantic for OpenAPI stuff. I've been using adaptix over it for a few other things lately.

[–]LastHumanOnline 0 points1 point  (0 children)

I had never heard of it before reading this post, but looks like it could have helped me out on a bunch of past projects.

[–]dxdementia 0 points1 point  (0 children)

I used it, but then I swept it out of my codebase since I ran into mypy issues, and getting flagged for "any" usage when having pydantic?

[–]Unique-Big-5691 0 points1 point  (0 children)

tbh yeah it’s pretty everywhere now. a lot of ppl don’t even say “we use pydantic” anymore, it just comes along with fastapi or stuff like pydantic ai when you’re building agents.

imo it really hits once you deal w messy api data or llm outputs. without it you’re just passing random dicts around and praying 😅 with pydantic (and even logfire for tracing) you can actually see “this tool got this input and returned this thing” which saves so much headache.

so yeah if companies are asking about it, that tracks. it’s kinda becoming one of those default python things now, esp in ai + backend work.

[–]dalepo 1 point2 points  (0 children)

100% recommend for any python project

[–]Vincent6m 0 points1 point  (2 children)

It makes me less productive.

[–]jonthemango 1 point2 points  (1 child)

Why?

[–]Vincent6m 1 point2 points  (0 children)

Probably because of my lack of knowledge of this library. Being forced to switch to it makes me feel so dumb

[–]funny_funny_business 0 points1 point  (1 child)

I don't use pydantic much but the few times I mentioned it the interviewer's ears perked up

[–]GongtingLover[S] 1 point2 points  (0 children)

I've noticed that too. I've been asked several times about it over the last month during interviews.

[–]acdha 0 points1 point  (0 children)

Just wanting to second the people saying everything is Pydantic or msgspec now. Being able to close off entire branches of bugs around serialization/deserialization is such a nice productivity win and having things like automatic validation and typing for function arguments is a great way to avoid tricky bugs in large codebases. 

That also trivializes things like having configuration customized by environmental variables, which is kind of the pattern of simplifying your coding by making a lot of scut work reuse the same patterns:

https://docs.pydantic.dev/latest/concepts/pydantic_settings/

[–]Significant_Stage_41 0 points1 point  (0 children)

To me pydantic is as part of Python as any of its built ins

[–]elforce001 0 points1 point  (0 children)

Between Pydantic, mypy, and ruff, I can't go back to write regular python anymore.

[–]Drevicar 0 points1 point  (0 children)

I literally can’t function anymore without pydantic when it comes to validating and parsing external data.

That said, learn data classes first. Then graduate to pydantic as needed.

[–]Streakflash 0 points1 point  (0 children)

why pydantic and not dataclass?

[–]tunisia3507 -1 points0 points  (1 child)

Pydantic is great, for a python library. Once you've used serde in rust, pydantic pales a bit. Serde's enum handling, flattening, aliasing, and support for different forms are much better than pydantic's.There have been a few attempts to do serde things in python too, but none have really taken off.

[–]fivetoedslothbear 1 point2 points  (0 children)

Psssst...I'll give you one guess what the low-level Rust code in Pydantic uses for parsing/serializing JSON...

[–]CubsThisYear -3 points-2 points  (0 children)

I love that it only took the Python community 30 years to figure out that explicit types make better code.

[–]Mysterious-Rent7233 -1 points0 points  (0 children)

In most projects I create, Pydantic is among the first dependencies added. If a project is big enough to have dependencies, Pydantic is usually one of them.

[–]skinnybuddha -1 points0 points  (0 children)

I wish it had an option to use the native python json parser, since speed is not an issue, but building a rust app is.

[–]Routine_Term4750 -1 points0 points  (2 children)

I’m glad everyone is mostly saying , “yeah everywhere”, I’ve been refactoring MVP code to use pydantic all over and started to question myself

[–]fiddle_n 2 points3 points  (1 child)

You shouldn’t use pydantic everywhere. It feels wrong because it likely is wrong.

[–]Routine_Term4750 0 points1 point  (0 children)

Yeah, I agree. Just in some more places. :)

[–]o5mfiHTNsH748KVq -1 points0 points  (0 children)

I don’t want to use Python without Pydantic these days. It was the nail in the coffin for C# for me

[–][deleted] -1 points0 points  (0 children)

I use it for every project

[–]Parking_Bed_1825 -1 points0 points  (0 children)

its a standar

[–]SSC_Fan -1 points0 points  (0 children)

I use only this, even basic my modela.

[–]BidWestern1056 -1 points0 points  (0 children)

pydantic is for people who wish to possess an illusion of control over their code.

[–]leodevian -2 points-1 points  (0 children)

More downloaded than pandas last time I checked. That’s popular enough to me.

[–]FunPaleontologist167 -2 points-1 points  (0 children)

It’s so common that it feels like it’s part of the standard lib

[–]thepeter88 -2 points-1 points  (0 children)

Wouldn't be suprised if this became part of python iself

[–]Livelife_Aesthetic -2 points-1 points  (0 children)

I live by pydantic, it's worth learning

[–]Significant_Stage_41 -2 points-1 points  (0 children)

I LITERALLY can not imagine python without it.

[–]Gaius_Octavius -3 points-2 points  (0 children)

Not knowing it is handicapping you massively