{"id":2566,"library":"lazy-model","title":"Lazy Model","description":"lazy-model is a Python library that provides a lazy interface for parsing objects into Pydantic models. It defers the parsing and validation of individual fields until they are explicitly accessed, saving the raw data within the object initially. This approach can significantly improve performance and reduce memory consumption, especially for large or complex data structures where not all fields are always needed immediately. The library is currently at version 0.4.0 and appears to have an active, though not rapid, release cadence, with its latest release in August 2025.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/BeanieODM/lazy_model","tags":["pydantic","lazy-loading","data-parsing","performance"],"install":[{"cmd":"pip install lazy-model","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"lazy-model is built on top of Pydantic models and requires it for its core functionality.","package":"pydantic"}],"imports":[{"symbol":"LazyModel","correct":"from lazy_model import LazyModel"}],"quickstart":{"code":"from lazy_model import LazyModel\nfrom pydantic import validator\n\nclass Sample(LazyModel):\n    i: int\n    s: str\n\n    @validator(\"s\")\n    def s_upper(cls, v):\n        return v.upper()\n\n# Create an instance with lazy parsing\nobj = Sample.lazy_parse({\"i\": \"10\", \"s\": \"test\"})\n\n# At this point, 'i' and 's' are stored in a raw format (NAO - Not An Object)\nprint(f\"Initial object dict: {obj.__dict__}\")\n\n# Accessing 's' triggers its parsing and validation\nprint(f\"Accessing s: {obj.s}\") # Output: TEST\nprint(f\"After 's' access: {obj.__dict__}\")\n\n# Accessing 'i' triggers its parsing and validation\nprint(f\"Accessing i: {obj.i}, type: {type(obj.i)}\") # Output: 10, <class 'int'>\nprint(f\"After 'i' access: {obj.__dict__}\") # Both fields are now parsed","lang":"python","description":"This example demonstrates how to define a `LazyModel` and use `lazy_parse` to defer field validation until access. It also shows that Pydantic validators still apply when a field is accessed for the first time."},"warnings":[{"fix":"Be aware that Pydantic's full validation for lazy fields occurs on first access. If strict upfront validation is required for all fields, consider using standard Pydantic model instantiation or explicitly accessing all fields after lazy parsing to trigger their validation.","message":"Fields parsed with `lazy_parse` are not fully validated by Pydantic until they are first accessed. This means initial model instantiation might succeed even if some lazily loaded fields contain invalid data, with validation errors only surfacing upon field access.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always interact with `LazyModel` instances and their fields through their public API and defined properties. Avoid directly manipulating `obj.__dict__` for lazily parsed fields unless you fully understand the implications and the library's internal workings.","message":"The underlying mechanism of `lazy-model` interacts with Pydantic's internal object representation. While the library handles this, direct manual manipulation of `__dict__` for fields intended to be lazy can lead to unexpected behavior or bypass the lazy loading/validation mechanism.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}