Skip to main content

LLM Compact Serializer

A dynamic, schema-driven Python library designed to drastically reduce LLM token usage by compressing complex JSON objects into a strict, hierarchical format.

🚀 Why Use This?

When building LLM applications (using GPT-4, Gemini, Claude, etc.), passing large JSON arrays in the system prompt consumes a massive amount of tokens due to repeated keys and whitespace.

Standard JSON (Heavy):

[
  {"product": "Apple", "price": "1.20", "category": "Fruit"},
  {"product": "Banana", "price": "0.80", "category": "Fruit"}
]

Compact Protocol (Efficient):

|{Apple, 1.20, Fruit}|;|{Banana, 0.80, Fruit}|

Impact:

Reduces token count from ~55 tokens (JSON) to ~18 tokens (Compact) for this example.

Key Benefits

Token Efficiency: Reduces input payload size by 40-60% for repetitive data structures.

Schema Enforcement: Generates strict instructions for the LLM, reducing hallucinations.

Dynamic: Works with any Python object or dictionary; just define the schema at runtime.

Recursive: Supports deeply nested objects and lists via the |{...}| syntax.

📦 Installation

Clone the repository

git clone https://github.com/Ryujose/llm-compact-serializer.git

Install dependencies using Poetry

poetry install

⚡ Quick Start

This example demonstrates how to serialize a product list with nested destination data.

  1. Define Your Schema Tell the serializer what your data looks like. Order matters!
from llm_compact_serializer.domain.schema import CompactSchema, FieldConfig
from llm_compact_serializer.core.prompt_builder import PromptBuilder

# Define a nested schema for complex objects
destination_schema = CompactSchema(
    name="Destination",
    fields=[
        FieldConfig(source_name="address"),
        FieldConfig(source_name="phones", is_list=True) # Handles arrays [x, y]
    ]
)

# Define the root schema
product_schema = CompactSchema(
    name="Product",
    fields=[
        FieldConfig(source_name="name"),
        FieldConfig(source_name="price"),
        FieldConfig(source_name="destination", nested_schema=destination_schema)
    ]
)
  1. Prepare Your Data You can use Dictionaries, Pydantic models, or Dataclasses.
data = [
    {
        "name": "MacBook Pro", 
        "price": "1200€", 
        "destination": {
            "address": "Silicon Valley, CA",
            "phones": [5550199, 5550200]
        }
    }
]
  1. Generate the Prompt The PromptBuilder automatically generates the protocol instructions and injects your compressed data.
builder = PromptBuilder(product_schema)
base_prompt = "Analyze the following orders: [INPUT]"

final_prompt = builder.build(base_prompt, data, data_marker="[INPUT]")
print(final_prompt)
  1. Output (What the LLM Sees)
[COMPACT_HIERARCHICAL_PROTOCOL]
[INSTRUCTIONS]
1. Interpret input strictly as a Recursive Compact Hierarchy.
2. Syntax: Complex objects enclosed in |{ }|, separated by comma.
3. Structure Mapping:
# 1 = Product (Root)
# 1a = name
# 1b = price
# 1.1 = destination
# 1.1a = address
# 1.1b* = phones
[END_PROTOCOL]

Analyze the following orders: |{MacBook Pro, 1200€, |{Silicon Valley, CA, [5550199, 5550200]}|}|

🏗 Architecture

The project follows Clean Architecture principles to ensure modularity and ease of testing.

llm-compact-serializer/
├── .github/
│   └── workflows/
│       ├── ci.yml              # CI/CD: Tests & Linting
│       └── publish.yml         # CD: Publish to PyPI
├── src/
│   └── llm_compact_serializer/
│       ├── __init__.py
│       ├── domain/             # Schema definitions (The "Rules")
│       │   ├── __init__.py
│       │   └── schema.py
│       └── core/               # The Engine (Generic Logic)
│           ├── __init__.py
│           └── serializer.py
├── tests/ 
│   └──/ # more tests
├── LICENSE                     # MIT
├── README.md
├── pyproject.toml              # Poetry Config
└── poetry.lock

The Protocol Rules

Object Wrapping: All objects are wrapped in |{ ... }|.

Separators: Fields are separated by ,. Objects in a list are separated by ;.

Recursion: A field can contain another object, creating a nested structure: |{ val1, |{ val2 }| }|.

Arrays: Simple lists are wrapped in [...].

Missing Data: None or empty values are automatically replaced with - to maintain positional integrity.

Sanitization: Commas found within data values are automatically replaced (e.g., Doe, John -> Doe John) to prevent parsing errors.

🧪 Testing

We use pytest for comprehensive testing, covering unit logic and end-to-end integration.

# Run all tests
poetry run pytest

# Run with coverage report
poetry run pytest --cov=src

Key Test Scenarios

test_serializer.py: Verifies primitive handling, recursive nesting logic, and sanitization (handling commas in data).

test_integration.py: Validates the full workflow (Schema -> Data -> Prompt) using complex real-world examples.

🤝 Contributing

  1. Fork the repository.

  2. Create a feature branch (git checkout -b feat/amazing-feature).

  3. Commit your changes (git commit -m 'feat: Add amazing feature').

  4. Push to the branch (git push origin feat/amazing-feature).

  5. Open a Pull Request.

📄 License

Distributed under the MIT License. See LICENSE for more information.

Metadata

Release files for llm-compact-serializer 0.1.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 llm-compact-serializer 0.1.0
File Size Uploaded Provenance
llm_compact_serializer-0.1.0.tar.gz 6.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-compact-serializer 0.1.0
File Interpreter ABI Platform
llm_compact_serializer-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / llm_compact_serializer-0.1.0.tar.gz

Download URL llm_compact_serializer-0.1.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0bc4b295643948da4c27a94e7dc6e7d6c8f5561cc714c100005d146a0c970811
BLAKE2b-256 checksum
How to use checksums
f1294e14a5699c66053803850a8c1df5347faf71594e6f9081ef7a12c355aa13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.10.19 Linux/6.11.0-1018-azure

Release files / llm_compact_serializer-0.1.0-py3-none-any.whl

Download URL llm_compact_serializer-0.1.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f183d6e23ae7656ee1828d5c5b92acf4f0dda53dd2c1dd0d46e9ead9fb3d27fd
BLAKE2b-256 checksum
How to use checksums
b4c0dbd9bc4fbc2c4a316d0934b512e33294c6f90feb2e592ec45b01b6876f05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.10.19 Linux/6.11.0-1018-azure

Security for llm-compact-serializer 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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