How to Generate a UUID in Python with uuid.uuid4
uuid.uuid4() from Python's standard library returns a new random UUID every time you call it. It returns a UUID object, not a string, and str() turns it into the familiar 36 characters.
Syntax
import uuid
<id> = uuid.uuid4()
<text> = str(<id>)
| Part | What it does |
|---|---|
import uuid | Loads the uuid module, which is part of Python. |
uuid.uuid4() | Makes a new random UUID and returns it as a uuid.UUID object. |
str(<id>) | Turns the UUID into text, such as abb7053f-6b55-48b6-80cb-6da04826231d. |
The Python documentation for uuid.uuid4(): "Generate a random UUID in a cryptographically-secure method according to RFC 9562, §5.4."
The Example
The example makes a UUID and prints it, its type, its version and the length of its text, then makes a second UUID.
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Write the Code
Create a file named main.py:

import uuid
token_id = uuid.uuid4()
print(token_id)
print(type(token_id))
print(token_id.version)
print(len(str(token_id)))
print(uuid.uuid4())
Step 2: Run It
uv run python main.py
abb7053f-6b55-48b6-80cb-6da04826231d
<class 'uuid.UUID'>
4
36
6bd546e4-67a3-4e1c-99cc-5d401351d486

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ, because they're random.
What Happened
printshowed the UUID as text, buttype()shows it's auuid.UUIDobject.printcallsstr()on it for you..versionis4, the random kind. The same digit starts the third group of the UUID.- The text is 36 characters long: 32 hexadecimal digits and four dashes.
- The second call made a different UUID. A version 4 UUID has 122 random bits, so two of them practically never repeat.
Where a library expects a string, for example a JSON Web Token's jti claim, pass str(uuid.uuid4()), not the object.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing the UUID object where text is expected | Libraries that write JSON can't serialize it | Pass str(uuid.uuid4()) |
Using uuid.uuid1() for IDs that must not be guessed | The documentation says it builds a UUID "from a host ID, sequence number, and the current time" | Use uuid.uuid4() |
Naming the variable uuid | It hides the module, and the next uuid.uuid4() fails with AttributeError: 'UUID' object has no attribute 'uuid4' | Use another name, such as token_id |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NameError: name 'uuid' is not defined. Did you forget to import 'uuid'? | The module wasn't imported | Add import uuid at the top |
TypeError: Object of type UUID is not JSON serializable | A UUID object went to json.dumps or a library that writes JSON | Convert it with str() first |
FAQ
Is uuid4 random enough for security?
Yes for IDs that must not be guessed: the documentation says it generates the UUID "in a cryptographically-secure method". Don't use a UUID as a password or a key, though, because anyone who sees it can copy it.
Can Python make time-ordered UUIDs?
Yes, since Python 3.14: uuid.uuid7() makes a version 7 UUID, which starts with the current time, so newer IDs sort after older ones.
Conclusion
uuid.uuid4() makes a random UUID object, and str() turns it into text. Use it wherever you need an ID that practically never repeats.
Additional Resources
- uuid, in the Python documentation.
- RFC 9562 section 5.4, the UUID version 4 format.
