Skip to main content

Crate llm_json_repair

Crate llm_json_repair 

Source
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:

  1. Strip markdown code fences (triple-backtick or triple-tilde).
  2. Extract the first balanced {...} or [...] from surrounding prose.
  3. 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§

RepairError
Errors returned by parse.

Functions§

extract_balanced
Return the first balanced {…} or […] substring found in input, or None if 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_str but 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.