Skip to main content

A token-efficient, repair-tolerant interchange format for LLM I/O — pure-Python encode/decode/fix/validate.

Project description

raif-format (Python)

Pure-Python implementation of RAIF — a token-efficient, repair-tolerant interchange format for LLM input/output. Stdlib only, no runtime dependencies, fully typed (PEP 561).

This package mirrors the canonical TypeScript reference byte-for-byte; parity is pinned by a shared conformance corpus.

Install

pip install raif-format        # or: uv add raif-format

Installs the raif-format distribution; the import package is raif.

Usage

from raif import encode, decode, decode_lenient, fix, validate, parse_schema

# JSON object -> canonical RAIF (byte-identical to the TS encoder)
encode({"to": "a@b.com", "subject": "hi"})
# 'subject=hi\nto=a@b.com'

# Generation profile (what models are trained to emit)
encode({"items": [{"id": 1}, {"id": 2}]}, {"profile": "generation"})

# RAIF -> JSON (with repair reporting)
decode("a=1\nb=hi")
# {'ok': True, 'value': {'a': 1, 'b': 'hi'}, 'repairs': []}

# Per-leaf recovery — never raises, surfaces truncation
decode_lenient("<raif>\ncity=Oslo\nlat")
# {'value': {'city': 'Oslo'}, 'errors': [...], 'repairs': [...], 'truncated': True}

# Canonicalize (decode -> re-encode); idempotent
fix("```\na=1\n```")
# {'ok': True, 'canonical': 'a=1', 'repairs': [...]}

# Read-only canonicality check
validate("a=1")
# {'ok': True}

# Optional schema-typed decode
schema = parse_schema("priority:n\nnote:s?")
decode("priority=2\nnote=hi", schema)

API

Function Returns
encode(obj, opts=None) str (canonical RAIF)
decode(text, schema=None) {"ok", "value"|"error", "repairs"}
decode_lenient(text, schema=None) {"value", "errors", "repairs", "truncated"}
fix(text, schema=None) {"ok", "canonical"|"error", "repairs"}
validate(text, schema=None) {"ok"} or {"ok": False, "errors"}
parse_schema(decl) RaifSchema

opts is {"profile": "canonical" | "generation", "markers": bool}.

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

raif_format-0.5.0.tar.gz (47.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

raif_format-0.5.0-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file raif_format-0.5.0.tar.gz.

File metadata

  • Download URL: raif_format-0.5.0.tar.gz
  • Upload date:
  • Size: 47.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for raif_format-0.5.0.tar.gz
Algorithm Hash digest
SHA256 42cbbcaff6cd0771344edc1068b606acf85ffb2438a98e8eee3cfc5c7b8d9312
MD5 daefd9efe7e29d7f6aaa6e5e87e70f09
BLAKE2b-256 a98f18c79cc1b3a056c06dc19e3edb9829ec67ce4fd27746c8169b83028a0e19

See more details on using hashes here.

Provenance

The following attestation bundles were made for raif_format-0.5.0.tar.gz:

Publisher: py-release.yml on skrrt-sh/raif-standard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file raif_format-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: raif_format-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for raif_format-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bae3c1498dcb0817af2a21d90ec3a6a5328fdb0b2e1d4550c4748091cca0d2c
MD5 bdc28d435355ecffc7c9605c6ca3f664
BLAKE2b-256 f49875ff5f902fbca652871002baacc1653cff247a792be2a7d6c79c88732d68

See more details on using hashes here.

Provenance

The following attestation bundles were made for raif_format-0.5.0-py3-none-any.whl:

Publisher: py-release.yml on skrrt-sh/raif-standard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page