Skip to main content

Repair malformed JSON from language models and hand-written config.

Project description

laga

laga repairs malformed JSON and returns Python objects.

It is built for the real world: model output, hand-written config, copied snippets, and other almost-JSON text that is usually close enough to recover but not strict enough for json.loads.

Highlights

  • Repairs common JSON mistakes such as trailing commas, missing commas, single quotes, unquoted keys, comments, fences, smart quotes, and truncated containers.
  • Tries the standard library first, so valid JSON stays fast and strict.
  • Keeps the public API small: repair, repair_to_str, loads, and LagaError.
  • Ships with a CLI, type information, tests, docs, and CI.

Supported Versions

laga 0.1.5 supports Python 3.10 through 3.13.

Install

For normal use from PyPI:

python -m pip install laga

For local development from a checkout:

python -m pip install -e ".[dev]"

Quick Start

import laga

text = "{name: 'Ada', active: True, roles: ['admin', 'writer',],}"

data = laga.repair(text)
print(data)
print(laga.repair_to_str(text))

CLI

You can also use laga from the command line:

laga "{name: 'Ada', active: True, roles: ['admin',]}"
laga --strict '{"a": 1,}'

Use --strict when you want to reject ambiguous repairs instead of filling in gaps. Use --pretty to format output for humans, --stdin to read from standard input, and --duplicate-keys error when duplicate keys should fail. Use --input to read from a file and --output to write the repaired result back to disk.

laga --stdin --pretty
laga --input sample.txt --output fixed.json
laga --max-depth 64 --max-input-size 10000 --duplicate-keys error '{"a": 1}'

API

Function Returns Description
laga.repair(text, strict=False) Python object Repairs malformed JSON and returns Python data.
laga.repair_to_str(text, strict=False) str Repairs malformed JSON and returns normalized JSON text.
laga.repair_file(path, strict=False) Python object Reads a file and repairs its contents.
laga.loads(text, strict=False) Python object Alias for laga.repair.
laga.LagaError Exception type Raised when repair fails or input is unrecoverable.

What It Repairs

Rule Before After
Trailing comma {"a": 1,} {"a":1}
Missing comma {"a": 1 "b": 2} {"a":1,"b":2}
Single quotes {'a': 'b'} {"a":"b"}
Unquoted keys {name: "Ada"} {"name":"Ada"}
Python literals {"ok": True, "none": None} {"ok":true,"none":null}
Comments {"a": 1 // note\n} {"a":1}
Markdown fences json\n{"a":1}\n {"a":1}
Smart quotes “a” "a"
Prose around JSON Result: {"a":1} {"a":1}
Truncated structure {"a": [1, 2 {"a":[1,2]}

Strict Mode

strict=True is for validation workflows where you want laga to fail instead of guessing.

In strict mode, laga rejects ambiguous recoveries such as:

  • missing commas between object members or array items
  • unterminated objects or arrays
  • other repairs that would require filling in structure the input did not clearly provide

Behavior Notes

  • json.loads is still the best choice when you control the producer and the input is already valid JSON.
  • Duplicate keys use last-value-wins behavior, matching Python dict semantics.
  • Use duplicate_keys="error" when you want duplicate object members to fail instead of being merged.
  • laga does not evaluate expressions or try to invent arbitrary JavaScript syntax.
  • The parser is intentionally conservative around malformed numbers and highly ambiguous text.

Quick Decision Guide

Situation Best choice
You control the producer and the input is valid JSON json.loads
You have noisy LLM output, copied config, or prose around JSON laga.repair
You need to repair a file directly laga.repair_file
You want duplicate keys to fail duplicate_keys="error"
You want a human-friendly result repair_to_str(..., pretty=True)

When To Use laga

Use laga when the input is noisy, user-facing, or generated by a model and you need a predictable repair step before parsing.

Use json.loads when correctness and strictness matter more than recovery.

Development

make install
make lint
make typecheck
make test
make build

Project Links

Citation

If you use laga in academic work, research notes, or other formal writing, please cite the repository using the metadata in CITATION.cff.

Suggested citation format:

Laitinen-Fredriksson Lundström-Imanov, G. O. Y. (2026). laga (Version 0.1.5) [Computer software]. https://github.com/olaflaitinen/laga

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

laga-0.1.5.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

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

laga-0.1.5-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file laga-0.1.5.tar.gz.

File metadata

  • Download URL: laga-0.1.5.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for laga-0.1.5.tar.gz
Algorithm Hash digest
SHA256 368f9112f0d744719ed349337c91699a7f68f3aaf74fad8a7f5f8251e8e0b826
MD5 0bbc818d7e24f37ea50e8817660d159a
BLAKE2b-256 9130dc6a9c6b1874f826fbfb534d303d14b550437fe3103f3e65a2e721a9ba8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for laga-0.1.5.tar.gz:

Publisher: release.yml on olaflaitinen/laga

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

File details

Details for the file laga-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: laga-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for laga-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a0005553d5fc7e272cad29f6370e9e32948b1e4fe9fb347225f8d83083776478
MD5 668f0b4d5bbb1096441976dc7ec166b6
BLAKE2b-256 146f506d947bbfe556a61f60de87abd22d512474d1e97d2269f0844f0c325620

See more details on using hashes here.

Provenance

The following attestation bundles were made for laga-0.1.5-py3-none-any.whl:

Publisher: release.yml on olaflaitinen/laga

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