argrepair
argrepair

how argrepair fits in

Install

consolepip install argrepair

Use

pythonfrom argrepair import parse_args, check, coerce, feedback, parse_and_check

parse_args("{'a': 1,}")                       # {'a': 1}
parse_args("{\"a\": True}")                   # {'a': True}
parse_args('```json\n{"a": 1}\n```')          # {'a': 1}

problems = check({"mode": "turbo"}, {"mode": {"type": "string", "enum": ["fast", "safe"]}})
print(feedback(problems, tool="search"))

The one call a middleware needs:

pythondef run_tool(name, raw_arguments, schema, tool):
    arguments, problems, message = parse_and_check(raw_arguments, schema, tool=name)
    if problems:
        return {"error": message}        # the model gets another turn
    return tool(**arguments)

Schema

python{
    "path":   {"type": "string", "required": True},
    "bytes":  {"type": "integer", "minimum": 0},
    "limit":  {"type": "number", "maximum": 100},
    "mode":   {"type": "string", "enum": ["fast", "safe"]},
    "tags":   {"type": "array", "items": {"type": "string"}},
    "dry_run": {"type": "boolean"},
}

type is one of string, integer, number, boolean, object, array, null. A boolean is not an integer here, and 1.5 is not an integer — the two mistakes that turn into silent data loss downstream. Unknown fields are reported as unexpected, so a model that invents colour: "red" is told so instead of the value being dropped.

What is repaired, and what is refused

input result
{'a': 1}parsed
{"a": True, "b": None}parsed
{"a": 1,}parsed
{"a": 1} // noteparsed
`json {…} ` parsed
{"a": 'x'} (single quotes inside JSON quotes)refused, so a real ' in a value is never mangled
[1, 2]refused: arguments must be an object
not json at alla message, not an exception, via parse_and_check

Coercion is deliberately narrow: "42" → 42, "1.5" → 1.5, "true"/"yes"/"on"/"1" → True, 3 → "3" for a string field, a lone value → a one-item array. Anything else stays as it was and is reported by check.

Limits

Reach for jsonschema when you need that; this library is about the message.

guessed at.

still the tool's job.

Development

consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest

The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.

License

MIT.

Other libraries here

One problem each, standard library only, no runtime dependencies.