Skip to main content

Crate fuzzy_parser

Crate fuzzy_parser 

Source
Expand description

Fuzzy JSON repair for LLM-generated DSL

This crate provides generic fuzzy matching and automatic correction for JSON that may contain typos (common when generated by LLMs).

§Design

This crate provides generic repair APIs - the schema definitions live in the calling crate (e.g., your application defines the schema).

The repair pipeline has three independent stages:

  1. Extract (extract_json) — locate the JSON payload inside raw LLM output (Markdown code fences, surrounding prose, multiple blocks)
  2. Sanitize (sanitize_json) — fix syntax errors (trailing commas, missing/mismatched closing delimiters, unclosed strings)
  3. Repair (repair_tagged_enum_json) — fix typos and coerce value types, guided by a caller-provided schema

§Features

  • JSON extraction: Pull JSON out of code fences and prose
  • JSON sanitization: Fix syntax errors (trailing commas, missing braces)
  • Tagged enum repair: Fix type discriminator typos (e.g., "AddDeriv""AddDerive")
  • Field name repair: Fix field name typos (e.g., "taget""target")
  • Enum value repair: Fix string values constrained to closed sets (e.g., ["Debg"]["Debug"])
  • Recursive schemas: Nested objects, arrays of objects, and nested tagged enums (e.g. arrays of DSL intents) are repaired to any depth (FieldKind)
  • Type coercion: Fix string-encoded scalars (e.g., "42"42)
  • Dynamic schemas: Schemas own their data and can be built at runtime
  • JSON Schema import: Derive repair schemas from JSON Schema documents (TaggedEnumSchema::from_json_schema) or, with the schemars feature, directly from #[derive(JsonSchema)] types
  • Transparency: Every applied change is a Correction; every collision-skipped rename is a SkippedCorrection
  • Multiple algorithm support: Jaro-Winkler, Levenshtein, Damerau-Levenshtein
  • Configurable similarity threshold

§Example

use fuzzy_parser::{
    extract_json, sanitize_json, repair_tagged_enum_json, TaggedEnumSchema, FuzzyOptions,
};

// Define schema with enum arrays and nested objects
let schema = TaggedEnumSchema::new(
    "type",  // tag field
    &["AddDerive", "RemoveDerive"],  // valid types
    |tag| match tag {
        "AddDerive" | "RemoveDerive" => Some(&["target", "derives", "config"][..]),
        _ => None,
    },
)
.with_enum_array("derives", &["Debug", "Clone", "Serialize", "Default"])
.with_nested_object("config", &["timeout", "retries"]);

// Raw LLM output: prose + code fence + syntax errors + typos
let raw = r#"Here is the change you asked for:

```json
{"type": "AddDeriv", "taget": "User", "derives": ["Debg",], "config": {"timout": 30,}}
```
"#;

// Step 0: Extract (strip prose and code fences)
let payload = extract_json(raw).unwrap();

// Step 1: Sanitize (fix syntax errors)
let sanitized = sanitize_json(payload);

// Step 2: Repair (fix typos)
let result = repair_tagged_enum_json(&sanitized, &schema, &FuzzyOptions::default()).unwrap();

assert_eq!(result.repaired["type"], "AddDerive");
assert!(result.repaired.get("target").is_some());
assert_eq!(result.repaired["derives"][0], "Debug");
assert!(result.repaired["config"].get("timeout").is_some());

Re-exports§

pub use distance::Algorithm;
pub use distance::Match;
pub use error::FuzzyError;
pub use extract::extract_json;
pub use extract::extract_json_blocks;
pub use extract::strip_code_fences;
pub use import::ImportWarning;
pub use import::SchemaImport;
pub use repair::repair_enum_array;
pub use repair::repair_fields_with_list;
pub use repair::repair_object_fields;
pub use repair::repair_tagged_enum;
pub use repair::repair_tagged_enum_array;
pub use repair::repair_tagged_enum_json;
pub use repair::Correction;
pub use repair::FuzzyOptions;
pub use repair::RepairLog;
pub use repair::RepairResult;
pub use repair::SkipReason;
pub use repair::SkippedCorrection;
pub use sanitize::sanitize_json;
pub use schema::FieldDef;
pub use schema::FieldKind;
pub use schema::ObjectSchema;
pub use schema::TaggedEnumSchema;

Modules§

distance
String distance/similarity calculation utilities
error
Error types for fuzzy parsing
extract
Extraction of JSON payloads from raw LLM output
import
Importing repair schemas from JSON Schema documents
repair
Generic JSON repair logic
sanitize
JSON sanitization for malformed LLM output
schema
Schema definitions for fuzzy repair