Skip to main content

raif-format (Python)

Pure-Python implementation of RAIF — a token-efficient, repair-tolerant layer for the JSON language models produce: structured outputs, strict objects, JSON mode, tool arguments. Stdlib only, no runtime dependencies, fully typed (PEP 561). Tool calls are one use case, not the point.

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}.

The RAIF ecosystem

raif-format is the Python codec (installs as raif-format, imports as raif); everything else builds on the same format.

License

Apache-2.0

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.6.0.tar.gz (51.9 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.6.0-py3-none-any.whl (31.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: raif_format-0.6.0.tar.gz
  • Upload date:
  • Size: 51.9 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.6.0.tar.gz
Algorithm Hash digest
SHA256 77c651efdef893b5a939dda05e50e3b664b445bd670fa1df6a87241fdccec7c1
MD5 ab5e947fbf85d3f8ec6087100f36446b
BLAKE2b-256 af35781e2bb53b8e753cccaf7b268ae179a97c0ad92a32582f090bb016aa10fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for raif_format-0.6.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.6.0-py3-none-any.whl.

File metadata

  • Download URL: raif_format-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 31.3 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.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7682601cd80c29c93c3ecb7e67b9f217d99ba62b370035bbe27ffb180703732
MD5 519b7a59d9d15b4bcb4b4ce3d339e5c5
BLAKE2b-256 a499ef14a967e4937d9a16f60d655ffe2e57aacb44593bbccc9e80ace4462918

See more details on using hashes here.

Provenance

The following attestation bundles were made for raif_format-0.6.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.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.0

2 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