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 reads the whole body into memory. Like DRF's own parsers it does not cap the size, so set a body limit in your reverse proxy.
  • 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.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.3.0
File Size Uploaded Provenance Actions
drf_toon-0.3.0.tar.gz 4.2 kB Without provenance Details for drf_toon-0.3.0.tar.gz

Built distribution (wheel)

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

Total release size: 8.8 kB

Release files / drf_toon-0.3.0.tar.gz

Download URL drf_toon-0.3.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
628f507b2181f4f811135f6c44e3202019f3154e834b509db096d309e6b0f43c
BLAKE2b-256 checksum
How to use checksums
56ebebabb879a5b1974c8756a6a6821f6f7aca34611deb2f1ea11829ffac03c7
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.0-py3-none-any.whl

Download URL drf_toon-0.3.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6595c36f304b6fe59e4168f33683971e5dcc675bc2b458a05f0842595e4f9113
BLAKE2b-256 checksum
How to use checksums
79513750303fff3a32c546f6f62442eba71800203ae35a07df33bd7176f1ceab
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.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