frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Loading Pydantic models from JSON without running out of memory

https://pythonspeed.com/articles/pydantic-json-memory/
52•itamarst•3h ago

Comments

thisguy47•2h ago
I'd like to see a comparison of ijson vs just `json.load(f)`. `ujson` would also be interesting to see.
itamarst•2h ago
For my PyCon 2025 talk I did this. Video isn't up yet, but slides are here: https://pythonspeed.com/pycon2025/slides/

The linked-from-original-article ijson article was the inspiration for the talk: https://pythonspeed.com/articles/json-memory-streaming/

fjasdfas•2h ago
So are there downsides to just always setting slots=True on all of my python data types?
itamarst•2h ago
You can't add extra attributes that weren't part of the original dataclass definition:

  >>> from dataclasses import dataclass
  >>> @dataclass
  ... class C: pass
  ... 
  >>> C().x = 1
  >>> @dataclass(slots=True)
  ... class D: pass
  ... 
  >>> D().x = 1
  Traceback (most recent call last):
    File "<python-input-4>", line 1, in <module>
      D().x = 1
      ^^^^^
  AttributeError: 'D' object has no attribute 'x' and no __dict__ for setting new attributes
Most of the time this is not a thing you actually need to do.
masklinn•1h ago
Also some of the introspection stops working e.g. vars().

If you're using dataclasses it's less of an issue because dataclasses.asdict.

monomial•59m ago
I rarely need to dynamically add attributes myself on dataclasses like this but unfortunately this also means things like `@cached_property` won't work because it can't internally cache the method result anywhere.
jmugan•2h ago
My problem isn't running out of memory; it's loading in a complex model where the fields are BaseModels and unions of BaseModels multiple levels deep. It doesn't load it all the way and leaves some of the deeper parts as dictionaries. I need like almost a parser to search the space of different loads. Anyone have any ideas for software that does that?
causasui•1h ago
You probably want to use Discriminated Unions https://docs.pydantic.dev/latest/concepts/unions/#discrimina...
enragedcacti•1h ago
The only reason I can think of for the behavior you are describing is if one of the unioned types at some level of the hierarchy is equivalent to Dict[str, Any]. My understanding is that Pydantic will explore every option provided recursively and raise a ValidationError if none match but will never just give up and hand you a partially validated object.

Are you able to share a snippet that reproduces what you're seeing?

cbcoutinho•1h ago
At some point, we have to admit we're asking too much from our tools.

I know nothing about your context, but in what context would a single model need to support so many permutations of a data structure? Just because software can, doesn't mean it should.

m_ke•2h ago
Or just dump pydantic and use msgspec instead: https://jcristharif.com/msgspec/
itamarst•1h ago
msgspec is much more memory efficient out of the box, yes. Also quite fast.
mbb70•1h ago
A great feature of pydantic are the validation hooks that let you intercept serialization/deserialization of specific fields and augment behavior.

For example if you are querying a DB that returns a column as a JSON string, trivial with Pydantic to json parse the column are part of deser with an annotation.

Pydantic is definitely slower and not a 'zero cost abstraction', but you do get a lot for it.

zxilly•1h ago
Maybe using mmap would also save some memory, I'm not quite sure if this can be implemented in Python.
itamarst•53m ago
Once you switch to ijson it will not save any memory, no, because ijson essentially uses zero memory for the parsing. You're just left with the in-memory representation.
dgan•1h ago
i gave up on python dataclasses & json. Using protobufs object within the application itself. I also have a "...Mixin" class for almost every wire model, with extra methods

Automatic, statically typed deserialization is worth the trouble in my opinion

Subaru Just Increased Prices on Nearly All Its Models

https://www.motor1.com/news/760173/new-subaru-models-price-increases-june/
1•rurp•26s ago•0 comments

Ask HN: iCloud hide my email / Gmail issues

1•lajosbacs•1m ago•0 comments

Claude 4 can whistleblow you [video]

https://www.youtube.com/watch?v=ndIj5FuYPqM
1•amrrs•2m ago•1 comments

Breez and Spark Partner to Bring Native Bitcoin Lightning Payments to Any App

https://www.spark.money/news/breez-spark-partnership
1•janandonly•4m ago•0 comments

Trump administration ends Harvard's ability to enrol international students

https://www.bbc.com/news/articles/c05768jmm11o
2•onemoresoop•9m ago•1 comments

College English majors can't read

https://kittenbeloved.substack.com/p/college-english-majors-cant-read
1•like_any_other•12m ago•0 comments

Trump administration bars Harvard from enrolling international students

https://www.cnn.com/2025/05/22/us/harvard-university-trump-international-students
4•breadwinner•15m ago•1 comments

Fifth-year students find relic of saint in Dublin school

https://www.rte.ie/news/dublin/2025/0522/1514234-dublin-school-relic/
1•austinallegro•18m ago•0 comments

Fast and Free Conversion Tools

https://convertr.online/
1•ailibrarian•20m ago•1 comments

The effectiveness of Large Language Models in the mechanical design domain

https://arxiv.org/abs/2505.01559
1•PaulHoule•22m ago•0 comments

Refactoring My Infrastructure as Code Configurations

https://not-a-number.io/2025/refactoring-my-infrastructure-as-code-configurations/
1•kugurerdem•23m ago•0 comments

SpaceX employees and members, its PAC campaign contributions

https://www.opensecrets.org/orgs/spacex/summary?id=D000029147&topnumcycle=2024
1•srameshc•23m ago•0 comments

AI Can't Even Fix a Simple Bug – But Sure, Let's Fire All Our Engineers

https://nmn.gl/blog/ai-scam
5•namanyayg•23m ago•0 comments

Hermes-PF's 6 CubeSats Watch the Entire Sky for High-Energy Bursts

https://www.universetoday.com/articles/hermes-pfs-6-cubesats-watch-the-entire-sky-for-high-energy-bursts
1•rbanffy•24m ago•0 comments

Quick AI Projects with Code, Beginner-Advanced

https://decodingml.substack.com/p/5-ai-projects-for-people-in-a-hurry
1•rbanffy•24m ago•0 comments

A safety institute advised against releasing early Anthropic Claude Opus 4 model

https://techcrunch.com/2025/05/22/a-safety-institute-advised-against-releasing-an-early-version-of-anthropics-claude-opus-4-ai-model/
1•sylvainkalache•25m ago•0 comments

Business owners are using AI "concerned residents" to fight a proposed bus lane

https://torontolife.com/city/bathurst-business-owners-are-using-ai-generated-concerned-residents-to-fight-a-proposed-bus-lane/
1•amichail•25m ago•0 comments

Claude 4 System Card [pdf]

https://www-cdn.anthropic.com/6be99a52cb68eb70eb9572b4cafad13df32ed995.pdf
1•ossusermivami•27m ago•0 comments

Ask HN: How Do You Review Your Code in the Age of AI?

2•aman_madhukar02•29m ago•0 comments

Food for Soul

https://foodforsoul.it/
1•oriettaxx•29m ago•0 comments

MAGA's assault on science is an act of grievous self-harm

https://www.economist.com/leaders/2025/05/22/magas-assault-on-science-is-an-act-of-grievous-self-harm
4•pseudolus•32m ago•1 comments

"How you design the beep is important." Behind the movement for calmer gadgets

https://arstechnica.com/gadgets/2025/05/what-makes-a-tech-gadget-calm-this-certification-firm-has-an-81-point-checklist/
3•PhotonHunter•33m ago•0 comments

New multi-sensor navigation system for GNSS-denied environments

https://www.gpsworld.com/advanced-navigation-unveils-multi-sensor-navigation-system-for-gnss-denied-environments/
1•sorenjan•34m ago•0 comments

Oblivion Name Generator

https://oblivionnamegenerator.online/
1•taoji•36m ago•1 comments

Supreme Court permits Trump removal of members of the NLRB and the MSPB [pdf]

https://www.supremecourt.gov/opinions/24pdf/24a966_1b8e.pdf
4•boroboro4•36m ago•2 comments

How Two CEOs Mixed Romance and Business, Leading to Scandal

https://www.wsj.com/business/retail/kohls-walmart-michaels-ashley-buchanan-chandra-holt-0c2dc613
1•impish9208•36m ago•1 comments

Show HN: I Built a Basketball and Wordle Game for NBA Nerds

https://baskordle.com/
1•amforever•36m ago•0 comments

Fender Studio

https://www.fender.com/pages/fender-studio
1•jaboutboul•41m ago•0 comments

Against Unnecessary Databases

https://beepb00p.xyz/unnecessary-db.html
1•harporoeder•41m ago•0 comments

Trump Administration Blocks Harvard from Enrolling Foreign Students

https://www.wsj.com/us-news/education/trump-harvard-foreign-international-student-enrollment-00124307
3•boplicity•42m ago•1 comments