Skip to main content

Module json_repair

Module json_repair 

Source
Expand description

Tolerant JSON parsing for LLM outputs.

LLMs frequently produce malformed JSON: trailing commas, unescaped quotes, incomplete brackets, or markdown-wrapped code blocks. Historically this logic lived in lc-core, but ToolCall::parse_arguments (in this crate) parses the same kind of LLM-generated JSON and cannot depend on lc-core. So the repair pipeline lives here so that every parser of LLM JSON — in lc-shared and lc-core alike — converges on a single tolerant parser.

Enums§

JsonRepairError
Error types for tolerant JSON parsing.

Functions§

extract_bracket_pair
Finds the outermost [...] or {...} bracket pair.
fix_unescaped_quotes
Escapes " characters that appear inside string values but are not the string’s closing quote.
parse_tolerant_json
Parses LLM JSON output with automatic repair of common errors.
remove_trailing_commas
Removes trailing commas before closing brackets.
repair_json
Repairs common LLM JSON mistakes and extracts the JSON portion.
strip_code_fences
Strips markdown code fences from the text.
truncate_to_matching_bracket
Truncates to the last matching closing bracket if there’s trailing garbage.