Hashing even a short password with passlib fails after bcrypt 5.0 is installed
Your error
ValueError: password cannot be longer than 72 bytes, truncate manually if necessary (e.g. my_password[:72])2 more lines it can showFix it fast
Most common cause, 1 of 3bcrypt 5 refuses the long test string passlib hashes at startup
Pin bcrypt below 5 in your requirements so every environment resolves the same version.
# requirements.txt
passlib==1.7.4
bcrypt==4.3.0
pip install -r requirements.txtShort answer
bcrypt 5.0.0 raises ValueError for any input over 72 bytes instead of truncating it. passlib 1.7.4, last released in 2020, hashes a long test string when it loads the bcrypt backend, so the first hash call fails even for short passwords, often only on a fresh deploy. Pin bcrypt to 4.3.0, or call the bcrypt library directly; existing hashes keep working.
Which cause do you have?
Print the installed versions in the environment that fails. A deploy that resolves bcrypt freshly often gets 5.x while your laptop still has 4.x.
python -m pip show passlib bcrypt | grep -E "^(Name|Version)"Then find the line you see in this list and open its fix:
- 1bcrypt 5 refuses the long test string passlib hashes at startup
The traceback goes through passlib/handlers/bcrypt.py into _bcrypt.hashpw, and the password you passed is short - 2passlib itself is no longer maintained
PyPI shows passlib 1.7.4, released 8 October 2020, as the latest version - 3Real user passwords can exceed 72 bytes
After removing passlib, the same ValueError appears for some users only, usually with long passphrases or many multi-byte characters
What does the error look like?
ValueError: password cannot be longer than 72 bytes, truncate manually if necessary (e.g. my_password[:72])(trapped) error reading bcrypt versionAttributeError: module 'bcrypt' has no attribute '__about__'
Cause 1 of 3
bcrypt 5 refuses the long test string passlib hashes at startup
You seeThe traceback goes through passlib/handlers/bcrypt.py into _bcrypt.hashpw, and the password you passed is short
Check
The bcrypt changelog for 5.0.0 says hashpw now raises ValueError above 72 bytes where it used to truncate silently. passlib's backend loader still assumes the old behaviour, so the error comes from passlib's own check, not from your input.
python -c "from passlib.context import CryptContext; print(CryptContext(schemes=['bcrypt']).hash('pw')[:4])"Fix
Pin bcrypt below 5 in your requirements so every environment resolves the same version.
# requirements.txt
passlib==1.7.4
bcrypt==4.3.0
pip install -r requirements.txtVerify
The same one-liner prints $2b$ and verify() returns True. The '(trapped) error reading bcrypt version' lines may still print; they are logged by passlib and do not stop hashing.
python -c "from passlib.context import CryptContext; c=CryptContext(schemes=['bcrypt']); h=c.hash('pw'); print(c.verify('pw', h))"Confirmed by raw.githubusercontent.com
Cause 2 of 3
passlib itself is no longer maintained
You seePyPI shows passlib 1.7.4, released 8 October 2020, as the latest version
Check
Pinning bcrypt keeps an old dependency alive. If passlib is only used for bcrypt, the bcrypt package covers hashing and checking on its own, and the hashes passlib stored use the standard $2b$ format that bcrypt reads.
grep -rn "passlib" --include=*.py . | grep -v venvFix
Replace the passlib calls with two small functions. Existing stored hashes verify without any migration.
import bcrypt
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
def verify_password(password: str, hashed: str) -> bool:
return bcrypt.checkpw(password.encode(), hashed.encode())Verify
Log in with an account created before the change; checkpw accepts its old passlib hash.
python -c "import bcrypt; print(bcrypt.checkpw(b'pw', b'<an existing hash from your users table>'))"Confirmed by pypi.org
Cause 3 of 3
Real user passwords can exceed 72 bytes
You seeAfter removing passlib, the same ValueError appears for some users only, usually with long passphrases or many multi-byte characters
Check
72 is a byte count, not a character count, so a 30-character password in a non-Latin script can pass it. bcrypt 5 no longer truncates for you.
python -c "print(len('pässwörd'.encode()))"Fix
Follow the bcrypt README: pre-hash with SHA-256 and base64-encode the digest before bcrypt, or enforce a maximum byte length at sign-up. Pre-hashing changes the stored format, so use it for new hashes and keep checkpw for old ones until users log in again.
import base64, hashlib, bcrypt
def prehash(password: str) -> bytes:
return base64.b64encode(hashlib.sha256(password.encode()).digest())
hashed = bcrypt.hashpw(prehash(password), bcrypt.gensalt())Verify
A 200-character password hashes and verifies with the same prehash function.
python -c "import base64,hashlib,bcrypt; p=base64.b64encode(hashlib.sha256(('x'*200).encode()).digest()); print(bcrypt.checkpw(p, bcrypt.hashpw(p, bcrypt.gensalt())))"Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pin security libraries to exact versions and update them on purpose, with a test that hashes and verifies a password.
- Lock dependencies (pip-tools, uv or Poetry lock files) so production installs the versions you tested.
- Keep password hashing in one module so a library swap touches one file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.