Skip to main content

drf-toon

Native TOON (Token Oriented Object Notation) parser and renderer for Django REST Framework.

TOON is a token-efficient serialization format designed for LLM contexts. It can result in 30–60% fewer tokens than JSON.

drf-toon is built on toon-format, the official pure-Python TOON implementation, which follows the interface of the standard json module.

Compatibility

drf-toon supports Django 5.2 LTS on Python 3.10–3.13, matching Django's own support matrix. Every supported Python version is exercised in CI.

Django Python
5.2 LTS 3.10, 3.11, 3.12, 3.13

Support for additional Django releases can be added later by extending the CI matrix.

Installation

pip install drf-toon

Or, with uv:

uv add drf-toon

Usage

Add the parser and renderer to your DRF settings:

# settings.py
REST_FRAMEWORK = {
    "DEFAULT_PARSER_CLASSES": [
        "rest_framework.parsers.JSONParser",
        "drf_toon.parsers.TOONParser",
    ],
    "DEFAULT_RENDERER_CLASSES": [
        "rest_framework.renderers.JSONRenderer",
        "drf_toon.renderers.TOONRenderer",
    ],
}

Or enable them per-view:

from rest_framework.views import APIView
from rest_framework.response import Response

from drf_toon.parsers import TOONParser
from drf_toon.renderers import TOONRenderer


class UserView(APIView):
    parser_classes = [TOONParser]
    renderer_classes = [TOONRenderer]

    def get(self, request):
        return Response({"name": "Alice", "age": 30})

Both classes use the application/toon media type. Send requests with Content-Type: application/toon to parse, and Accept: application/toon (or ?format=toon) to render.

DRF answers Accept: */* with the first renderer, so list JSONRenderer first to keep JSON the default and serve TOON only when it is requested.

The renderer encodes values through DRF's JSON encoder first, so UUID, datetime, date and Decimal values render as they do in JSON. None renders an empty body (like JSONRenderer), and {} renders a single newline, which is the same empty document but keeps DRF from dropping the Content-Type header.

Security notes

  • Request size: TOONParser rejects bodies larger than the lower of DRF_TOON_MAX_BODY_SIZE (1 MiB by default) and Django's DATA_UPLOAD_MAX_MEMORY_SIZE with a 413, reading at most one byte past the limit. Django itself only applies DATA_UPLOAD_MAX_MEMORY_SIZE to request.body, which DRF never reads. toon-format is pure Python and takes far more memory per byte than JSON (a 4 MB document of empty list items needs about 250 MB), so TOON gets its own lower default. Raise DRF_TOON_MAX_BODY_SIZE if you need larger bodies, and also cap bodies in your reverse proxy. None disables a limit.
  • Object count: a tabular header can nest field groups (x[n]{a{a{v}}}), so every short row decodes into one dict per level and a 520 KB body can need over 1 GB. TOONParser rejects documents that decode into more than DRF_TOON_MAX_OBJECTS (100,000 by default) objects with a 400. None disables the limit.
  • Charset: only text charsets are accepted. Compression "charsets" such as zlib or bz2 are rejected with a 400, and invalid bytes also return a 400.
  • Malformed input: documents that toon-format rejects (duplicate keys, out-of-range or oversized numbers, excessive nesting, wrong declared array lengths) return a 400.
  • Rendering: values are encoded through DRF's JSON encoder first, so circular references, non-finite floats and unsupported types raise an error instead of rendering silently.

Performance

toon-format is pure Python, so it is slower than the Rust toons package that versions up to 0.2.0 used. As a rough guide, 10,000 small records take about 90 ms to render and 110 ms to parse. Consider this when serving very large responses.

Development

Install the dev dependencies and run the checks with uv:

uv sync --dev

uv run ruff check .          # lint
uv run ruff format .         # format
uv run ruff format --check . # format check (CI)
uv run mypy                  # type check
uv run pytest                # tests + coverage

Optionally install the git hooks:

uv run pre-commit install --install-hooks

This wires up both a pre-commit hook (autofix + format) and a pre-push hook that blocks the push if the code is not properly linted/formatted.

License

MIT

Metadata

Release files for drf-toon 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-toon 0.4.0
File Size Uploaded Provenance Actions
drf_toon-0.4.0.tar.gz 5.2 kB Without provenance Details for drf_toon-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-toon 0.4.0
File Interpreter ABI Platform Actions
drf_toon-0.4.0-py3-none-any.whl Python 3 none any Details for drf_toon-0.4.0-py3-none-any.whl

Total release size: 10.8 kB

Release files / drf_toon-0.4.0.tar.gz

Download URL drf_toon-0.4.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
06ae02b558dbbd9ff418b49c936f93567e75567db6214d69b2d0186a8f4329f1
BLAKE2b-256 checksum
How to use checksums
74a9a5b867a71b6133dde10239c3bc53e2c54ee861fe88329a234f3574792dff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / drf_toon-0.4.0-py3-none-any.whl

Download URL drf_toon-0.4.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0169d9358772019242f89e9370aaf3121c399f4ecd106cdb6f2af738d1f75860
BLAKE2b-256 checksum
How to use checksums
23745e8c94803679faa16337081ea3f6deb3e1720d95033efedb498b99e93a7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for drf-toon 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page