Expand description
rstructor: get structured, validated data out of LLMs as native Rust structs and enums
§Overview
rstructor gets structured, validated data out of large language models (LLMs) as native Rust structs and enums. It generates JSON Schema from your types, prompts the model, parses the response, and validates the result — retrying automatically when validation fails.
Key features:
- Derive macro for automatic JSON Schema generation
- Built-in clients for OpenAI, Anthropic, Google Gemini, and xAI Grok
- Validation of responses against schemas
- Type-safe conversion from LLM outputs to Rust structs and enums
- Customizable client configurations
§Quick Start
use rstructor::{LLMClient, OpenAIClient, Instructor};
use serde::{Serialize, Deserialize};
#[derive(Instructor, Serialize, Deserialize, Debug)]
struct Person {
name: String,
age: u8,
bio: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client
let client = OpenAIClient::new("your-openai-api-key")?;
// Extract a structured response
let person: Person = client.extract("Describe a fictional person").await?;
println!("Name: {}", person.name);
println!("Age: {}", person.age);
println!("Bio: {}", person.bio);
Ok(())
}Re-exports§
pub use error::ApiErrorKind;pub use error::RStructorError;pub use error::Result;pub use error::StreamErrorKind;pub use model::Instructor;pub use schema::CustomTypeSchema;pub use schema::Schema;pub use schema::SchemaBuilder;pub use schema::SchemaType;
Modules§
- client
- error
- logging
- Logging functionality for rstructor
- model
- schema
- telemetry
- OpenTelemetry-compatible GenAI tracing fields.
Macros§
- impl_
client_ builder_ methods - Macro to generate standard builder methods for LLM clients.
Structs§
- Anthropic
Client - Anthropic client for generating completions
- Attempt
Record - Immutable record of one materialization attempt.
- Chat
Message - A chat message for conversation history.
- Extraction
- A validated value and its complete available extraction report.
- Extraction
Error - A failed extraction and the same report shape returned on success.
- Extraction
Report - Run metadata shared by successful and failed structured extractions.
- Fixture
- A versioned collection of sanitized, ordered request/response interactions.
- Fixture
Recorder - An
LLMClientwrapper that records sanitized non-streaming interactions. - Fixture
Sanitizer - Mandatory privacy boundary used before requests and responses enter a fixture.
- FnTool
- A
Toolbuilt from a closure. - Gemini
Client - Gemini client for generating completions
- Generate
Result - Result of a generate call, containing the text and optional usage information.
- Grok
Client - Grok client for generating completions
- Materialize
Failure - Failed structured-output run with available usage and attempt metadata.
- Materialize
Report - Successful structured-output run with usage and available attempt metadata.
- Materialize
Result - Result of a materialize call, containing both the data and optional usage information.
- Media
File - File reference for media-aware prompts (e.g., Gemini file URI or inline data).
- Mock
Client - An in-memory
LLMClientfor offline tests. - Mock
Request View - A borrowed view of an incoming request, passed to a responder closure so it can
branch on the prompt/target type without cloning. Mirrors
RecordedRequest. - Model
Info - Information about an available model from an LLM provider.
- OpenAI
Client - OpenAI client for generating completions
- Recorded
Request - A single request the mock received, captured for assertions.
- Replay
Client - Strict, ordered, offline replay of a
Fixture. - Request
- A fluent request being built against a client. Created via
RequestExt. - Response
Body Capture - Opt-in, caller-controlled response-body capture.
- Response
Metadata - HTTP metadata retained for one provider response.
- RunUsage
- Cumulative token usage across every provider response in one materialization run.
- Sanitized
Response Body - A response body that has passed through a caller-provided sanitizer.
- Schemars
- Transparent adapter from a schemars model to rstructor’s
Instructor. - Token
Usage - Token usage information from an LLM API call.
- Toolbox
- A collection of tools made available to the model.
Enums§
- Anthropic
Model - Anthropic models available for completion
- AnyClient
- A provider-agnostic client chosen at runtime.
- Attempt
Kind - Whether an attempt reached structured-output validation.
- Attempt
Outcome - Outcome of one materialization attempt.
- Chat
Role - Role of a chat message participant.
- Fixture
Error - File-format and replay-completion errors for fixtures.
- Gemini
Model - Gemini models available for completion
- Grok
Model - Grok models available for completion
- Mock
Response - One scripted reply the mock will hand back for a call.
- OpenAI
Model - OpenAI models available for completion
- Provider
- Identifies an LLM provider for runtime selection via
AnyClient. - Request
Kind - Which
LLMClient(or extension) method produced aRecordedRequest. - Retry
Disposition - Why execution did or did not continue after a failed attempt.
- Streamed
Object - An item yielded by a streaming structured (“object”) request.
- Thinking
Level - Thinking level configuration for models that support extended reasoning.
Constants§
- DEFAULT_
CONNECT_ TIMEOUT - Default timeout for establishing a TCP connection to an LLM provider (30 seconds).
- DEFAULT_
REQUEST_ TIMEOUT - Default timeout for an entire HTTP request to an LLM provider (5 minutes).
- FIXTURE_
SCHEMA_ VERSION - Current on-disk fixture schema version.
Traits§
- DynTool
- Object-safe, type-erased view of a
Tool, used to store heterogeneous tools in aToolbox. Implemented automatically for everyTool. - LLMClient
- LLMClient trait defines the interface for all LLM API clients.
- Request
Ext - Fluent request entry points, available on every
LLMClient. - Tool
- A typed tool the model can call.
Functions§
- client
- Build a client from a
provider/modelstring. - client_
from_ env - Build a client for the first provider whose API key is configured.
- parse_
client_ spec - Parse a
provider/modelclient specification without reading the environment.
Type Aliases§
- Extraction
Result - The advanced structured-extraction result type.
- Item
Stream - A boxed stream of fully-parsed items, one per element of a streamed JSON array.
- Object
Stream - A boxed stream of
StreamedObjectitems for a streaming structured request. - Text
Stream - A boxed stream of text deltas. Each item is either an incremental piece of the model’s text output or a transport/decode error.
Derive Macros§
- Instructor
- Derive macro for implementing Instructor and SchemaType