Skip to main content

LlmDeserialize

Derive Macro LlmDeserialize 

Source
#[derive(LlmDeserialize)]
{
    // Attributes available to this derive:
    #[llm]
}
Expand description

Derives the LlmDeserialize trait for structs and enums.

This macro generates a custom deserialization implementation using BAML’s algorithms for fuzzy field matching and type coercion.

§Features

  • Fuzzy field matching: Handles different naming conventions (userName ↔ user_name)
  • Fuzzy enum matching: Case-insensitive, substring, and edit-distance matching for variants
  • Union types: Score-based variant selection with #[llm(union)]
  • Optional fields: Automatic handling of Option<T> fields
  • Transformation tracking: Records all coercions applied during parsing

§Example

use tryparse::deserializer::LlmDeserialize;

#[derive(LlmDeserialize)]
struct User {
    name: String,
    age: u32,
    email: Option<String>, // Optional field
}

// Handles messy input like:
// {"userName": "Alice", "age": "30"}  // camelCase + string number

§Union Types

#[derive(LlmDeserialize)]
#[llm(union)]
enum Value {
    Number(i64),
    Text(String),
}

// Automatically picks the best matching variant