#314: Sortable GUIDs With UUID V7
One of the new features in Python 3.14 is the support for version 7 of UUID. This means we can get a built-in support for collision free and sortable unique identifiers without the need for additional packages. Let us see how we can use them.
Create version 7 UUIDs
We do not need to install any packages and can directly import the uuid module to access the version 7 function:
We can see that the identifiers we generate have a large part that is similar. In this part is the UNIX timestamp of the creation time encoded. That way we get the sortable keys and keep the randomness – albeit limited.
Version 4 is totally random
As comparison, the generated identifiers for version 4 are random and we have no clue about the order in which the keys were generated:
Support for older versions of Python
There are a few packages on PYPI.org that offer support for version 7 in older Python versions. However, not all packages are correct. The package uuid7 generates the identifiers based on an outdated draft of the specification. While the code on GitHub is fixed, the package was not updated and still has the wrong algorithm in it.
You can try other packages or go to the uuid implementation of Python 3.14 and copy the code into your project. Make a note that this copy can be replaced when you move to Python 3.14 or newer.
Conclusion
If you need identifiers that are random but still offer a way to sort them in order of their creation, then version 7 of UUID is a great help. You get all the benefits of UUIDs and only need to change the function you call from uuid4() to uuid7().