RStructor: Structured LLM Outputs for Rust
RStructor is a Rust library for extracting structured data from Large Language Models (LLMs) with built-in validation. Define your schemas as Rust structs/enums, and RStructor will handle the restβgenerating JSON Schemas, communicating with LLMs, parsing responses, and validating the results.
Think of it as the Rust equivalent of Instructor + Pydantic for Python, bringing the same structured output capabilities to the Rust ecosystem.
β¨ Features
- π Type-Safe Definitions: Define data models as standard Rust structs/enums with attributes
- π JSON Schema Generation: Auto-generates JSON Schema from your Rust types
- β Built-in Validation: Type checking plus custom business rule validation
- π Multiple LLM Providers: Support for OpenAI and Anthropic, with an extensible backend system
- π§© Complex Data Structures: Support for nested objects, arrays, and optional fields
- π Custom Validation Rules: Add domain-specific validation for reliable results
- π Async API: Fully asynchronous API for efficient operations
- βοΈ Builder Pattern: Fluent API for configuring LLM clients
- π Feature Flags: Optional backends via feature flags
π¦ Installation
Add RStructor to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
= { = "1.0", = ["rt-multi-thread", "macros"] }
π Quick Start
Here's a simple example of extracting structured information about a movie from an LLM:
use ;
use ;
use env;
// Define your data model
async
π Detailed Examples
Basic Example with Validation
Add custom validation rules to enforce business logic beyond type checking:
use ;
use ;
// Add custom validation
Complex Nested Structures
RStructor supports complex nested data structures:
use ;
use ;
// Define a nested data model for a recipe
// Usage:
// let recipe: Recipe = client.generate_struct("Give me a recipe for chocolate chip cookies").await?;
Working with Enums
RStructor supports both simple enums and enums with associated data.
Simple Enums
Use enums for categorical data:
use ;
use ;
// Define an enum for sentiment analysis
// Usage:
// let analysis: SentimentAnalysis = client.generate_struct("Analyze the sentiment of: I love this product!").await?;
Enums with Associated Data (Tagged Unions)
RStructor also supports more complex enums with associated data:
use ;
use ;
// Enum with different types of associated data
// Using struct variants for more complex associated data
// Usage:
// let user_status: UserStatus = client.generate_struct("What's the user's status?").await?;
When serialized to JSON, these enum variants with data become tagged unions:
// UserStatus::Away("Back in 10 minutes")
// PaymentMethod::Card { number: "4111...", expiry: "12/25" }
Configuring Different LLM Providers
Choose between different providers:
// Using OpenAI
let openai_client = new?
.model
.temperature
.max_tokens
.build;
// Using Anthropic
let anthropic_client = new?
.model
.temperature
.max_tokens
.build;
Handling Container-Level Attributes
Add metadata and examples at the container level:
)]
π API Reference
Instructor Trait
The Instructor trait is the core of RStructor. It's implemented automatically via the derive macro and provides schema generation and validation:
Override the validate method to add custom validation logic.
LLMClient Trait
The LLMClient trait defines the interface for all LLM providers:
Supported Attributes
Field Attributes
description: Text description of the fieldexample: A single example valueexamples: Multiple example values
Container Attributes
description: Text description of the struct or enumtitle: Custom title for the JSON Schemaexamples: Example instances as JSON objects
π§ Feature Flags
Configure RStructor with feature flags:
[]
= { = "0.1.0", = ["openai", "anthropic"] }
Available features:
openai: Include the OpenAI clientanthropic: Include the Anthropic clientderive: Include the derive macro (enabled by default)
π Examples
See the examples/ directory for complete, working examples:
structured_movie_info.rs: Basic example of getting movie information with validationnested_objects_example.rs: Working with complex nested structures for recipe datanews_article_categorizer.rs: Using enums for categorizationenum_with_data_example.rs: Working with enums that have associated data (tagged unions)event_planner.rs: Interactive event planning with user inputweather_example.rs: Simple model with validation demonstration
βΆοΈ Running the Examples
# Set environment variables
# or
# Run examples
π£οΈ Roadmap
- Core traits and interfaces
- OpenAI backend implementation
- Anthropic backend implementation
- Procedural macro for deriving
Instructor - Schema generation functionality
- Custom validation capabilities
- Support for nested structures
- Rich validation API with custom domain rules
- Support for enums with associated data (tagged unions)
- Streaming responses
- Support for additional LLM providers
- Integration with web frameworks (Axum, Actix)
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π₯ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.