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.
- Spec & monorepo: https://github.com/skrrt-sh/raif-standard
- JavaScript/TypeScript package:
raif-formaton npm
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.
- Spec, ADRs & conformance corpus:
raif-standard - JavaScript/TypeScript package, same name:
raif-formaton npm - Models that emit RAIF natively — LoRA fine-tunes that make small/local models output RAIF instead of JSON. Decode their output with this package:
decode(model_output)["value"].skrrt-sh/raif-llama-3.2-3b-lora— clears the v0.5 gate (100% parse / 95% fidelity)skrrt-sh/raif-qwen3-4b-lora— agent-grade, runs on ~14 GB VRAMskrrt-sh/raif-qwen2.5-0.5b-lora— a 6×-smaller-base study- Training & eval recipe:
raif-lora
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77c651efdef893b5a939dda05e50e3b664b445bd670fa1df6a87241fdccec7c1
|
|
| MD5 |
ab5e947fbf85d3f8ec6087100f36446b
|
|
| BLAKE2b-256 |
af35781e2bb53b8e753cccaf7b268ae179a97c0ad92a32582f090bb016aa10fd
|
Provenance
The following attestation bundles were made for raif_format-0.6.0.tar.gz:
Publisher:
py-release.yml on skrrt-sh/raif-standard
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
raif_format-0.6.0.tar.gz -
Subject digest:
77c651efdef893b5a939dda05e50e3b664b445bd670fa1df6a87241fdccec7c1 - Sigstore transparency entry: 1895362949
- Sigstore integration time:
-
Permalink:
skrrt-sh/raif-standard@3e1811761e0adcbcd48bd2177a1711992fe3141e -
Branch / Tag:
refs/tags/py-v0.6.0 - Owner: https://github.com/skrrt-sh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
py-release.yml@3e1811761e0adcbcd48bd2177a1711992fe3141e -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7682601cd80c29c93c3ecb7e67b9f217d99ba62b370035bbe27ffb180703732
|
|
| MD5 |
519b7a59d9d15b4bcb4b4ce3d339e5c5
|
|
| BLAKE2b-256 |
a499ef14a967e4937d9a16f60d655ffe2e57aacb44593bbccc9e80ace4462918
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
raif_format-0.6.0-py3-none-any.whl -
Subject digest:
c7682601cd80c29c93c3ecb7e67b9f217d99ba62b370035bbe27ffb180703732 - Sigstore transparency entry: 1895362993
- Sigstore integration time:
-
Permalink:
skrrt-sh/raif-standard@3e1811761e0adcbcd48bd2177a1711992fe3141e -
Branch / Tag:
refs/tags/py-v0.6.0 - Owner: https://github.com/skrrt-sh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
py-release.yml@3e1811761e0adcbcd48bd2177a1711992fe3141e -
Trigger Event:
push
-
Statement type: