Skip to main content

How to Generate a UUID in Python with uuid.uuid4

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import uuid

<id> = uuid.uuid4()
<text> = str(<id>)
PartWhat it does
import uuidLoads 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:

main.py, which imports uuid, stores uuid.uuid4() in token_id, prints it, its type, its version and the length of its text, then prints a second uuid.uuid4().

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​

Terminal
uv run python main.py
Output
abb7053f-6b55-48b6-80cb-6da04826231d
<class 'uuid.UUID'>
4
36
6bd546e4-67a3-4e1c-99cc-5d401351d486

A zsh terminal where uv run python main.py prints a UUID, class uuid.UUID, the version 4, the length 36 and a second, different UUID.

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ, because they're random.

What Happened​

  • print showed the UUID as text, but type() shows it's a uuid.UUID object. print calls str() on it for you.
  • .version is 4, 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​

MistakeWhat happensDo this instead
Passing the UUID object where text is expectedLibraries that write JSON can't serialize itPass str(uuid.uuid4())
Using uuid.uuid1() for IDs that must not be guessedThe documentation says it builds a UUID "from a host ID, sequence number, and the current time"Use uuid.uuid4()
Naming the variable uuidIt 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​

SymptomLikely causeFix
NameError: name 'uuid' is not defined. Did you forget to import 'uuid'?The module wasn't importedAdd import uuid at the top
TypeError: Object of type UUID is not JSON serializableA UUID object went to json.dumps or a library that writes JSONConvert 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​