ryjwt
Fast, strictly typed JWTs for Python, powered by Rust.
- Python
- Rust
- PyO3
- msgspec
- pydantic
A token is a typed boundary
A JWT is untrusted input that arrives on almost every authenticated request. Most
Python libraries verify it and hand back a dict, leaving the application to check
the shape of the claims afterwards. ryjwt verifies the signature, checks the claims,
and decodes the payload into the type the application actually wants, in one step.
uv add 'ryjwt[msgspec]'
Install it from PyPI. ryjwt supports Python 3.12 to 3.15, including the free-threaded 3.14t and 3.15t builds.
import secrets
from datetime import UTC, datetime, timedelta
import msgspec
import ryjwt
class Claims(msgspec.Struct):
sub: str
exp: datetime
key = ryjwt.SecretKey(secrets.token_bytes(32), algorithms=["HS256"])
expires = datetime.now(UTC) + timedelta(minutes=15)
token = key.encode(Claims(sub="user-1", exp=expires))
claims = key.decode(token, into=Claims) # a Claims, with signature and exp checked
Safe by default
Keys accept only the algorithms they are given, and algorithm names are Literal
types, so a typo fails type checking rather than verification. Weak keys, short
secrets, and malformed tokens are rejected. HMAC, RSA, RSA-PSS, ECDSA, and Ed25519
are all supported.
JWKS in both directions
JWKSClient fetches and caches a provider’s keys from a JWKS URL, from sync or async
code, and keeps serving the last good keys through an outage. In the other direction,
ryjwt.jwks([key]) produces the document to publish at /.well-known/jwks.json.
Performance
The cryptography and claim checks run in Rust. On a typical HS256 token, ryjwt is over 26 times faster than PyJWT on one core, and objects are thread-safe, so decoding scales across threads on free-threaded Python.
Read the benchmarks for the complete methodology, numbers, and caveats, and why ryjwt for the motivation behind it and a note on how it was built.