Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

PyJWT decode fails with ValueError: Could not deserialize key data

ErrorValueError: 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.

  • Applies to PyJWT 2.x, cryptography 42+, Python 3.13
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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]))
    PY
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The text starts with -----BEGIN CERTIFICATE-----A certificate was passed where a key is expectedFix
    3. 3The call is jwt.decode(token, key, verify=False) and the key is empty or a placeholderOld code uses verify=False, which PyJWT 2 removedFix

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.pem

2Fix

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.py

Confirmed by pyjwt.readthedocs.io

Did this fix it?

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.pem

2Fix

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.py

Confirmed by cryptography.io

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.