Crate tryparse

Crate tryparse 

Source
Expand description

§tryparse

A forgiving parser that converts messy LLM responses into strongly-typed Rust structs.

This library handles common issues in LLM outputs like:

  • JSON wrapped in markdown code blocks
  • Trailing commas
  • Single quotes instead of double quotes
  • Unquoted object keys
  • Type mismatches (string numbers, etc.)

§Quick Start

use tryparse::parse;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct User {
    name: String,
    age: u32,
}

// Parse messy LLM output with unquoted keys and string numbers
let messy_response = r#"{name: "Alice", age: "30"}"#;

let user: User = parse(messy_response).unwrap();
assert_eq!(user.name, "Alice");
assert_eq!(user.age, 30); // Automatically coerced from string

§Features

  • Multi-Strategy Parsing: Tries multiple approaches to extract JSON
  • Smart Type Coercion: Converts between compatible types automatically
  • Transformation Tracking: Records all modifications made during parsing
  • Candidate Scoring: Ranks multiple interpretations by quality
  • Zero Configuration: Works out of the box with sensible defaults

§Advanced Usage

For more control over the parsing process:

use tryparse::{parse_with_candidates, parser::FlexibleParser};
use serde::Deserialize;

#[derive(Deserialize)]
struct Data {
    value: i32,
}

let response = r#"{"value": "42"}"#;

// Get all candidates with metadata
let (result, candidates) = parse_with_candidates::<Data>(response).unwrap();

// Or use the parser directly
let parser = FlexibleParser::new();
let flex_values = parser.parse(response).unwrap();

Modules§

constraints
Constraint validation system for LLM-parsed values.
deserializer
Smart deserializer with type coercion.
error
Error types for LLM-to-struct parsing.
parser
Parser module that coordinates parsing strategies.
scoring
Scoring system for ranking parsing candidates.
value
Flexible value types with metadata.

Structs§

ParseMetadata
Metadata about a parsing operation.

Functions§

parse
Parses an LLM response into a strongly-typed Rust struct.
parse_llm
Parses an LLM response using BAML’s deserialization algorithms.
parse_llm_with_candidates
Parses an LLM response using BAML’s algorithms and returns all candidates.
parse_with_candidates
Parses an LLM response and returns both the result and all candidates.
parse_with_metadata
Parses an LLM response and returns metadata about the parsing process.
parse_with_parser
Parses an LLM response using a custom parser.