rstructor/lib.rs
1//! rstructor: get structured, validated data out of LLMs as native Rust structs and enums
2//!
3//! # Overview
4//!
5//! rstructor gets structured, validated data out of large language models (LLMs) as native
6//! Rust structs and enums. It generates JSON Schema from your types, prompts the model, parses
7//! the response, and validates the result — retrying automatically when validation fails.
8//!
9//! Key features:
10//! - Derive macro for automatic JSON Schema generation
11//! - Built-in clients for OpenAI, Anthropic, Google Gemini, and xAI Grok
12//! - Validation of responses against schemas
13//! - Type-safe conversion from LLM outputs to Rust structs and enums
14//! - Customizable client configurations
15//!
16//! # Quick Start
17//!
18//! ```no_run
19//! use rstructor::{LLMClient, OpenAIClient, Instructor};
20//! use serde::{Serialize, Deserialize};
21//!
22//! #[derive(Instructor, Serialize, Deserialize, Debug)]
23//! struct Person {
24//! name: String,
25//! age: u8,
26//! bio: String,
27//! }
28//!
29//! #[tokio::main]
30//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
31//! // Create a client
32//! let client = OpenAIClient::new("your-openai-api-key")?;
33//!
34//! // Generate a structured response
35//! let person: Person = client.materialize("Describe a fictional person").await?;
36//!
37//! println!("Name: {}", person.name);
38//! println!("Age: {}", person.age);
39//! println!("Bio: {}", person.bio);
40//!
41//! Ok(())
42//! }
43//! ```
44// Let the crate refer to itself as `rstructor` so `#[derive(Instructor)]` — which
45// emits absolute `::rstructor::…` paths — works in the crate's own unit tests.
46extern crate self as rstructor;
47
48mod backend;
49pub mod error;
50#[cfg(feature = "logging")]
51pub mod logging;
52pub mod model;
53pub mod schema;
54
55// Re-exports for convenience
56pub use error::{ApiErrorKind, RStructorError, Result};
57pub use model::Instructor;
58pub use schema::{CustomTypeSchema, Schema, SchemaBuilder, SchemaType};
59
60#[cfg(feature = "openai")]
61pub use backend::openai::{Model as OpenAIModel, OpenAIClient};
62
63#[cfg(feature = "anthropic")]
64pub use backend::anthropic::{AnthropicClient, AnthropicModel};
65
66#[cfg(feature = "gemini")]
67pub use backend::gemini::{GeminiClient, Model as GeminiModel};
68
69#[cfg(feature = "grok")]
70pub use backend::grok::{GrokClient, Model as GrokModel};
71
72#[cfg(feature = "derive")]
73pub use rstructor_derive::Instructor;
74
75pub use backend::LLMClient;
76pub use backend::ModelInfo;
77pub use backend::ThinkingLevel;
78#[cfg(feature = "_client")]
79pub use backend::{AnyClient, Provider, Request, RequestExt};
80pub use backend::{
81 ChatMessage, ChatRole, GenerateResult, MaterializeResult, MediaFile, TokenUsage,
82};
83#[cfg(feature = "_client")]
84pub use backend::{DEFAULT_CONNECT_TIMEOUT, DEFAULT_REQUEST_TIMEOUT};
85#[cfg(feature = "tools")]
86pub use backend::{DynTool, FnTool, Tool, ToolRunner, Toolbox};
87#[cfg(feature = "streaming")]
88pub use backend::{ItemStream, ObjectStream, StreamedObject, TextStream};
89#[cfg(feature = "mock")]
90pub use backend::{MockClient, MockRequestView, MockResponse, RecordedRequest, RequestKind};