Skip to content

Commit bdbda41

Browse files
committed
feat: add AGONStruct format
1 parent 59165ec commit bdbda41

7 files changed

Lines changed: 1507 additions & 5 deletions

File tree

src/agon/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
"""
55

66
from agon.core import AGON, EncodingResult, Format
7-
from agon.errors import AGONColumnsError, AGONError, AGONTextError
7+
from agon.errors import (
8+
AGONColumnsError,
9+
AGONError,
10+
AGONStructError,
11+
AGONTextError,
12+
)
813

914
__all__ = [
1015
"AGON",
1116
"AGONColumnsError",
1217
"AGONError",
18+
"AGONStructError",
1319
"AGONTextError",
1420
"EncodingResult",
1521
"Format",

src/agon/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
from agon.encoding import DEFAULT_ENCODING, count_tokens
2424
from agon.errors import AGONError
25-
from agon.formats import AGONColumns, AGONFormat, AGONText
25+
from agon.formats import AGONColumns, AGONFormat, AGONStruct, AGONText
2626

27-
Format = Literal["auto", "json", "text", "columns"]
27+
Format = Literal["auto", "json", "text", "columns", "struct"]
2828

2929

3030
@dataclass(frozen=True)
@@ -45,6 +45,7 @@ class AGON:
4545
- "json": Raw JSON (baseline)
4646
- "text": AGONText row-based format
4747
- "columns": AGONColumns columnar format for wide tables
48+
- "struct": AGONStruct template format for repeated object shapes
4849
4950
Core ideas:
5051
- Key elimination: objects become positional rows with inline schema.
@@ -58,11 +59,13 @@ class AGON:
5859
"json": lambda data: orjson.dumps(data).decode(),
5960
"text": AGONText.encode,
6061
"columns": AGONColumns.encode,
62+
"struct": AGONStruct.encode,
6163
}
6264

6365
_decoders: ClassVar[dict[str, Callable[[str], Any]]] = {
6466
"@AGON text": AGONText.decode,
6567
"@AGON columns": AGONColumns.decode,
68+
"@AGON struct": AGONStruct.decode,
6669
}
6770

6871
@staticmethod
@@ -83,6 +86,7 @@ def encode(
8386
- "json": Raw JSON
8487
- "text": AGONText row-based format
8588
- "columns": AGONColumns columnar format for wide tables
89+
- "struct": AGONStruct template format for repeated shapes
8690
force: If True with format="auto", always use a non-JSON format.
8791
min_savings: Minimum token savings ratio vs JSON to use non-JSON format.
8892
encoding: Tiktoken encoding for token counting (default: o200k_base).

src/agon/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ class AGONTextError(AGONError):
1717

1818
class AGONColumnsError(AGONError):
1919
"""Raised when AGONColumns encoding/decoding fails."""
20+
21+
22+
class AGONStructError(AGONError):
23+
"""Raised when AGONStruct encoding/decoding fails."""

src/agon/formats/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from agon.formats.base import AGONFormat
1010
from agon.formats.columns import AGONColumns
11+
from agon.formats.struct import AGONStruct
1112
from agon.formats.text import AGONText
1213

13-
__all__ = ["AGONColumns", "AGONFormat", "AGONText"]
14+
__all__ = ["AGONColumns", "AGONFormat", "AGONStruct", "AGONText"]

0 commit comments

Comments
 (0)