Skip to main content

How to Hide a Password or API Key in Pydantic with SecretStr

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A password or API key in a Pydantic model shows up in full whenever the model is printed or logged. Typing the field as SecretStr replaces it with asterisks everywhere it's displayed, and hands out the real value only when you ask for it by name. This guide shows both, with real output from Pydantic 2.14.4.

Syntax​

Syntax
from pydantic import BaseModel, SecretStr


class <Model>(BaseModel):
<field>: SecretStr


<value> = <instance>.<field>.get_secret_value()
PartWhat it means
<field>: SecretStrStores the text, but displays it as **********
.get_secret_value()Returns the real text, for the one place that needs it

Pydantic's API documentation describes SecretStr as "A string used for storing sensitive information that you do not want to be visible in logging or tracebacks. When the secret value is nonempty, it is displayed as '**********' instead of the underlying value in calls to repr() and str()."

Prerequisites​

  • Pydantic 2 in a uv project. This guide uses Pydantic 2.14.4, added with uv add pydantic, on Python 3.15.2.

Step 1: Write the Script​

secret.py
from pydantic import BaseModel, SecretStr


class Account(BaseModel):
username: str
password: SecretStr


account = Account(username="sam", password="correct-horse")

print(account)
print(account.password)
print(account.password.get_secret_value())

The secret.py script in a macOS-style code window: an Account model with a username string and a SecretStr password, created with the password correct-horse, then printed three ways.

The password is passed as a plain string; Pydantic wraps it in a SecretStr.

Step 2: Run It​

Terminal
uv run python secret.py
Output
username='sam' password=SecretStr('**********')
**********
correct-horse

The output in a terminal: the model prints with password=SecretStr('**********'), the field alone prints **********, and get_secret_value() prints correct-horse.

Printing the model or the field shows only asterisks, so a log line or an error message can't leak the password. get_secret_value() returned the real text. The same masking applies when the model is turned into data: account.model_dump() gives {'username': 'sam', 'password': SecretStr('**********')} and account.model_dump_json() gives {"username":"sam","password":"**********"}.

Common Mistakes​

MistakeWhat happensDo this instead
Comparing the field with a string, as in account.password == "correct-horse"False: a SecretStr never equals a plain strCompare account.password.get_secret_value(), which gives True
Saving model_dump_json() and expecting the password in itThe JSON holds "**********"Write the secret where it belongs, such as an environment variable, not in a dump
Calling get_secret_value() early and passing the plain string aroundThe value can end up in logs againCall it at the last moment, where the value is used

Troubleshooting​

SymptomLikely causeFix
A library says the key is invalid, and it looks like **********The SecretStr was passed instead of its valuePass field.get_secret_value()

FAQ​

Is the value encrypted?​

No. It's only hidden from display; it's still plain text in memory. SecretStr protects against accidental printing, not against someone reading the program's memory.

Can other types be secret?​

Yes. SecretBytes does the same for bytes, and the generic Secret[...] wraps other types, such as Secret[int].

Conclusion​

The SecretStr password printed as ********** in the model, on its own and in JSON, and get_secret_value() returned correct-horse for the place that needed it.

Additional Resources​