Show HN: Painless structured data from LLMs in Rust: rstructor

github.com

2 points by cliftonk 9 hours ago

I built rstructor, a Rust library for structured LLM outputs. It's like Python's Instructor/Pydantic, but with the power and type-safety of Rust.

You define your data models as Rust structs/enums, and rstructor handles the rest: JSON Schema generation, LLM communication (OpenAI, Anthropic, etc.), parsing, and validation.

Example - extracting structured data from unstructured text:

    use rstructor::{Instructor, LLMClient, OpenAIClient, OpenAIModel};
    use serde::{Serialize, Deserialize};

    #[derive(Instructor, Serialize, Deserialize, Debug)]
    enum Severity { Low, Medium, High, Critical }

    #[derive(Instructor, Serialize, Deserialize, Debug)]
    enum IssueType {
        Bug { severity: Severity },
        FeatureRequest,
        Performance { ms_impact: u32 },
    }

    #[derive(Instructor, Serialize, Deserialize, Debug)]
    struct TriagedIssue {
        title: String,
        issue_type: IssueType,
    }

    let client = OpenAIClient::new(env::var("OPENAI_API_KEY")?)?
        .model(OpenAIModel::Gpt4OMini);

    // Performance issue example
    let perf_issue: TriagedIssue = client.materialize(
        "The login page takes about 500ms to load, which feels really slow compared to other pages."
    ).await?;

    // Bug report example
    let bug_issue: TriagedIssue = client.materialize(
        "When I enter an invalid email address, the login page crashes with a null pointer exception."
    ).await?;

    // Now you have type-safe data - leverage Rust's pattern matching
    match perf_issue.issue_type {
        IssueType::Performance { ms_impact } => {
            println!("Performance issue detected: {}ms impact", ms_impact);
        }
        IssueType::Bug { severity } => {
            println!("Bug found with severity: {:?}", severity);
        }
        _ => {}
    }


    Key features:
    * Support for OpenAI, Anthropic, Grok (xAI), and Gemini
    * Custom validation rules with automatic detection
    * Nested structures, arrays, and enums with associated data
    * Automatic retry with validation error feedback
Crate: https://crates.io/crates/rstructor

GitHub: https://github.com/clifton/rstructor

Docs: https://docs.rs/rstructor

Would love feedback from anyone building LLM-powered tools in Rust!