PyJWT decode fails with ValueError: Could not deserialize key data
ValueError: Could not deserialize key data.Short answer
jwt.decode hands your key to the cryptography library, which must read it as PEM or SSH. The error means it could not. The usual reasons: you passed bare base64 or a JWK instead of a PEM key, you passed a certificate, or you used the removed verify=False argument.
Fix it fast
1Find out which cause you have
Print the first line of the key you pass in. A real PEM key starts with a BEGIN line.
python - <<'PY' key = open("public.pem").read() print(repr(key[:40])) PY2Match what you see, then jump to its fix
- 1
The key string has no -----BEGIN line, or it is a JSON JWK, or a single line of base64 copied from an identity provider's admin pageThe key is not in PEM (or SSH) formatFix - 2
The text starts with -----BEGIN CERTIFICATE-----A certificate was passed where a key is expectedFix - 3
The call is jwt.decode(token, key, verify=False) and the key is empty or a placeholderOld code uses verify=False, which PyJWT 2 removedFix
- 1
What does the error look like?
ValueError: Could not deserialize key data.jwt.exceptions.InvalidKeyError
Cause 1 of 3
The key is not in PEM (or SSH) format
1Check
PyJWT documents that an RS256 key is expected as an RSA public or private key in PEM or SSH format. A bare base64 string is neither.
head -c 60 public.pem2Fix
Wrap the base64 text in PEM header and footer lines with a line break after each, or skip manual keys and let PyJWKClient fetch the matching key from the provider's JWKS endpoint.
import jwt
from jwt import PyJWKClient
jwks = PyJWKClient("https://idp.example.com/realms/demo/protocol/openid-connect/certs")
signing_key = jwks.get_signing_key_from_jwt(token)
data = jwt.decode(token, signing_key, algorithms=["RS256"], audience="my-api")3Verify
The call returns the claims dict and raises no ValueError.
python decode_test.pyConfirmed by pyjwt.readthedocs.io
Cause 2 of 3
A certificate was passed where a key is expected
1Check
The cryptography docs say a BEGIN CERTIFICATE block is an X.509 certificate, not a key, so the key loaders cannot read it.
head -n 1 public.pem2Fix
Do not pass the certificate to jwt.decode. Use the provider's JWKS endpoint with PyJWKClient as shown above, or pass the public key in PEM form.
signing_key = PyJWKClient(jwks_url).get_signing_key_from_jwt(token)3Verify
Decode a fresh token and confirm the claims come back.
python decode_test.pyConfirmed by cryptography.io
Cause 3 of 3
Old code uses verify=False, which PyJWT 2 removed
1Check
PyJWT 2.0 dropped the verify argument, so the placeholder key is now read as a real key and fails to load.
grep -rn "verify=False" .2Fix
To read the claims without checking the signature (for inspection only, never for trust decisions), use options instead.
claims = jwt.decode(token, options={"verify_signature": False}, algorithms=["RS256"])3Verify
The claims print without an error.
python -c "import jwt,sys; print(jwt.decode(sys.argv[1], options={'verify_signature': False}, algorithms=['RS256']))" "$TOKEN"Confirmed by github.com
How do I stop it coming back?
- Load keys from the provider's JWKS URL with PyJWKClient instead of pasting them into code.
- Always pass algorithms=[...] with a fixed list to jwt.decode.
- Never use verify_signature False for anything that grants access.
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.
Related fixes
Security
Paramiko fails now and then with Error reading SSH protocol banner
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Paramiko opens the TCP connection, then waits a short time for the server to send its SSH banner.
2 causes, each with a fixSecurity
Paramiko key login fails after agreeing on rsa-sha2-512
paramiko.ssh_exception.AuthenticationException: Authentication failed.
Since Paramiko 2.9 an RSA key is signed with rsa-sha2-512 or rsa-sha2-256 instead of SHA-1.
2 causes, each with a fixSecurity
PyJWT will not decode a Keycloak or RS256 token: algorithm errors
jwt.exceptions.MissingCryptographyError
RS256 and ES256 need the cryptography package, and jwt.decode only accepts the algorithms you list.
4 causes, each with a fixSecurity
Python rejects a self-signed certificate in the chain
certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)
Python could not build a trusted path from the server certificate to a root it knows.
3 causes, each with a fixSecurity
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fix