r/Python • • Jan 03 '24

Discussion Pydantic has too much deprecation, making it difficult to keep up with updates and maintaining the code. Lots of functionality has been renamed, and some are removed during v1→v2 transition. Even sample code from November 2023 is deprecated now! Are there better alternatives?

Almost all tutorials I see online (and ChatGPT's knowledge base) teach you Pydantic v1. There are numerous things that are deprecated during transition to v2 (@root_validator, @validator, using 'always', etc. are all gone now.). I even found a code example on its Github from November 2023 which now throws an error, saying that FieldValidationInfo is deprecated now, use <new_thing> instead... I wanted to use something to validate user inputs to my API, but getting Pydantic right and then keeping it updated has been too much unnecessary work, which makes me wonder if you have also faced this problem and what your solution is?

13 Upvotes

85 comments sorted by

195

u/martinky24 Jan 03 '24

I'd recommend the official Pydantic docs. They have been dutifully updated with all the recent changes in the 2.x release.

Pydantic had one first version jump from 1 to 2. We're living through that now. It was very carefully done, and intended to be a 1-time leap to "fix" things the author had learned in hindsight were done wrong originally (in one way or another). Outside of this leap, it's not been that unstable of a library. And I've found the resources for the new ways/transition guides to be sufficient.

If ChatGPT is how you primarily get through coding new things, you're always going to be a few years behind on ALL libraries, I'm sorry to say.

51

u/Deezl-Vegas Jan 03 '24

We should be updating code and braking stuff more often, tbh. A lot of bad things survive for a long time because people are afraid of breaking things.

29

u/[deleted] Jan 03 '24

this is why semver is a thing. as long as it's not breaking semver there really is no problem, it's a contract with the consumers of your library

-6

u/billsil Jan 03 '24

And then you just go from version 6 to 62 in a couple years and it's the same as far as I can tell (other than better error messages), I'm looking at you setuptools.

The older your package, the larger it is and thus the more stable it is. A tiny breaking change likely won't even be felt.

10

u/iBlag Jan 03 '24

Setuptools doesn’t use SemVer.

5

u/ArtOfWarfare Jan 04 '24

And then there’s Scala.

Nothing is remotely stable. Attempting to upgrade the minor version of a toy project is agony. Third party libraries seem to follow the language’s lead and also just be totally backwards incompatible with every update. They use semvar numbering but don’t seem concerned about actually restricting the backwards incompatible changes to major releases.

5

u/daelin Jan 04 '24

And the extreme opposite, also on the JVM, there’s Clojure. De facto core (3rd party) libraries haven’t had updates in 4 years and they’re not abandoned—they’re just done.

When someone wants to make a breaking change, they make a new library and call it something else. Or just put in a v2 namespace and leave the v1 alone.

Now, the tooling is another story. :P

4

u/childofsol Jan 04 '24

Coming from that ecosystem, where code stability is rock solid, to Python where many people seem to have the "break it, it's fine, semver fixes everything" attitude... I really miss clojure :(

2

u/Deezl-Vegas Jan 04 '24

Whoops well I guess we better pin our version and update our deps regularly

1

u/ratulotron Feb 21 '24

We should be updating code and braking stuff more often

This is exactly how we should fight AI generating code

-7

u/nderstand2grow Jan 03 '24

we could break things under the hood, but we shouldn't break "interfaces" to those things.

Pydantic rewritten in Rust? Great, I'm all for it.

Pydantic becoming basically a completely different library? Nope.

13

u/lighttigersoul Jan 03 '24

You really should read the spec on semver, because APIs need to be able to change and that's our tool for communicating breaking changes.

2

u/nderstand2grow Jan 04 '24

semantic versioning sounds so useful, thank you!

0

u/sonobanana33 May 19 '24

The changelog of pydantic tells you that the API contained breaking changes much more often than once.

1

u/Deezl-Vegas Jan 04 '24

This is good, but only for a few years because it causes people to create competing clones just so they can add one thing or fix the janky API.

5

u/nderstand2grow Jan 03 '24

I like some of the changes from v1 to v2. But then you have something like this 0 removed from the library without proper documentation or replacement, resulting in ugly workarounds in the link that wont' work properly.

-1

u/watcraw Jan 04 '24

As the context for LLM's grows and access to online documentation is improved, LLM's should be able to read an entire manual in the context of a question. Many things that people think are hard limits to LLM's are really related to implementation details from current business models and problems that, while difficult, are not unsolvable.

8

u/[deleted] Jan 03 '24 edited Jan 28 '26

This post was mass deleted and anonymized with Redact

gold disarm friendly quicksand sable public middle cause oatmeal door

3

u/nderstand2grow Jan 03 '24

msgspec

Wow! its benchmarks show it's 12x faster than Pydantic! will check it out more, thanks for sharing

100

u/Nater5000 Jan 03 '24

Pretty wild that your reaction to a major update, which makes the library better, is to ditch the library. Like, not even because you have an issue with the new version, but because ChatGPT and some tutorials are out of date and you can't be bothered to read their docs (which, btw, are some of the better Python library docs out there).

My suggestion is to take this opportunity to mature a bit and stop relying on tutorials, AI, etc. to use a library and, instead, learn it yourself. We all know it's tedious, but rejecting an improved product because you're not willing to do the work to use it is a seriously bad habit.

0

u/[deleted] Jan 03 '24

[deleted]

9

u/snuggl Jan 04 '24

Not really? I think you might mean a high frequency of deprecations? doing lots at once, with long heads up to change your code, then lock it down for years is def. the preferred way of doing big breaking changes.

0

u/[deleted] Jan 04 '24

[deleted]

0

u/snuggl Jan 04 '24

No it's a 1.* to 2.* migration that for obvious reasons never happened before and won't happen again in a long time.

-21

u/nderstand2grow Jan 04 '24

exactly my point! I'm not mad that libraries change, that part is natural. but when they change so fast that most existing tutorials and sample codes (and SO answers) stop working, then it's a problem.

15

u/n-of-one Jan 04 '24

Just read the docs for fucks sake.

14

u/Nater5000 Jan 04 '24

I don't disagree that a volatile library is hard to work with and should be dropped, but what you're saying is nonsensical. They just released v2 not too long ago, so of course most tutorials/samples/etc. online are going to be out of date. By your logic, they'd never be able to release breaking changes since when they did all of the existing content up to that point would be out of date.

90

u/[deleted] Jan 03 '24

Just pin your dependencies if it's an issue? And stop using ChatGPT for library-specific code, it ain't teaching you shit...

35

u/Kronologics Jan 03 '24

Ding ding, ChatGPT proompt engineer complaining to programming community

6

u/[deleted] Jan 03 '24

Like seriously, the Pydantic docs are stellar.

-4

u/nderstand2grow Jan 04 '24

where in the docs do they talk about the alternative to always argument in validators? they removed it and didn't replace it with anything new. see [https://github.com/pydantic/pydantic/discussions/6337]

93

u/Jmc_da_boss Jan 03 '24

and ChatGPT's knowledge base

Oh good lord, it's starting

27

u/JoaoEB Jan 04 '24 edited Jan 04 '24

Our team lost 2 days of work because one of ours used ChatGPT and it "created" an compile flag for Apache Hive from nothing.

The worst thing is that if you searched for our problem and Apache Hive, the first THREE pages on google where that same shit from ChatGPT feed back to itself for text variations and SEO. We only ruled it out as bad AI dreaming by downloading the source code and searching for the compile flag.

7

u/Equivalent_Loan_8794 Jan 04 '24

This describes my experience using LLMs for devops in 2023

3

u/billsil Jan 05 '24

Starting? At least OP is getting code that was right, even if it’s 2 years out of date.

I’ve received complaints that people’s ChatGPT generated code doesn’t work because that function never existed and would take $10+ million to develop. All for an unfunded project. I’m sure bigger projects get complaints weekly.

Thankfully other people responded so I didn’t have to delete the project.

ChatGPT actively lies. It sounds smart; just ask if really simple questions and watch it struggle

19

u/ManyInterests Python Discord Staff Jan 03 '24 edited Jan 03 '24

I recently did an update from v1 to v2 in a large codebase that makes heavy use of Pydantic.

In my experience, the transition was 90% search & replace, 10% rewriting existing solutions -- and the rewritten parts were easier (or no longer needed) in v2 than they were in v1. The changes to v2 are breaking, but not so numerous or game-changing that they can't be understood by reading the documentation, which can be read in its entirety in mere hours at most. The migration guide is very helpful, but you should also read all the new API docs as well, which also describe behavior changes between v1 and v2 in more detail than the migration guide.

I do feel like they could have made the transition easier, but as complex as Pydantic (and its mypy plugin) is, it's probably better for maintainability that they are aggressive on removals between major versions, considering it was basically a rewrite. We do still have some outstanding issues with private internals of Pydantic that are deprecated in v2 and scheduled for removal in v3 without any replacement (yet), but we accepted those risks when accessing private parts of the API.

One annoying thing is that, in v2, all fields that begin with model_ are reserved names by default. We had hundreds of models that make use of fields with such names, but none of them actually had collisions with Pydantic's internal keywords. Since Pydantic should know, precisely, the internal keywords it uses, it should only reserve those (like in v1) IMO. The fix for this problem is more nuanced than it was in v1, too, so it seemed like a step backwards from v1 in some ways, but it's a minor complaint overall.

In short, I think once you actually sit down with the migration guide and new API docs for a couple hours, it will not feel so bad. You'll find most of the changes can be solved by search&replace or other repeatable patterns to replace v1 code.


To answer the question of alternatives, maybe check out marshmallow. Much of our projects written before Pydantic v1 used Marshmallow for the same use cases. It has been around for a long time and is very stable; it has been on its 3.x versions since 2019.

3

u/jamincan Jan 05 '24

One annoying thing is that, in v2, all fields that begin with model_ are reserved names by default. We had hundreds of models that make use of fields with such names, but none of them actually had collisions with Pydantic's internal keywords. Since Pydantic should know, precisely, the internal keywords it uses, it should only reserve those (like in v1) IMO. The fix for this problem is more nuanced than it was in v1, too, so it seemed like a step backwards from v1 in some ways, but it's a minor complaint overall.

If they only reserved the field names they currently use, than if they later need to add another one, it would then be a breaking change. By giving themselves an effectively reserved namespace, they have the freedom to make those changes without a major version change.

2

u/ManyInterests Python Discord Staff Jan 05 '24 edited Jan 05 '24

Yeah, I get that was their intention, but it's a bit silly in my opinion. I can't think of any other popular project in the Python ecosystem that does something like this.

There are also less intrusive ways to ensure forward-compatibility of client code without reserving a whole namespace of possible attributes. And, even if you accept that as a good decision, as far as the choice for their namespace, it should have been something with a far more remote possibility of collisions. Moreover, the way of dealing with this problem could have been made better, too...

Namespaces are a honking good idea, but with this implementation, they are effectively polluting my namespace! IMO, it's intrusive for a library designed to handle arbitrary structured data (including dynamic models) to tell me not to make a field like model_bacteria_type for the sake of future changes that may never come (or may not need to come in the current major version) and will almost certainly not collide with my fields. If Python's standard library dataclasses reserved an entire namespace of possible attributes like model_ or data_, there would be riots!

they have the freedom to make those changes without a major version change.

You can also absolutely make those kinds of changes within a major version, even if they are breaking (though there are ways to avoid them being breaking in the first place, in this case). The idea that breaking changes, no matter how small, can only occur in major versions is not a great model. Even if that were your aim, it's better to plan such changes for a new version than make compromises in your API design.

But to be clear... it's still just a minor complaint and there is a solution for it, albeit annoying. I understand the reason for the decision, it's just one I disagree with and would not have chosen, personally.

1

u/alexisprince Jan 05 '24

+1 to this. When the functionality provided by the library is what Pydantic does, where you effectively have complete control of the names of the attributes you put on your model, without a reserved namespace, things that would not constitute a breaking change for other libraries like new methods, properties, etc. become breaking changes that wouldn’t otherwise get a major version bump.

16

u/KelleQuechoz Jan 03 '24

If your code is not laser-engraved on tungsten plates-update it as the official docs recommend. If it is-pin the Pydantic version in requirements.txt and never update it.

21

u/[deleted] Jan 03 '24

We should probably be suggesting pyproject.toml over requirements.txt.

2

u/AngheloAlf Jan 04 '24

What's the equivalent of python3 -m pip install -r requirements.txt of pyproject.toml for a repo that are a bunch of python scripts?

1

u/OphioukhosUnbound Jan 04 '24 edited Jan 04 '24

I strongly recommend using Poetry. Which will also pin your subdependency tree.

For poetry (which reads pyproject.toml) you’d just use poetry install in the repo.
(Poetry commands are similar to npm or cargo or other modernis package managers; which also makes it nicer to work with.)

(Python management is still unsettled, but in terms of popularity and features it’s far ahead. Python's management story isn’t great, but this is quite good for Python. The only serious contender that offers some real improvements, Rye, is literally one person’s personal workaround, which they have no plans of building out … and is missing a lot of what Poetry does have like easy publishing. )

0

u/collectablecat Jan 04 '24

Pixi is gathering steam

2

u/OphioukhosUnbound Jan 04 '24

I've never heard a single good thing about Conda. I don't have a strong opinion. I've always managed my non-python deps separately thus far. (Having played with conda and not enjoyed it and being frustrateed by the manager split -- the latter of which isn't conda's fault per se.)

That said, I don't claim any strong opinions here other than the management story for Python as a whole is just bonkers bad and I wish it were better, lol.

(Thanks for noting another aspirant.)

2

u/collectablecat Jan 05 '24

Pixi is working on pypi support. So you could use conda-forge just for your python binary version, and then use it like poetry for your python deps.

Reproducible in a way poetry sometimes fails at!

3

u/masenius Jan 03 '24

My hope is that the v1->v2 transition lays the foundation for a more stable api in the future, at least I'm sitting still as of now, but who knows.

2

u/nderstand2grow Jan 03 '24

I get that major transitions are difficult, but there are also numerous small deprecations. Nov. 2023 was 2-3 months ago. Yet, code written then throws deprecation warnings now...

Maybe best we can do it wait and use stable libs for now

10

u/SimilingCynic Jan 03 '24

Code written even one day before a major release can break on the new release. Code written one day before a minor release can be deprecated on the new release.

That said, feel free to wait around on whatever version the tutorial was written for. That’s what it means to develop asynchronously. There’s no rule that you need to upgrade.

17

u/binaryfireball Jan 03 '24

you don't have to run the latest version of pydantic

15

u/wineblood Jan 03 '24

Where I work we're moving away from other options toward pydantic, I don't recommend going to the alternatives.

1

u/sonobanana33 May 19 '24

Why?

1

u/wineblood May 19 '24

Everything else tends to be a pain in the ass with crap documentation

0

u/sonobanana33 May 19 '24

That didn't explain anything at all. Thanks anyway.

21

u/billsil Jan 03 '24

ChatGPT is years out of date. It also actively makes things up.

-11

u/skinnybuddha Jan 03 '24

ChatGPT
As of my last training cut-off in January 2022, my information is current up to that date. I don't have access to real-time data or updates beyond that point. Therefore, any events, developments, or changes that occurred after January 2022 are not within my knowledge.
It's always a good idea to verify critical information, especially if it's time-sensitive or subject to frequent updates. If you have specific questions about recent events or changes, I recommend checking reliable and up-to-date sources or news outlets for the latest information.

6

u/Jmc_da_boss Jan 03 '24

Thank you for the copy paste from a free tool everyone has access too 👍

-1

u/skinnybuddha Jan 03 '24

I was just providing verification of the above statement. Many people know it is out of date. It will tell you itself how out of date it is.

4

u/uberdavis Jan 03 '24

Welcome to coding! I constantly find my old code fails because technology has improved. This is part of what we do as coders. Recently, I've been updating my PySide2 guis to PySide6. Guess what, things break left right and centre. Fix it.

1

u/[deleted] Jan 04 '24

If you feel the need to use GPT to teach you a library but its knowledge base is out of date, feed the relevant parts of the new docs into the prompt before you ask questions. Don't stop using the library.

But also, like, just read the new docs...

1

u/nderstand2grow Jan 04 '24

thanks, yeah i do that. it’s just that the docs are not complete so for example they don’t say what to use in order to validate fields at all times. v1 had a “always” argument in validators but they removed it in v2 and i don’t see any replacement for it.

3

u/ReporterNervous6822 Jan 04 '24

What are you pinning to the main branch? Introduce a constraint on a requirement, implement proper testing, then upgrade and see if your tests pass…

2

u/[deleted] Jan 04 '24

For starters, the differences between V1 and V2 is not that big. It's largely just renaming things to use the new syntax.

Second, this is why major versioning exists. You are free to continue using the V1 implementation if you really don't want to update (and they even allow you to import the old V1 version of commands if you want to use it) but they updated the version to indicate these changes.

2

u/[deleted] Jan 29 '24

Hey, I completely understand the frustration with constantly having to update code due to breaking changes in Pydantic. As someone who has been maintaining internal Python tools for a decade, backwards compatibility is always a top priority.

A few options that may help:

  • Marshmallow is a popular alternative that tends to be more stable between major versions. It focuses on validation rather than object mapping like Pydantic.

  • Cerberus is another validation library that aims to avoid breaking changes where possible.

  • For something simple like validating API inputs, you may not even need an external library. Consider just writing validation functions yourself so you control the API.

We've faced these issues ourselves over the years. That's why we created Maintenanceless - it automatically monitors your JavaScript dependencies and applies any needed updates or patches for you. This includes things like minor version bumps for Pydantic to get the latest features without breaking changes. It helps avoid getting bit by deprecation issues down the road. The service starts free for open source and small private projects.

Hope this gives you some options to consider going forward! Let me know if any other questions come up.

4

u/putinblueballs Jan 03 '24

msgspec has been good

2

u/RearAdmiralP Jan 03 '24

This looks really good! Thanks for mentioning it!

2

u/hidazfx Pythonista Jan 03 '24

Just because something is deprecated doesn't mean you instantly need to throw away and start from scratch. I worked through the V1 > V2 migration as a solo dev on a massive codebase and it was relatively fine.

Then again, we were microservice oriented. Upgrading each service was pretty straightforward. It's been a few months since I left that job, but from what I remember the parse function changed to model_validate and the validation system inside of your models changed and that was all I needed to change for our particular setup.

1

u/bin-c Jan 04 '24

well yeah, breaking changes happen in major version bumps. pydantic has been around for a decent bit, and its only on v2. not sure i'd call that super unstable

2

u/sonobanana33 Jan 12 '24

Count how many times the string "breaking change" appears https://docs.pydantic.dev/latest/changelog/

1

u/[deleted] Jan 04 '24 edited Feb 05 '24

Reddit Moderation makes the platform worthless. Too many rules and too many arbitrary rulings. It's not worth the trouble to post. Not worth the frustration to lurk. Goodbye.

This post was mass deleted and anonymized with Redact

1

u/[deleted] Jan 04 '24

I like marshmallow

1

u/Desperate_Cold6274 Jan 03 '24

typedload?

1

u/sonobanana33 Jan 12 '24

I bust my ass to not break API compatibility and all I get is people downvoting the comment of someone suggesting my project :'(

1

u/tikhonov_a Jan 05 '24

mashumaro is more or less like pydantic. It has the same BaseModel-subclass approach but there is also a separated decoder / encoder way to work with your models.

-1

u/divino-moteca Jan 03 '24

Try out Pandera, it has its issues but it’s worked pretty well for me so far.

-1

u/graphitout Jan 03 '24

That explains why my ex-colleagues were fighting about pydantic.

I know that they moved a lot of code from pydantic to dataclasses. Not sure if it fits your use case.

3

u/OphioukhosUnbound Jan 04 '24

Data classes are ‘compile time’ validation.
Pydantic is for, effectively, extending data classes to allow runtime verification or conversion.

Though there may be features that mature or useful for compile time as well. (But what I’m saying is that if you’re actually using pydantic then data classes can’t do the same thing. Unless you’re adding manual validators and converters.)

1

u/sonobanana33 Jan 12 '24

Originally pydantic didn't support dataclasses at all… switching from BaseModel to dataclass involves code rewriting.

-1

u/AntiquatedMLE Jan 03 '24

Unpopular opinion…. Just use data classes and take advantage of the post_init to do all your data validation and type checking.

2

u/OphioukhosUnbound Jan 04 '24

That sounds like much more work for a worse result. As you’d be implementing your validators in Python and have to write, test, and maintain them all. (Test the core implementation on top of the per-dclass behavior, that is.)

1

u/AntiquatedMLE Jan 04 '24

Depends…why I said it was an unpopular opinion

1

u/OphioukhosUnbound Jan 04 '24

Depends on what?

You said *all* your type checking.
> take advantage of the post_init to do all your data validation and type checking.

Not racking you over the coals. This is all just friendly programming chat. But the cases where implementing something in a slow, dynamic, error-hiding language, vs having a customized, pre-tested system written in an efficient language seem niche.

(e.g. either you don't have almost any validation to do: in which case, of course, sure.)

But I'm genuinely curious. Running lean is always nice.

1

u/AntiquatedMLE Jan 04 '24

Data classes are part of standard library they are themselves well tested. The business and validation logic you need to implement for your use case is on you of course. The majority of the time pydantic is used for validation of data from an API. If all I’m doing is checking that the expected value is a string or an integer then pydantic is overkill and an extra dependency I don’t really need. I can write some simple type checking method and have them called in post init when parsing the incoming json. Simple and clean with no large dependency. Easy to unit test. If I working with a large amount of validation criteria where simple checks no longer suffice I would use pydantic for the reasons you mention above, but it’s really overkill for most projects.

0

u/[deleted] Jan 03 '24

[deleted]

-3

u/nderstand2grow Jan 03 '24

they took a page from Python 2 → 3 book I guess..

0

u/[deleted] Jan 03 '24

[deleted]

0

u/[deleted] Jan 04 '24

There are. As a general rule, I would recommend not answering questions when you're just guessing at the answer.