Skip to main content

Module flexible_parser

Module flexible_parser 

Source
Expand description

Flexible JSON parser — extracts structured data from messy LLM output.

Inspired by BAML’s “jsonish” SAP (Schema-Aligned Parsing) approach. Collects multiple parse candidates (AnyOf), tries to deserialize each into the target type T, returns the first success.

Parse cascade:

  1. Direct JSON (serde_json::from_str)
  2. Markdown code blocks (json ... )
  3. Greedy JSON extraction (first {...} or [...] in text)
  4. Fixing parser (close brackets, strip trailing commas, unquoted keys)
  5. Fail with all candidates listed

Works with any model — no structured output API required.

Structs§

Candidate
A parse candidate with provenance info for debugging.
ParseError
Parse error with all attempted candidates.
ParseResult
Result of a flexible parse attempt.

Enums§

CandidateSource

Functions§

collect_candidates
Collect all parse candidates from raw text (AnyOf pattern).
parse_flexible
Parse raw LLM output into type T using the AnyOf cascade.
parse_flexible_coerced
Parse with schema-aware coercion: “42” → 42, “true” → true, “redd” → “Red”.