{"id":5392,"library":"pydantic-collections","title":"Pydantic Collections","description":"Pydantic-collections provides the `BaseCollectionModel` class, which allows for the manipulation and validation of collections of Pydantic models. It supports various Pydantic-compatible types within its collections. The library is actively maintained, with releases often corresponding to updates in the Pydantic ecosystem, and the current version is 0.6.0.","status":"active","version":"0.6.0","language":"en","source_language":"en","source_url":"https://github.com/romis2012/pydantic-collections","tags":["pydantic","collections","data validation","data structures","model"],"install":[{"cmd":"pip install pydantic-collections","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core functionality for defining and validating data models within collections. Requires `>=1.8.2,<3.0`.","package":"pydantic","optional":false}],"imports":[{"note":"The primary class for creating Pydantic-validated collections.","symbol":"BaseCollectionModel","correct":"from pydantic_collections import BaseCollectionModel"}],"quickstart":{"code":"from datetime import datetime\nfrom pydantic import BaseModel\nfrom pydantic_collections import BaseCollectionModel\n\nclass User(BaseModel):\n    id: int\n    name: str\n    birth_date: datetime\n\nclass UserCollection(BaseCollectionModel[User]):\n    pass\n\nuser_data = [\n    {'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'},\n    {'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'},\n]\nusers = UserCollection(user_data)\n\nprint(users)\n# For Pydantic v2.x, use model_dump() and model_dump_json()\nprint(users.model_dump())\nprint(users.model_dump_json())","lang":"python","description":"This example demonstrates defining a Pydantic model (`User`) and then using `BaseCollectionModel` to create a validated collection of `User` objects. It shows instantiation and serialization for Pydantic V2."},"warnings":[{"fix":"Upgrade `pydantic-collections` to `0.5.0+` when migrating to Pydantic V2. Review Pydantic's official migration guide for V1 to V2 changes and update method calls and configurations accordingly.","message":"Version `0.5.0` of `pydantic-collections` introduced support for Pydantic V2. Projects upgrading Pydantic from V1 to V2 must also upgrade `pydantic-collections` to `v0.5.0` or newer. This also implies adapting to Pydantic V2's own breaking changes, such as method renames (`.dict()` to `.model_dump()`, `.json()` to `.model_dump_json()`) and configuration changes (e.g., `Config` inner class to `model_config` attribute).","severity":"breaking","affected_versions":"v0.5.0 and later (for compatibility with Pydantic V2)"},{"fix":"Ensure that items assigned to or appended to a `BaseCollectionModel` instance are pre-validated instances of the expected Pydantic model. If non-strict behavior is desired, consult the library's documentation on how to configure this (e.g., via Pydantic model config).","message":"By default, `BaseCollectionModel` enforces strict assignment validation. Attempting to append or assign raw dictionaries or objects that are not already instances of the collection's base model will raise a `ValidationError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid overly restrictive `pydantic` version pinning. If you need to use Pydantic V1 features with a V2-compatible `pydantic-collections` version, import from `pydantic.v1` where necessary (available in Pydantic >=1.10.17).","message":"The `pydantic-collections` library declares a dependency on `pydantic>=1.8.2,<3.0`. Be cautious when pinning `pydantic` in your project, especially to `<2`, as this can cause conflicts with `pydantic-collections` versions expecting V2 features or if `pydantic-collections` itself updates to explicitly require Pydantic V2+ in a future major version.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}