Structured Output Repair
Structured Output Repair extracts and repairs JSON and YAML generated by language models. It runs locally on text you already have, using Python and deterministic parsing and repair rules. Use it between a model response and your application's own schema or domain validation.
Install
Install the package in a fresh Python 3.11–3.13 environment:
python -m venv .venv
.venv/bin/python -m pip install structured-output-repair==0.4.0
On Windows, use .venv\Scripts\python.exe. For a local build, install the wheel by its file path.
The distribution is named structured-output-repair. The Python import remains
llm_output_repair and the diagnostic command remains llm-repair.
The PyPI project named llm-output-repair
belongs to a different project. Its 0.2.0 source distribution uses
llm_json_repair and declares no console commands.
Use a clean environment for Structured Output Repair, especially if you
previously installed a Git-based distribution of this repository under the old name. The old Git
distribution shares the namespace and CLI.
Quick start
from llm_output_repair import repair
result = repair('Result:\n{"items": [1,,2,], "ok": true}')
assert result == {"items": [1, 2], "ok": True}
repair() returns the decoded Python value. It defaults to SAFE for JSON
and TOLERANT for YAML. The named helpers repair_json() and
repair_yaml() retain their historical TOLERANT defaults for plain calls.
With a schema hint, repair_json(raw, schema=hint) defaults to SAFE.
Choose a policy
| Policy | JSON | YAML |
|---|---|---|
strict |
Decode without repair, with duplicate-key and nonstandard constant rejection. Supports objects, arrays and scalars through the value-returning helpers. | One document through PyYAML's safe_load, with native YAML value types. |
safe |
Object root only. Recover one object from supported presentation and apply supported local syntax repairs with data-preservation checks. Reject missing or competing structured content, duplicate keys, ambiguous repairs and floats that decode to infinity. | Unavailable. |
tolerant |
Legacy heuristic extraction and repair. Can select a fragment, remove placeholders or reinterpret encoded strings. Supports objects, arrays and scalar values when the extractor can decode them. | Legacy normalization of YAML-like text followed by safe loading. The heuristics can change formatting, structure and scalar interpretation. |
SAFE can handle supported headings, fences and prose around one object. It
also handles supported combinations of missing and redundant separators where
keys and values are already present. It may reject malformed text that an
application could interpret using more context. In the development version, a strict object may be repeated verbatim after a
prose heading in a labeled JSON code fence. Bare repetitions and differences
inside the fenced copy cause a refusal. JSON SAFE accepts
an optional JSON Schema hint, for example
repair(raw, schema=Plan.model_json_schema()). Valid objects and unique syntax
repairs retain every field, including undeclared properties. For adjacent
strings in an array, inline array-length hints can disambiguate two readings.
Validate the returned value in your application. CrashRecorder stores the hint
with the original input for replay.
The SAFE v2 design contract
includes future behavior beyond this release.
Use parse_json(raw, policy="safe") for a JsonResult with value,
repaired and strategy. Its default expected root is dict. For
STRICT or TOLERANT, pass expected_type=object to allow any supported JSON
root. Previously exported legacy extractors remain available.
extract_json_new() returns JSON text, and extract_yaml_new()
returns a Python value. dump_yaml() returns YAML text. YAML can return
None for an empty document. STRICT JSON uses the Python decoder’s float
semantics, including overflow in exponent notation. SAFE rejects that overflow.
In TOLERANT mode, parse_json() tries the checked decoder before its repair
fallback. The historical repair_json() default calls the extractor directly.
Handle failures and validate the result
from llm_output_repair import repair
def read_items(raw: str) -> list[int]:
result = repair(raw)
items = result.get("items")
if not isinstance(items, list) or any(type(item) is not int for item in items):
raise ValueError("Expected an array of integers")
return items
try:
items = read_items('{"items":[1,2,],}')
except ValueError:
# Report a parsing or validation failure through your application.
items = None
JSON parsing and policy refusals raise ValueError (including
json.JSONDecodeError). Non-string arguments to repair() raise
TypeError. YAML loading can raise yaml.YAMLError; tolerant YAML
normalization can also raise ValueError. Deep inputs may hit Python's
recursion limit. Validate field types, required fields and domain invariants
after parsing. Errors can include input text, so handle their details as application data.
Opt-in diagnostics and replay
Importing the package and parsing normally create no diagnostic files or
directories. The library performs no network calls, inference or background
work. To capture a terminal failure explicitly, attach a CrashRecorder
and choose a private directory:
from llm_output_repair import CrashRecorder, repair
recorder = CrashRecorder("./private-dumps")
try:
repair('{"items":', recorder=recorder)
except ValueError:
pass
.venv/bin/llm-repair replay ./private-dumps/CASE_ID.json
.venv/bin/llm-repair verify --corpus ./private-regressions --bucket fixed
Replay uses the saved operation and options locally. An expected terminal
failure returns exit code 1 when replaying a raw dump. For a reviewed
regression fixture, exit code 0 means the expectation matched. Use
llm-repair promote --help to turn a dump into a fixture with an explicit
expected value or error. Dumps can contain the original response. Keep them
private. CrashLogger remains available for legacy diagnostic logs.
Beta support and limits
Supported and tested Python versions are 3.11, 3.12 and 3.13. The runtime requires PyYAML and Pydantic. The library has no Fetchgraph dependency. When migrating from a Git pin, install the new distribution in a fresh environment. A dependency declaration for the old distribution name must be updated separately.
The JSON and YAML repair and extraction entry points check a limit of 1,000,000 Python string characters before parsing. The malformed-object scan in JSON SAFE caps nesting at 128 levels. An object that decodes strictly can take a different path. The repeated candidate search in TOLERANT JSON caps the sum of inspected suffix lengths at 8,000,000 characters per search. These checks do not bound total CPU, memory, output size or depth across all decoder paths. For untrusted input, limit payloads, resulting sizes and alias expansion in the calling application. Use process-level limits when you need an enforceable execution budget.
See the changelog for compatibility changes and the repository for implementation, provenance and issue tracking.
License
This standalone library is licensed under MIT. The NOTICE records the provenance of the code extracted from Backplane.
Metadata
Release files for structured-output-repair 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| structured_output_repair-0.4.0.tar.gz | 78.1 kB | With provenance | Details for structured_output_repair-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| structured_output_repair-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for structured_output_repair-0.4.0-py3-none-any.whl |
Total release size: 139.5 kB
Release files / structured_output_repair-0.4.0.tar.gz
| Download URL | structured_output_repair-0.4.0.tar.gz |
|---|---|
| Size | 78.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46c88ba92f5c807defa28d2157a08592952db3fcefc42b30aba42424cd4b96d7
|
|
BLAKE2b-256 checksum How to use checksums |
b023d3055ef9002dcbf7d825333bd661d9db26abd0451ec734ef7c49c47c8a92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logRelease files / structured_output_repair-0.4.0-py3-none-any.whl
| Download URL | structured_output_repair-0.4.0-py3-none-any.whl |
|---|---|
| Size | 61.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd774a67fd60de3b2849cb2b9782073e0f89d835dd116085b465e9e9ac0b7cc3
|
|
BLAKE2b-256 checksum How to use checksums |
5c32acc2136693ce685a0980ba32924fef6b5533606a67e9442775bfdc60e321
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logSecurity for structured-output-repair 0.4.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for structured_output_repair-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.
Transparency logA 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.