django-msgspec is a new package offering drop-in replacements for Django and Django REST Framework components backed by msgspec, a C-based JSON serialization library that is several times faster than Python's standard library. It provides faster versions of JsonResponse, test client classes, the json_script template tag, session serializer, dumpdata/loaddata serializers, and DRF renderer/parser — most activatable via settings alone with no code changes. The author explains why he chose msgspec over orjson (which he packaged three weeks earlier): orjson has issues with non-string dict keys, 64-bit integer limits, a closed issue tracker, no sub-interpreter support, no free-threading wheels, and trust concerns around maintainer anonymity. msgspec addresses all these points while matching orjson's performance. Known incompatibilities with the standard library include encoding non-finite floats as null and rejecting None/bool dict keys. Future plans include integrating msgspec's typed Struct class for single-pass decode-and-validate workflows.
Table of contents
FeaturesDéjà vu?msgspec’s advantagesmsgspec has standard library incompatibilities tooWhat might come next in django-msgspecFinQuestions this post answers
What are the main limitations of orjson compared to the Python standard library json module?
orjson requires string dictionary keys (raising TypeError for integer keys unless OPT_NON_STR_KEYS is set), caps integers at 64 bits with no opt-out, has no open issue tracker, does not support Python sub-interpreters at all, publishes no free-threading wheels for Python 3.14, and is maintained pseudonymously with no public engagement channel. Developers choosing between JSON libraries for Django projects track trade-offs like these on daily.dev.
How do I replace Django's default JSON serializer and DRF renderer with msgspec for better performance?
Install django-msgspec and update settings: set SESSION_SERIALIZER to 'django_msgspec.sessions.JSONSerializer', add 'json' and 'jsonl' entries to SERIALIZATION_MODULES pointing to django_msgspec serializers, and set DEFAULT_RENDERER_CLASSES and DEFAULT_PARSER_CLASSES in REST_FRAMEWORK to django_msgspec.rest_framework.JSONRenderer and JSONParser. No application code changes are required. Python developers rolling out performance upgrades like this across Django projects find the latest ecosystem moves on daily.dev.
How does msgspec handle non-finite floats like inf and NaN compared to Python's json module?
msgspec encodes non-finite floats such as float('inf') and float('nan') as null rather than the non-standard Infinity and NaN tokens that Python's standard library emits. This is arguably more correct since Infinity is not valid JSON and many parsers reject it, but it is a breaking change if your code round-trips those values. Teams migrating serialization libraries and watching for silent data changes like this rely on daily.dev to stay ahead.
23.3K Impressions2 Comments