Skip to main content

flattype

JSON in, compact key: value text out.
Flat Notation for Python APIs and LLM prompts: about half the size of JSON on lists, and easy for people and models to read.

PyPI Python versions CI No dependencies MIT license

FlatType in 92 seconds: an API response encoded to Flat Notation, typed models, a server answering in Flat Notation or JSON, framework apps, the CLI, a FastAPI app with FlatMiddleware, and the size comparison with JSON
▶ Watch the 92-second intro. The Python part (step 6) is a real FastAPI app.


This is the Python side of FlatType, whose @flattype/* packages cover JavaScript, React, Vue, Angular, Svelte and Solid. Both write exactly the same text, so a FastAPI backend and a TypeScript frontend can talk Flat Notation to each other. A shared set of thousands of test cases checks this on every change.

Install

pip install flattype

Python 3.9+, no dependencies. pip install "flattype[starlette]" adds the optional FlatResponse class.

Quick start

import flattype

flattype.encode({"name": "John", "age": 30, "city": "New York"})
# name: John
# age: 30
# city: New York

flattype.decode("name: John\nage: 30")  # {'name': 'John', 'age': 30}

Lists, the shape of most API responses, become tables. Keys are written once, and nested dicts become dot paths:

flattype.encode({
    "page": 1,
    "users": [
        {"id": 1, "name": "Alice", "address": {"city": "Paris"}},
        {"id": 2, "name": "Bob", "address": {"city": "Delhi"}},
    ],
})
# page: 1
# users[2]{id,name,address.city}:
#   1, Alice, Paris
#   2, Bob, Delhi

flattype.dumps and flattype.loads are aliases, if you prefer the json names.

FastAPI, Starlette and any ASGI app

One line. Clients that ask for text/x-flat get Flat Notation, and everyone else gets the same JSON as before:

from fastapi import FastAPI
from flattype.asgi import FlatMiddleware

app = FastAPI()
app.add_middleware(FlatMiddleware)

@app.get("/users")
def users():
    return {"users": USERS}  # plain FastAPI
$ curl localhost:3000/users
{"users":[{"id":1,"name":"Alice Martin","role":"admin"},{"id":2,"name":"Bob Singh","role":"editor"}]}

$ curl -H "Accept: text/x-flat" localhost:3000/users
users[2]{id,name,role}:
  1, Alice Martin, admin
  2, Bob Singh, editor
  • How it decides: only JSON responses (application/json or +json) are converted, and only when the request's Accept header names text/x-flat and prefers it to JSON. A browser's */* never counts. These are the same rules as @flattype/http.
  • Caching: Vary: Accept is added to every response, so caches keep the two formats apart.
  • Dependencies: none. It's plain ASGI, so it also works with Litestar, Quart or a raw ASGI app.
  • Clients: on the JavaScript side, flatFetch from @flattype/http or the React, Vue, Angular and Svelte hooks send that header for you.

To always answer in Flat Notation from one route, use the response class:

from flattype.starlette import FlatResponse

@app.get("/export", response_class=FlatResponse)
def export():
    return {"users": USERS}

The FastAPI example runs with one command.

Typed models with Pydantic

decode returns plain dicts and lists, so Pydantic validates them like any parsed JSON:

from datetime import datetime
from pydantic import BaseModel, TypeAdapter

class User(BaseModel):
    id: int
    full_name: str
    created_at: datetime

users = TypeAdapter(list[User]).validate_python(flattype.decode(text)["users"])
# [User(id=1, full_name='Alice', created_at=datetime.datetime(2026, 1, 1, 9, 0, tzinfo=TzInfo(0)))]

Claude and other LLMs

You pay for every character you send to a model, and on lists Flat Notation is about half the characters of JSON. flattype.ai has the helpers from @flattype/ai, with the same output:

import anthropic
from flattype.ai import to_prompt, output_instructions, from_model_output

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=2048,
    messages=[{
        "role": "user",
        "content": f"{to_prompt(orders, name='orders')}\n\nWhich customers ordered twice? "
                   + output_instructions({"customers": [{"name": "Alice", "orders": 2}]}),
    }],
)
reply = message.content[0].text
customers = from_model_output(reply)  # or from_model_output(reply, Customer, many=True) with a Pydantic model
  • to_prompt(data): the data as Flat Notation inside a <data> tag, with a one-line legend that explains the format.
  • output_instructions(example): asks the model to answer in Flat Notation, with an example of the shape you expect.
  • from_model_output(reply): finds the data in the reply (a <data> block, a code fence, or plain JSON instead) and skips prose lines around it. It runs in linear time, even on hostile replies.
  • count_tokens(anthropic.Anthropic(), data): compares JSON with Flat Notation using Claude's free token counter.
  • tool_result(data) and claude_tool_result(id, data): tool results for MCP servers and the Claude API.

More

Lenient decoding for text from models or people: decode(text, strict=False) fills in what's missing instead of raising. Add skip_invalid_lines=True to drop lines that aren't Flat Notation.

Streaming: Decoder reads chunks as they arrive, and calls on_row for each table row as soon as its line is complete:

d = flattype.Decoder(on_row=lambda row: print(row.path, row.index, row.value), keep_rows=False)
for chunk in response.iter_text():
    d.write(chunk)
d.end()

Shared objects and cycles: encode(x, refs=True) writes an object that appears more than once only once, then path = first.path everywhere else. decode(text, refs=True) gives back the same objects, not copies. It's off by default on both ends, because a few reference lines can describe a graph that is endless when walked.

team = {"lead": {"id": 1}}
team["members"] = [team["lead"]]
team["lead"]["team"] = team
flattype.encode(team, refs=True)
# lead.id: 1
# lead.team = ~
# members[0] = lead

Other types: encode(x, default=fn) works like json.dumps(default=...). Tuples are written as lists. encode, decode, delimiter="|" / "\t", indent, tabular=False, flatten / unflatten and stats match the JavaScript options.

Same text as JavaScript

Every case in conformance/ is generated from @flattype/core and @flattype/ai, and the Python package must match it exactly. That's 990 encode cases, 3,215 decode cases (1,965 of them errors, compared message, line and column), 150 object graphs, and 425 prompt and model-reply cases. A few things follow from that:

  • Floats are written the way JavaScript writes them, with 1e+21, 1e-7 and 100. So a float with no fractional part (30.0) decodes as the int 30. Floats of 1e16 or more come back as an int with JavaScript's digits: the same number once converted to float, but not exactly equal to it.
  • Ints are exact, however large. JavaScript would round anything past 2⁵³. A literal longer than 4,300 digits is read as a float, so it can't hit Python's slow and limited int() path.
  • Error columns count code points. JavaScript counts UTF-16 units, so the two differ only after an emoji or another character outside the Basic Multilingual Plane.

Performance

It's pure Python. For 1,000 users with a nested address (Python 3.13, Apple M-series):

Size Encode Decode
json (minified, C accelerated) 114 KB 0.5 ms 0.45 ms
flattype 59 KB (−48%) 4.6 ms 3.9 ms

The win is in bytes on the wire and tokens in a prompt, not CPU time. Linear-time tests on hostile input (long digit runs, deep paths, long lines streamed in tiny chunks, model replies full of prose or lookalike tags) run in CI.

MIT © karammm

Metadata

Release files for flattype 1.11.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 flattype 1.11.0
File Size Uploaded
flattype-1.11.0.tar.gz 21.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flattype 1.11.0
File Interpreter ABI Platform
flattype-1.11.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.0 kB

Release files / flattype-1.11.0.tar.gz

Download URL flattype-1.11.0.tar.gz
Size 21.5 kB
Tags Source
SHA-256 checksum
How to use checksums
db74da5e35d6a13b0f33ff1cdf44bfecd44d298cafb9a25726465d4d4bc85308
BLAKE2b-256 checksum
How to use checksums
78a22f0e89c06b07c3e24637285c67f08a9c5c09a86a09eb84fdb3de41b34743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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 / flattype-1.11.0-py3-none-any.whl

Download URL flattype-1.11.0-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2df430f0fa5cc9f4d30ff12bcdcc92cadcc60ca593c2a1304a868a8e329e525a
BLAKE2b-256 checksum
How to use checksums
3e2bcbca895649e0edd018b279cf46d48346c491cbbb22aae0f3d2ac57e98309
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","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 history Release notifications | RSS feed

1.14.0

2 release files

1.13.0

2 release files

1.12.0

2 release files

This release

1.11.0 This release

2 release files

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