Skip to main content

parse_llm_json

Function parse_llm_json 

Source
pub fn parse_llm_json<T: DeserializeOwned>(
    raw: &str,
) -> Result<T, LlmJsonParseError>
Expand description

Parses LLM JSON output with automatic repair of common errors.

Applies these repairs in order:

  1. Strip markdown code fences (json ... )
  2. Find the outermost JSON bracket pair
  3. Remove trailing commas before } or ]
  4. Fix unescaped inner quotes within string values (heuristic)
  5. Truncate to the last matching } or ] if there’s trailing garbage

Returns the deserialized value or an error if repair fails.