Live data from Hacker News

Loading Pydantic models from JSON without running out of memory

pythonspeed.com

11–20 of 51 posts

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

#11
post #9

Or just dump pydantic and use msgspec instead: https://jcristharif.com/msgspec/

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.

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

#12
post #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?

You probably want to use Discriminated Unions https://docs.pydantic.dev/latest/concepts/unions/#discrimina...

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

#13
post #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?

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?

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

#14
post #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 t…

Also some of the introspection stops working e.g. vars().

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

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

#15
post #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?

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.

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

#18
post #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 t…

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.

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

#19
post #16

Maybe using mmap would also save some memory, I'm not quite sure if this can be implemented in Python.

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.

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

#20
post #11
post #9

Or just dump pydantic and use msgspec instead: https://jcristharif.com/msgspec/

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.

One approach to do that in msgspec is described here https://github.com/jcrist/msgspec/issues/375#issuecomment-15...
Post reply on HN