Python · uuid Module · Code Reference

Generate a UUID in Python

Complete reference for generating, validating, and formatting UUIDs using Python’s built-in uuid module — covers uuid1, uuid3, uuid4, uuid5, and the new draft uuid7 support.

Quick Start

Generate a Random UUID (v4) in Python

Python’s standard library includes the uuid module — no installation needed. The most common use case is generating a random UUID v4:

Python
import uuid # Generate a random UUID (version 4) my_uuid = uuid.uuid4() print(my_uuid) # 550e8400-e29b-41d4-a716-446655440000 print(str(my_uuid)) # as string print(my_uuid.hex) # no hyphens, 32 chars print(str(my_uuid).upper()) # uppercase
💡

uuid.uuid4() is the right default

For 95% of use cases — API IDs, session tokens, database keys where you don’t need time-ordering — uuid.uuid4() is exactly what you want. It uses os.urandom() internally, which is cryptographically secure.

All Versions

Every UUID Version in Python

VersionPython CallUse Case
v1 (Time-based)uuid.uuid1()Legacy systems needing timestamp + MAC. Leaks machine identity — avoid in user-facing apps.
v3 (MD5 name-based)uuid.uuid3(namespace, name)Deterministic IDs from a name. Use v5 instead (MD5 is broken).
v4 (Random)uuid.uuid4()General purpose. The default choice for most applications.
v5 (SHA-1 name-based)uuid.uuid5(namespace, name)Deterministic IDs from a name — same input always gives same UUID.
v7 (Unix timestamp)Not in stdlib yet — use uuid7 packageTime-ordered UUIDs for database primary keys. See below for install.
Python — Deterministic UUID (v5)
import uuid # Same namespace + name ALWAYS produces the same UUID namespace = uuid.NAMESPACE_DNS my_uuid = uuid.uuid5(namespace, ‘example.com’) print(my_uuid) # cfbff0d1-9375-5685-968c-48ce8b15ae17 (always the same)

UUID v7

UUID v7 in Python (2026 Recommended for DB Keys)

UUID v7 is not yet in Python’s standard library (expected in a future release). For now, install the uuid7 package or use this drop-in implementation:

Terminal
pip install uuid7
Python
from uuid_extensions import uuid7, uuid7str my_uuid = uuid7() print(my_uuid) # time-ordered, sortable UUID # Or as a string directly my_uuid_str = uuid7str()
⚠️

Check your Python version

Native uuid.uuid7() may land in a future CPython release once the RFC 9562 draft is fully adopted into stdlib. Until then, third-party packages are the reliable path.

Validation

Validate a UUID String in Python

To check whether a string is a properly formatted UUID, wrap the constructor in a try/except block:

Python
import uuid def is_valid_uuid(value: str) -> bool: try: uuid.UUID(str(value)) return True except (ValueError, AttributeError, TypeError): return False is_valid_uuid(‘550e8400-e29b-41d4-a716-446655440000’) # True is_valid_uuid(‘not-a-uuid’) # False # Check the version of a parsed UUID u = uuid.UUID(‘550e8400-e29b-41d4-a716-446655440000’) print(u.version) # 4

Frameworks

UUIDs in Django, FastAPI & SQLAlchemy

Django Model Field
import uuid from django.db import models class Order(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
FastAPI + Pydantic
from pydantic import BaseModel from uuid import UUID, uuid4 class Item(BaseModel): id: UUID = uuid4() name: str
SQLAlchemy
import uuid from sqlalchemy import Column from sqlalchemy.dialects.postgresql import UUID class Order(Base): __tablename__ = ‘orders’ id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)

FAQ

Python UUID FAQ

Python’s uuid module is part of the standard library since Python 2.5, so no installation is needed. Just import uuid and call uuid.uuid4() for a random UUID. This works in any standard Python 3 installation with zero external dependencies.
uuid.uuid4() returns a UUID object with useful properties like .hex, .int, .bytes, and .version. str(uuid.uuid4()) converts it to the standard 36-character hyphenated string format. Use the UUID object when you need to inspect version or variant bits, and the string form for storage, display, or JSON serialization.
Yes. Python’s uuid4() uses os.urandom() internally, which draws from the operating system’s cryptographically secure random number source. It is safe to use in contexts requiring unpredictability, though a UUID alone should still not be used as a password or authentication secret.
Python’s standard library does not yet include uuid7(). Install the uuid7 package via pip install uuid7, then use from uuid_extensions import uuid7. This gives you time-ordered, RFC 9562-compliant UUIDs suitable for database primary keys.

Need to Generate a UUID Right Now?

Skip the code — use our free browser-based generator for all 8 UUID versions.

Open UUID Generator