rydantic 0.7.0

Type-driven validation, JSON Schema, Cap'n Proto, Axum and SQLx models for Rust
Documentation

Rydantic

Validated Rust models, from JSON to HTTP, SQL, and Cap'n Proto.

CI crates.io License: MIT

Documentation · Quick start · Stable API · Homepage

Define a model once. Keep parsing, normalization, validation, and serialization rules next to its fields. Add the integrations your application needs without maintaining a separate validation layer for every boundary.

#[derive(rydantic::Model)]
struct User {
    #[model(trim, min_length = 3)]
    name: String,
    #[model(coerce, ge = 18, le = 120)]
    age: u8,
}

Coding Agent toolkit (P0)

The portable Rydantic Skill includes an exact stable 0.6.0 consumer recipe and adoption guidance. The unpublished CLI provides offline doctor, explain and versioned JSON diagnostics, not validation or code generation. The three-arm evaluation harness keeps harness smoke tests separate from actual Agent trials; no measured Agent improvement is claimed. Nothing is installed or executed automatically.

What you get

Capability What it does
Validated JSON Explicit coercion, string normalization, defaults, nested models, and structured errors.
Serde + JSON Schema Generated serialization/deserialization and input/output schemas from the same model.
HTTP with Axum Validated request extractors, responses, and structured rejections.
SQL with SQLx Model metadata, validated CRUD, queries, and migration planning for SQLite, PostgreSQL, and MySQL.
Native Cap'n Proto Generate schemas and official Rust bindings, or connect an existing protocol.
More model shapes Borrowed strings, enums, generics, recursive models, async validators, and streaming JSON.
RydanticAI Independently versioned rydantic-ai: typed agents and tools, image inputs, structured output streams, resumable graphs, an authenticated three-protocol gateway, and a TypeSafe Jev SDK.

Integrations are opt-in Cargo features. The default derive feature covers the core model API.

Start with a validated model

For the 0.7.0 stable release (verify registry availability before installation):

[dependencies]
rydantic = "=0.7.0"
use rydantic::{Model, SchemaMode};

#[derive(Debug, Model)]
struct User {
    #[model(trim, min_length = 3)]
    name: String,
    #[model(coerce, ge = 18, le = 120)]
    age: u8,
}

fn main() -> Result<(), rydantic::ValidationErrors> {
    let user = User::parse_json(r#"{"name":" Alice ","age":"25"}"#)?;

    assert_eq!(user.name, "Alice");
    assert_eq!(user.age, 25);
    println!("{}", user.dump_json()?);
    // {"age":25,"name":"Alice"}

    let schema = User::json_schema(SchemaMode::Output);
    assert!(schema.is_object());
    Ok(())
}

coerce makes conversion explicit. Invalid fields produce structured errors with locations and codes. Model also generates Serde implementations: deserialization validates input, and serialization revalidates the current object.

Continue with validation rules, model shapes, or JSON Schema.

Choose your integrations

For AI applications, add the separate rydantic-ai crate. It supports Responses, Chat Completions and Anthropic Messages APIs through BASEURL and APIKEY, accepts ordered URL/Base64 image inputs, validates typed outputs and tool arguments, streams partial JSON snapshots and validated list items, and provides graph checkpoints and a gateway that converts between all three protocols with routing and budgets. See the RydanticAI guide and crate README.

Enable features on the version you selected:

# Stable HTTP + PostgreSQL example
rydantic = { version = "=0.7.0", features = ["axum", "postgres"] }
Build with Features Guide
HTTP APIs axum Axum
Relational data sqlite, postgres, or mysql SQL models · Migrations
Binary protocols capnp Generated schemas · Existing schemas
Dates and exact decimals chrono, decimal Dates and Decimal
Runtime JSON Schema dynamic JSON Schema

See the complete feature reference. Automatic Cap'n Proto binding generation also requires the official capnp compiler on PATH.

Stable release 0.7.0

This README describes the 0.7.0 stable release contract: tested settings, partial JSON and domain types, callback hygiene, and conditional Cap’n Proto generation. Runtime and derive use matching 0.7.0 versions, with an exact derive dependency pin.

Source metadata alone does not prove registry availability. Before installing, verify both runtime and derive packages and the release record. Use rydantic = "=0.7.0" for this release. For older 0.6.0 deployments, consult the versioned source and API reference; the live guides follow the current source.

Breaking upgrade from 0.6.0: 0.7.0 removes automatic Model derive insertion inside #[rydantic::capnp_models]. Add an explicit #[derive(rydantic::Model)] to every model struct and enum, including nested models. The release also rejects unsupported custom SQLite index layouts before a table rebuild. Conditional Cap’n Proto definitions produce configuration-specific wire schemas; different feature/target configurations are not automatically wire-compatible. Read the changelog and migration guidance before upgrading.

Breaking changes are allowed in this early-stage library. Application owners are responsible for backups, migration review, and deployment validation. Unsupported SQLite layouts need a reviewed migration, not removal of constraints to bypass the guard. We do not describe destructive changes as lossless.

Know the boundaries

  • JSON Schema cannot express every normalization, coercion, or custom-validation rule. Schema validation and model validation are not interchangeable.
  • Reusing models reduces repeated rules; it does not replace separate public, internal, and storage models when their contracts differ.
  • Native Cap'n Proto bindings use concrete, owned models. Keep protocol IDs and ordinals stable when wire compatibility matters.
  • capnp_models evaluates cfg and cfg_attr in the consuming crate and generates only the active configuration. Conditional fields or variants can change wire layouts; a shared protocol ID does not make different configurations compatible. See the conditional-compilation boundaries before deploying different builds.

Explore and contribute

The workspace contains the rydantic runtime, rydantic-derive macros, and executable integration fixtures. Browse examples for tool arguments, HTTP, databases, and binary protocols.

cargo run -p rydantic --example user
cargo run -p rydantic --example tool_call -- --schema
cargo test --workspace --all-features --locked
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
cargo fmt --all -- --check

Use Rust 1.98.1 to match the repository's tested toolchain, and install the official Cap'n Proto compiler for all-feature checks. Database integration tests require their configured services; see CI for the full matrix. To work on the site, see documentation development.

Found a bug? Open an issue with a minimal model, input, expected behavior, and crate version. Small fixes with regression tests are welcome.


MIT license