cuid2 is a Python 3 library providing next-generation Globally Unique Identifiers (GUIDs). It's a port of the CUID2 reference implementation, optimized for horizontal scaling and performance. CUID2 IDs are secure, collision-resistant, horizontally scalable, offline-compatible, and URL-friendly, making them ideal for modern distributed systems. The library is actively maintained, with the current version being 2.0.1, and receives regular updates.
pip install cuid2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the two primary ways to generate CUID2 identifiers: using the simple `cuid_wrapper` function for a callable generator, or instantiating the `Cuid` class for more granular control, such as specifying the ID length.
Migrate your code to use the `cuid_wrapper()` function for a simple generator or `Cuid(length=...)` class (note the casing: 'Cuid' with a lowercase 'uid') for customized generation. For example, `from cuid2 import Cuid`.
For new projects, always use `cuid2`. For existing projects using the older `cuid` library, evaluate your security requirements and strongly consider migrating to `cuid2` to benefit from its improved cryptographic security and collision resistance.
Do not rely on CUID2 IDs for sequential ordering or to extract creation timestamps. If time-based sorting is crucial for your database, consider adding a separate timestamp column. If you need strictly sequential, gap-less IDs (e.g., invoice numbers), CUID2 is not suitable; use an auto-incrementing ID or a different ID scheme designed for that purpose.
Run `pip install cuid2` to install the library. Ensure you are running your script in the same Python environment where you installed the package.
Update your import and usage to `from cuid2 import Cuid` (lowercase 'uid') for the class, or `from cuid2 import cuid_wrapper` for a simple callable ID generator.
Instead of `from cuid2 import cuid`, use `from cuid2 import cuid_wrapper` to get a callable ID generator, or `from cuid2 import Cuid` to use the class for more control.