flattype 1.11.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flattype/__init__.py +63 -0
- flattype/_decode.py +294 -0
- flattype/_encode.py +187 -0
- flattype/_flatten.py +55 -0
- flattype/_path.py +298 -0
- flattype/_syntax.py +171 -0
- flattype/ai.py +154 -0
- flattype/asgi.py +93 -0
- flattype/negotiate.py +47 -0
- flattype/py.typed +0 -0
- flattype/starlette.py +26 -0
- flattype-1.11.0.dist-info/METADATA +240 -0
- flattype-1.11.0.dist-info/RECORD +15 -0
- flattype-1.11.0.dist-info/WHEEL +4 -0
- flattype-1.11.0.dist-info/licenses/LICENSE +21 -0
flattype/asgi.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""ASGI middleware for FastAPI, Starlette, Litestar, Quart or any ASGI app. No dependencies.
|
|
2
|
+
|
|
3
|
+
from flattype.asgi import FlatMiddleware
|
|
4
|
+
app.add_middleware(FlatMiddleware)
|
|
5
|
+
|
|
6
|
+
JSON responses are re-encoded as Flat Notation when the client asks for ``text/x-flat`` (and prefers it to
|
|
7
|
+
JSON). Everyone else gets the JSON unchanged. ``Vary: Accept`` is always added so caches keep the two apart.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import re
|
|
14
|
+
from collections.abc import Awaitable, MutableMapping
|
|
15
|
+
from typing import Any, Callable
|
|
16
|
+
|
|
17
|
+
from ._encode import encode
|
|
18
|
+
from .negotiate import add_vary, prefers_flat
|
|
19
|
+
|
|
20
|
+
Scope = MutableMapping[str, Any]
|
|
21
|
+
Message = MutableMapping[str, Any]
|
|
22
|
+
Receive = Callable[[], Awaitable[Message]]
|
|
23
|
+
Send = Callable[[Message], Awaitable[None]]
|
|
24
|
+
ASGIApp = Callable[[Scope, Receive, Send], Awaitable[None]]
|
|
25
|
+
|
|
26
|
+
_JSON = re.compile(r"[/+]json\b", re.I)
|
|
27
|
+
CONTENT_TYPE = b"text/x-flat; charset=utf-8"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _header(headers: list[tuple[bytes, bytes]], name: bytes) -> bytes | None:
|
|
31
|
+
for k, v in headers:
|
|
32
|
+
if k.lower() == name:
|
|
33
|
+
return v
|
|
34
|
+
return None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _without(headers: list[tuple[bytes, bytes]], *names: bytes) -> list[tuple[bytes, bytes]]:
|
|
38
|
+
return [(k, v) for k, v in headers if k.lower() not in names]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class FlatMiddleware:
|
|
42
|
+
"""Answers in Flat Notation when the client asks for it. Encode options (``delimiter``, ``indent``, ``tabular``)
|
|
43
|
+
are passed to :func:`flattype.encode`."""
|
|
44
|
+
|
|
45
|
+
def __init__(self, app: ASGIApp, **encode_options: Any) -> None:
|
|
46
|
+
self.app = app
|
|
47
|
+
self.options = encode_options
|
|
48
|
+
|
|
49
|
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
50
|
+
if scope["type"] != "http":
|
|
51
|
+
await self.app(scope, receive, send)
|
|
52
|
+
return
|
|
53
|
+
accept = _header(list(scope.get("headers") or []), b"accept")
|
|
54
|
+
wanted = prefers_flat(accept.decode("latin-1") if accept else None)
|
|
55
|
+
start: Message | None = None
|
|
56
|
+
body: list[bytes] = []
|
|
57
|
+
passthrough = True
|
|
58
|
+
|
|
59
|
+
async def wrapped(message: Message) -> None:
|
|
60
|
+
nonlocal start, passthrough
|
|
61
|
+
if message["type"] == "http.response.start":
|
|
62
|
+
headers = list(message.get("headers") or [])
|
|
63
|
+
vary = _header(headers, b"vary")
|
|
64
|
+
headers = _without(headers, b"vary") + [
|
|
65
|
+
(b"vary", add_vary(vary.decode("latin-1") if vary else None).encode("latin-1"))
|
|
66
|
+
]
|
|
67
|
+
message = {**message, "headers": headers}
|
|
68
|
+
ctype = _header(headers, b"content-type")
|
|
69
|
+
passthrough = not (wanted and ctype is not None and _JSON.search(ctype.decode("latin-1")))
|
|
70
|
+
if passthrough:
|
|
71
|
+
await send(message)
|
|
72
|
+
else:
|
|
73
|
+
start = message
|
|
74
|
+
return
|
|
75
|
+
if message["type"] != "http.response.body" or passthrough:
|
|
76
|
+
await send(message)
|
|
77
|
+
return
|
|
78
|
+
body.append(message.get("body", b""))
|
|
79
|
+
if message.get("more_body", False):
|
|
80
|
+
return
|
|
81
|
+
assert start is not None
|
|
82
|
+
raw = b"".join(body)
|
|
83
|
+
try:
|
|
84
|
+
payload = encode(json.loads(raw), **self.options).encode("utf-8")
|
|
85
|
+
headers = _without(start["headers"], b"content-type", b"content-length")
|
|
86
|
+
headers += [(b"content-type", CONTENT_TYPE), (b"content-length", str(len(payload)).encode())]
|
|
87
|
+
start = {**start, "headers": headers}
|
|
88
|
+
except (ValueError, TypeError, RecursionError):
|
|
89
|
+
payload = raw # not valid JSON after all: send it as it was
|
|
90
|
+
await send(start)
|
|
91
|
+
await send({"type": "http.response.body", "body": payload, "more_body": False})
|
|
92
|
+
|
|
93
|
+
await self.app(scope, receive, wrapped)
|
flattype/negotiate.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Content negotiation, with the same rules as ``@flattype/http``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
MEDIA_TYPE = "text/x-flat"
|
|
6
|
+
#: The ``Accept`` header a client sends: Flat Notation first, JSON as the fallback.
|
|
7
|
+
ACCEPT = f"{MEDIA_TYPE}, application/json;q=0.9, */*;q=0.1"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _quality(accept: str, media_type: str) -> float:
|
|
11
|
+
best = -1.0
|
|
12
|
+
for part in accept.split(","):
|
|
13
|
+
media, *params = part.strip().split(";")
|
|
14
|
+
if media.strip().lower() != media_type:
|
|
15
|
+
continue
|
|
16
|
+
q = 1.0
|
|
17
|
+
for p in params:
|
|
18
|
+
k, _, v = p.partition("=")
|
|
19
|
+
if k.strip() == "q":
|
|
20
|
+
try:
|
|
21
|
+
q = float(v)
|
|
22
|
+
except ValueError:
|
|
23
|
+
q = 0.0
|
|
24
|
+
best = max(best, 0.0 if q != q else q)
|
|
25
|
+
return best
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def prefers_flat(accept: str | None) -> bool:
|
|
29
|
+
"""True when the client asked for Flat Notation explicitly and prefers it to JSON.
|
|
30
|
+
|
|
31
|
+
Wildcards (``*/*``) never count, so ordinary clients keep getting JSON.
|
|
32
|
+
"""
|
|
33
|
+
if not accept:
|
|
34
|
+
return False
|
|
35
|
+
flat = _quality(accept, MEDIA_TYPE)
|
|
36
|
+
return flat > 0 and flat >= _quality(accept, "application/json")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def is_flat_type(content_type: str | None) -> bool:
|
|
40
|
+
return bool(content_type) and MEDIA_TYPE in content_type.lower() # type: ignore[union-attr]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def add_vary(current: str | None) -> str:
|
|
44
|
+
"""Adds ``Accept`` to an existing ``Vary`` header value."""
|
|
45
|
+
if not current:
|
|
46
|
+
return "Accept"
|
|
47
|
+
return current if any(v.strip().lower() == "accept" for v in current.split(",")) else f"{current}, Accept"
|
flattype/py.typed
ADDED
|
File without changes
|
flattype/starlette.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Starlette / FastAPI response class that always answers in Flat Notation. Needs ``pip install flattype[starlette]``.
|
|
2
|
+
|
|
3
|
+
from flattype.starlette import FlatResponse
|
|
4
|
+
|
|
5
|
+
@app.get("/users", response_class=FlatResponse)
|
|
6
|
+
def users():
|
|
7
|
+
return [{"id": 1, "name": "Alice"}]
|
|
8
|
+
|
|
9
|
+
To answer in Flat Notation only when the client asks for it, use :class:`flattype.asgi.FlatMiddleware` instead.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from starlette.responses import Response
|
|
17
|
+
|
|
18
|
+
from ._encode import encode
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class FlatResponse(Response):
|
|
22
|
+
media_type = "text/x-flat"
|
|
23
|
+
charset = "utf-8"
|
|
24
|
+
|
|
25
|
+
def render(self, content: Any) -> bytes:
|
|
26
|
+
return encode(content).encode("utf-8")
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: flattype
|
|
3
|
+
Version: 1.11.0
|
|
4
|
+
Summary: JSON <-> Flat Notation: compact key: value text for APIs and LLM prompts. The Python side of @flattype/* on npm.
|
|
5
|
+
Project-URL: Homepage, https://karammm.github.io/flattype/docs/
|
|
6
|
+
Project-URL: Documentation, https://karammm.github.io/flattype/docs/guide/python
|
|
7
|
+
Project-URL: Repository, https://github.com/karammm/flattype
|
|
8
|
+
Project-URL: Issues, https://github.com/karammm/flattype/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/karammm/flattype/blob/main/CHANGELOG.md
|
|
10
|
+
Author: karammm
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: api,asgi,claude,fastapi,flatten,json,llm,serialization,tokens,toon
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Framework :: FastAPI
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
26
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: >=3.9
|
|
30
|
+
Provides-Extra: starlette
|
|
31
|
+
Requires-Dist: starlette>=0.27; extra == 'starlette'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
<h1 align="center">flattype</h1>
|
|
35
|
+
|
|
36
|
+
<p align="center">
|
|
37
|
+
<b>JSON in, compact <code>key: value</code> text out.</b><br>
|
|
38
|
+
Flat Notation for Python APIs and LLM prompts: about half the size of JSON on lists, and easy for people and models to read.
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<p align="center">
|
|
42
|
+
<a href="https://pypi.org/project/flattype/"><img src="https://img.shields.io/pypi/v/flattype?color=8b6cff" alt="PyPI"></a>
|
|
43
|
+
<a href="https://pypi.org/project/flattype/"><img src="https://img.shields.io/pypi/pyversions/flattype?color=8b6cff" alt="Python versions"></a>
|
|
44
|
+
<a href="https://github.com/karammm/flattype/actions/workflows/ci.yml"><img src="https://github.com/karammm/flattype/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
45
|
+
<img src="https://img.shields.io/badge/dependencies-0-2fd4b2" alt="No dependencies">
|
|
46
|
+
<a href="https://github.com/karammm/flattype/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license"></a>
|
|
47
|
+
</p>
|
|
48
|
+
|
|
49
|
+
<p align="center">
|
|
50
|
+
<a href="https://github.com/karammm/flattype/raw/main/docs/assets/flattype-intro.mp4">
|
|
51
|
+
<img src="https://raw.githubusercontent.com/karammm/flattype/main/docs/assets/flattype-intro.gif" width="800" alt="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">
|
|
52
|
+
</a><br>
|
|
53
|
+
<sub>▶ <a href="https://github.com/karammm/flattype/raw/main/docs/assets/flattype-intro.mp4">Watch the 92-second intro</a>. The Python part (step 6) is a real FastAPI app.</sub>
|
|
54
|
+
</p>
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
This is the Python side of [FlatType](https://github.com/karammm/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.
|
|
59
|
+
|
|
60
|
+
## Install
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install flattype
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Python 3.9+, no dependencies. `pip install "flattype[starlette]"` adds the optional `FlatResponse` class.
|
|
67
|
+
|
|
68
|
+
## Quick start
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
import flattype
|
|
72
|
+
|
|
73
|
+
flattype.encode({"name": "John", "age": 30, "city": "New York"})
|
|
74
|
+
# name: John
|
|
75
|
+
# age: 30
|
|
76
|
+
# city: New York
|
|
77
|
+
|
|
78
|
+
flattype.decode("name: John\nage: 30") # {'name': 'John', 'age': 30}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Lists, the shape of most API responses, become tables. Keys are written once, and nested dicts become dot paths:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
flattype.encode({
|
|
85
|
+
"page": 1,
|
|
86
|
+
"users": [
|
|
87
|
+
{"id": 1, "name": "Alice", "address": {"city": "Paris"}},
|
|
88
|
+
{"id": 2, "name": "Bob", "address": {"city": "Delhi"}},
|
|
89
|
+
],
|
|
90
|
+
})
|
|
91
|
+
# page: 1
|
|
92
|
+
# users[2]{id,name,address.city}:
|
|
93
|
+
# 1, Alice, Paris
|
|
94
|
+
# 2, Bob, Delhi
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`flattype.dumps` and `flattype.loads` are aliases, if you prefer the `json` names.
|
|
98
|
+
|
|
99
|
+
## FastAPI, Starlette and any ASGI app
|
|
100
|
+
|
|
101
|
+
One line. Clients that ask for `text/x-flat` get Flat Notation, and everyone else gets the same JSON as before:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from fastapi import FastAPI
|
|
105
|
+
from flattype.asgi import FlatMiddleware
|
|
106
|
+
|
|
107
|
+
app = FastAPI()
|
|
108
|
+
app.add_middleware(FlatMiddleware)
|
|
109
|
+
|
|
110
|
+
@app.get("/users")
|
|
111
|
+
def users():
|
|
112
|
+
return {"users": USERS} # plain FastAPI
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```console
|
|
116
|
+
$ curl localhost:3000/users
|
|
117
|
+
{"users":[{"id":1,"name":"Alice Martin","role":"admin"},{"id":2,"name":"Bob Singh","role":"editor"}]}
|
|
118
|
+
|
|
119
|
+
$ curl -H "Accept: text/x-flat" localhost:3000/users
|
|
120
|
+
users[2]{id,name,role}:
|
|
121
|
+
1, Alice Martin, admin
|
|
122
|
+
2, Bob Singh, editor
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
- **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`.
|
|
126
|
+
- **Caching:** `Vary: Accept` is added to every response, so caches keep the two formats apart.
|
|
127
|
+
- **Dependencies:** none. It's plain ASGI, so it also works with Litestar, Quart or a raw ASGI app.
|
|
128
|
+
- **Clients:** on the JavaScript side, `flatFetch` from [`@flattype/http`](https://www.npmjs.com/package/@flattype/http) or the React, Vue, Angular and Svelte hooks send that header for you.
|
|
129
|
+
|
|
130
|
+
To always answer in Flat Notation from one route, use the response class:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from flattype.starlette import FlatResponse
|
|
134
|
+
|
|
135
|
+
@app.get("/export", response_class=FlatResponse)
|
|
136
|
+
def export():
|
|
137
|
+
return {"users": USERS}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The [FastAPI example](https://github.com/karammm/flattype/tree/main/examples/python-fastapi) runs with one command.
|
|
141
|
+
|
|
142
|
+
## Typed models with Pydantic
|
|
143
|
+
|
|
144
|
+
`decode` returns plain dicts and lists, so Pydantic validates them like any parsed JSON:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from datetime import datetime
|
|
148
|
+
from pydantic import BaseModel, TypeAdapter
|
|
149
|
+
|
|
150
|
+
class User(BaseModel):
|
|
151
|
+
id: int
|
|
152
|
+
full_name: str
|
|
153
|
+
created_at: datetime
|
|
154
|
+
|
|
155
|
+
users = TypeAdapter(list[User]).validate_python(flattype.decode(text)["users"])
|
|
156
|
+
# [User(id=1, full_name='Alice', created_at=datetime.datetime(2026, 1, 1, 9, 0, tzinfo=TzInfo(0)))]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Claude and other LLMs
|
|
160
|
+
|
|
161
|
+
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`](https://www.npmjs.com/package/@flattype/ai), with the same output:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
import anthropic
|
|
165
|
+
from flattype.ai import to_prompt, output_instructions, from_model_output
|
|
166
|
+
|
|
167
|
+
client = anthropic.Anthropic()
|
|
168
|
+
message = client.messages.create(
|
|
169
|
+
model="claude-opus-5-5",
|
|
170
|
+
max_tokens=2048,
|
|
171
|
+
messages=[{
|
|
172
|
+
"role": "user",
|
|
173
|
+
"content": f"{to_prompt(orders, name='orders')}\n\nWhich customers ordered twice? "
|
|
174
|
+
+ output_instructions({"customers": [{"name": "Alice", "orders": 2}]}),
|
|
175
|
+
}],
|
|
176
|
+
)
|
|
177
|
+
reply = message.content[0].text
|
|
178
|
+
customers = from_model_output(reply) # or from_model_output(reply, Customer, many=True) with a Pydantic model
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
- **`to_prompt(data)`:** the data as Flat Notation inside a `<data>` tag, with a one-line legend that explains the format.
|
|
182
|
+
- **`output_instructions(example)`:** asks the model to answer in Flat Notation, with an example of the shape you expect.
|
|
183
|
+
- **`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.
|
|
184
|
+
- **`count_tokens(anthropic.Anthropic(), data)`:** compares JSON with Flat Notation using Claude's free token counter.
|
|
185
|
+
- **`tool_result(data)` and `claude_tool_result(id, data)`:** tool results for MCP servers and the Claude API.
|
|
186
|
+
|
|
187
|
+
## More
|
|
188
|
+
|
|
189
|
+
**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.
|
|
190
|
+
|
|
191
|
+
**Streaming:** `Decoder` reads chunks as they arrive, and calls `on_row` for each table row as soon as its line is complete:
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
d = flattype.Decoder(on_row=lambda row: print(row.path, row.index, row.value), keep_rows=False)
|
|
195
|
+
for chunk in response.iter_text():
|
|
196
|
+
d.write(chunk)
|
|
197
|
+
d.end()
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
**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.
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
team = {"lead": {"id": 1}}
|
|
204
|
+
team["members"] = [team["lead"]]
|
|
205
|
+
team["lead"]["team"] = team
|
|
206
|
+
flattype.encode(team, refs=True)
|
|
207
|
+
# lead.id: 1
|
|
208
|
+
# lead.team = ~
|
|
209
|
+
# members[0] = lead
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**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.
|
|
213
|
+
|
|
214
|
+
## Same text as JavaScript
|
|
215
|
+
|
|
216
|
+
Every case in [`conformance/`](https://github.com/karammm/flattype/tree/main/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:
|
|
217
|
+
|
|
218
|
+
- **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.
|
|
219
|
+
- **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.
|
|
220
|
+
- **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.
|
|
221
|
+
|
|
222
|
+
## Performance
|
|
223
|
+
|
|
224
|
+
It's pure Python. For 1,000 users with a nested address (Python 3.13, Apple M-series):
|
|
225
|
+
|
|
226
|
+
| | Size | Encode | Decode |
|
|
227
|
+
| --- | ---: | ---: | ---: |
|
|
228
|
+
| `json` (minified, C accelerated) | 114 KB | 0.5 ms | 0.45 ms |
|
|
229
|
+
| `flattype` | 59 KB (−48%) | 4.6 ms | 3.9 ms |
|
|
230
|
+
|
|
231
|
+
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.
|
|
232
|
+
|
|
233
|
+
## Links
|
|
234
|
+
|
|
235
|
+
- **Docs:** [karammm.github.io/flattype/docs](https://karammm.github.io/flattype/docs/), including the [format specification](https://karammm.github.io/flattype/docs/guide/format)
|
|
236
|
+
- **Playground:** [karammm.github.io/flattype](https://karammm.github.io/flattype/)
|
|
237
|
+
- **Security:** [policy and guarantees](https://karammm.github.io/flattype/docs/guide/security)
|
|
238
|
+
- **Changes:** [CHANGELOG](https://github.com/karammm/flattype/blob/main/CHANGELOG.md)
|
|
239
|
+
|
|
240
|
+
MIT © karammm
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
flattype/__init__.py,sha256=XnhlwwXSiuj9mR-s7-I_iLeZS2ROIevbva2aGemTr9U,1582
|
|
2
|
+
flattype/_decode.py,sha256=OyprVm01s_A-4VfSho8n8od57eo8GM5WORvp8UivFD8,11409
|
|
3
|
+
flattype/_encode.py,sha256=O5J5yexa8Ol-KmEIo1TV_UQdXh_kdwm4OD6AP9npVBE,6951
|
|
4
|
+
flattype/_flatten.py,sha256=YE9ea0a4hFeD1Zvr39kAfQ3B4uxtLk3T3LN_UHo5VXs,2110
|
|
5
|
+
flattype/_path.py,sha256=yG1rz8sflrwWzwzq9qKHG6CUhm7EXGZDZncgOURcy-Y,10774
|
|
6
|
+
flattype/_syntax.py,sha256=PdjCQI_ily0y7oJJuAd9MQDeOyXGv4EDiOJLST1o_-g,5342
|
|
7
|
+
flattype/ai.py,sha256=SZ0nQDsKxQdSR6xK_dIXRv_62gOVyoXVUFrqE-vQmdQ,6712
|
|
8
|
+
flattype/asgi.py,sha256=jYUNG62L55FpnkeGisF55hcUMa0UEq2_sW-qTkQDvM8,3795
|
|
9
|
+
flattype/negotiate.py,sha256=fzapsIkLAyZOfPZHJwsE2JG1uE3tJopURmlAIK6QiNY,1578
|
|
10
|
+
flattype/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
flattype/starlette.py,sha256=sC2MyRCH4hpLqiWAx5iPUw85iTP8o1bXV-s5QHUqVDc,708
|
|
12
|
+
flattype-1.11.0.dist-info/METADATA,sha256=sPqBwBdDwH14Mp5wFEIoq5ORVyT1ElZhG8sMBNsCiY0,11294
|
|
13
|
+
flattype-1.11.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
14
|
+
flattype-1.11.0.dist-info/licenses/LICENSE,sha256=OXM2ik92vIvE849OIfPp1fCN7TFVL6KqosdOQhKY-bE,1081
|
|
15
|
+
flattype-1.11.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FlatType.js contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|