How to Hide a Password or API Key in Pydantic with SecretStr
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
from pydantic import BaseModel, SecretStr
class <Model>(BaseModel):
<field>: SecretStr
<value> = <instance>.<field>.get_secret_value()
| Part | What it means |
|---|---|
<field>: SecretStr | Stores 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
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 password is passed as a plain string; Pydantic wraps it in a SecretStr.
Step 2: Run It
uv run python secret.py
username='sam' password=SecretStr('**********')
**********
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
| Mistake | What happens | Do this instead |
|---|---|---|
Comparing the field with a string, as in account.password == "correct-horse" | False: a SecretStr never equals a plain str | Compare account.password.get_secret_value(), which gives True |
Saving model_dump_json() and expecting the password in it | The 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 around | The value can end up in logs again | Call it at the last moment, where the value is used |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A library says the key is invalid, and it looks like ********** | The SecretStr was passed instead of its value | Pass 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
- pydantic.types.SecretStr — secret types and
get_secret_value().
