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 Django's DATA_UPLOAD_MAX_MEMORY_SIZE (2.5 MiB by default) with a 413, reading at most one byte past the limit. Django itself only applies this setting to request.body, which DRF never reads, so JSON bodies are not capped unless you add a limit of your own. toon-format is pure Python, so an uncapped TOON body costs far more CPU and memory than the same JSON; a 4 MB document of empty list items takes about a second and 250 MB to parse. Set the limit to what your endpoints need and also cap bodies in your reverse proxy. None disables the check.
  • 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.3.2

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.3.2
File Size Uploaded Provenance Actions
drf_toon-0.3.2.tar.gz 4.7 kB Without provenance Details for drf_toon-0.3.2.tar.gz

Built distribution (wheel)

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

Total release size: 9.9 kB

Release files / drf_toon-0.3.2.tar.gz

Download URL drf_toon-0.3.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0302998ca2f71402b1dd8c585edb50f16bd9ccadb547299717b57b2ee8f0b4f5
BLAKE2b-256 checksum
How to use checksums
385e702cc52f9814c98f52589ccc9669ba3b5e8c2511b44e275ce534001ea1df
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.3.2-py3-none-any.whl

Download URL drf_toon-0.3.2-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9240e02fdaea03a36ab3264a0ebf502073ba701bc7e7b65d5c8cd3a9f9055ae3
BLAKE2b-256 checksum
How to use checksums
3071a249790fb0dfe766587400658bb8fd3fe6acfb73ecc40dfbf5d801af6896
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.3.2

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