Live data from Hacker News

Loading Pydantic models from JSON without running out of memory

pythonspeed.com

1–10 of 51 posts

Re: Loading Pydantic models from JSON without running out of memory

#6

I'd like to see a comparison of ijson vs just `json.load(f)`. `ujson` would also be interesting to see.

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/

Re: Loading Pydantic models from JSON without running out of memory

#7
post #5

So are there downsides to just always setting slots=True on all of my python data types?

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 "", line 1, in 
      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.

Re: Loading Pydantic models from JSON without running out of memory

#8
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?
Post reply on HN