Registry / serialization / pydantic-collections

pydantic-collections

JSON →
library0.6.0pypypi✓ verified 24d ago

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.

pip install pydantic-collections
INSTALL
IMPORT
SIG · PYDANTIC-COLLECTIO
P
pydantic-collections
serializationpythonv0.6.0
Install
3.6s avg
Import
383ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.396s · 27.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.6s · import 0.370s · 27MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BaseCollectionModel
from pydantic_collections import BaseCollectionModel
The primary class for creating Pydantic-validated collections.

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.

from datetime import datetime from pydantic import BaseModel from pydantic_collections import BaseCollectionModel class User(BaseModel): id: int name: str birth_date: datetime class UserCollection(BaseCollectionModel[User]): pass user_data = [ {'id': 1, 'name': 'Bender', 'birth_date': '2010-04-01T12:59:59'}, {'id': 2, 'name': 'Balaganov', 'birth_date': '2020-04-01T12:59:59'}, ] users = UserCollection(user_data) print(users) # For Pydantic v2.x, use model_dump() and model_dump_json() print(users.model_dump()) print(users.model_dump_json())
Debug
Known issues
breakingVersion `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).
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.
affects: v0.5.0 and later (for compatibility with Pydantic V2)
gotchaBy 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`.
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).
affects: All versions
gotchaThe `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.
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).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pydantic_collections'
The `pydantic-collections` package has not been installed, or the Python environment where the code is being run does not have access to the installed package.
fix
Install the library using pip: `pip install pydantic-collections`
pydantic.error_wrappers.ValidationError: 1 validation error for UserCollection __root__ -> 2 instance of User expected (type=type_error.arbitrary_type; expected_arbitrary_type=User)
By default, `BaseCollectionModel` enforces strict assignment validation, meaning you cannot add raw dictionaries or unvalidated objects directly; items must be instances of the defined Pydantic model for the collection.
fix
Ensure that items assigned to or appended to a `BaseCollectionModel` instance are pre-validated instances of the expected Pydantic model. For example, if `UserCollection[User]` is expected, pass `User(...)` objects. To allow non-strict behavior, you would typically need to configure Pydantic's model config (though `pydantic-collections` itself emphasizes strictness by default).
AttributeError: 'UserCollection' object has no attribute 'dict'
This error occurs when migrating from Pydantic V1 to V2, and `pydantic-collections` (version 0.5.0+) has updated its API to align with Pydantic V2. The `.dict()` method from Pydantic V1 has been renamed to `.model_dump()` in Pydantic V2.
fix
Replace `.dict()` with `.model_dump()` and `.json()` with `.model_dump_json()` for `BaseCollectionModel` instances and any contained Pydantic models when using `pydantic-collections` version 0.5.0 or newer with Pydantic V2.
PydanticSchemaGenerationError: Unable to generate pydantic-core schema for collections.abc.Collection[int]. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
Pydantic, and by extension `pydantic-collections`, cannot automatically generate a schema for certain abstract base classes (like `collections.abc.Collection` without specific type arguments) without explicit instruction or allowing arbitrary types.
fix
Either use a concrete collection type (e.g., `list`, `set`) instead of abstract ones, or, if truly necessary, set `model_config = ConfigDict(arbitrary_types_allowed=True)` in your `BaseCollectionModel` definition (from Pydantic v2) or `Config.arbitrary_types_allowed = True` (from Pydantic v1) to allow Pydantic to handle types it can't otherwise validate. Alternatively, consider implementing `__get_pydantic_core_schema__` for complex custom types.
Upgrade
Version history
0.6.0latest on PyPI · released Jul 9, 2024
Audit
Dependencies
pydanticrequiredCore functionality for defining and validating data models within collections. Requires `>=1.8.2,<3.0`.
Agent activity
10 hits · last 30 days
node
8
Resources
pydantic-collections — pip install pydantic-collections · libregistry