Expand description
§llm-json-repair
Clean and parse JSON emitted by LLMs.
Models still emit invalid JSON. They wrap it in markdown code fences, leave trailing commas, write prose around it, or use smart quotes. This crate does three local repair passes before you reach for a retry:
- Strip markdown code fences (triple-backtick or triple-tilde).
- Extract the first balanced
{...}or[...]from surrounding prose. - Remove trailing commas before
}or].
let raw = r#"Sure, here you go:
```json
{ "answer": "Paris", "confidence": 0.95, }
```"#;
let cleaned = llm_json_repair::repair(raw);
assert_eq!(cleaned, r#"{ "answer": "Paris", "confidence": 0.95 }"#);With the default serde feature you can parse straight to a typed value:
use serde::Deserialize;
#[derive(Deserialize)]
struct Answer { value: String }
let raw = "```\n{\"value\": \"42\",}\n```";
let parsed: Answer = llm_json_repair::parse(raw).unwrap();
assert_eq!(parsed.value, "42");Enums§
- Repair
Error - Errors returned by
parse.
Functions§
- extract_
balanced - Return the first balanced
{…}or[…]substring found ininput, orNoneif no balanced structure exists. - parse
- Repair the input and parse it as JSON into the given type.
- parse_
strict - Parse without repair. Same as
serde_json::from_strbut typed under the crate’s error. - repair
- Apply all three repair passes and return the cleaned string.
- strip_
fences - Remove leading and trailing markdown fences if present, returning the inner text. Whitespace around the result is trimmed.
- strip_
trailing_ commas - Remove
, }and, ]patterns outside of strings.