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:
- Direct JSON (
serde_json::from_str) - Markdown code blocks (
json ...) - Greedy JSON extraction (first
{...}or[...]in text) - Fixing parser (close brackets, strip trailing commas, unquoted keys)
- Fail with all candidates listed
Works with any model — no structured output API required.
Structs§
- Candidate
- A parse candidate with provenance info for debugging.
- Parse
Error - Parse error with all attempted candidates.
- Parse
Result - Result of a flexible parse attempt.
Enums§
Functions§
- collect_
candidates - Collect all parse candidates from raw text (AnyOf pattern).
- parse_
flexible - Parse raw LLM output into type
Tusing the AnyOf cascade. - parse_
flexible_ coerced - Parse with schema-aware coercion: “42” → 42, “true” → true, “redd” → “Red”.