Skip to content
/ dhi Public

⚡ Ultra-fast data validation for Python & TypeScript. Drop-in Pydantic/Zod replacement powered by Zig + SIMD WASM.

License

Notifications You must be signed in to change notification settings

justrach/dhi

Repository files navigation

dhi logo

dhi

136x faster than Pydantic. 20x faster than Zod. Same API.

One validation core. Three ecosystems. Zero compromise.

npm PyPI MIT

Python:     27,300,000 validations/sec (136x faster than Pydantic)
TypeScript: 20x faster than Zod 4 (avg), up to 50x on number formats
Zig:        Zero-cost. Comptime. No runtime.

You don't have to change your code.

Python — drop-in Pydantic replacement

from dhi import BaseModel, Field, EmailStr
from typing import Annotated

class User(BaseModel):
    name: Annotated[str, Field(min_length=1, max_length=100)]
    email: EmailStr
    age: Annotated[int, Field(gt=0, le=150)]

user = User(name="Alice", email="alice@example.com", age=28)
user.model_dump()   # {'name': 'Alice', 'email': 'alice@example.com', 'age': 28}

TypeScript — drop-in Zod 4 replacement

- import { z } from 'zod';
+ import { z } from 'dhi/schema';
const User = z.object({
  id: z.string().uuid(),
  name: z.string().min(1).max(100),
  email: z.email(),              // Zod 4 top-level shortcuts
  age: z.int().positive(),       // Zod 4 number formats
  createdAt: z.iso.datetime(),   // Zod 4 ISO namespace
});

const user = User.parse(data); // same API, 20x faster

Zig — compile-time validated, zero overhead

const dhi = @import("model");

const User = dhi.Model("User", .{
    .name = dhi.Str(.{ .min_length = 1, .max_length = 100 }),
    .email = dhi.EmailStr,
    .age = dhi.Int(i32, .{ .gt = 0, .le = 150 }),
});

const user = try User.parse(.{
    .name = "Alice",
    .email = "alice@example.com",
    .age = @as(i32, 28),
});
// Validation is inlined at compile time. Zero allocations. Zero dispatch.

Same validation rules. Same error behavior. Three languages. One core.


The Numbers

TypeScript (vs Zod 4)

dhi vs Zod Performance

Category Speedup vs Zod 4
Number Formats 30-50x faster
StringBool 32x faster
Coercion 23-56x faster
String Formats 12-27x faster
ISO Formats 12-22x faster
Objects 4-7x faster
Arrays 8x faster

Benchmarks auto-generated via CI. See raw data.

Python

Library Throughput vs dhi
dhi 27.3M/sec
satya (Rust + PyO3) 9.6M/sec 2.8x slower
msgspec (C) 8.7M/sec 3.1x slower
Pydantic v2 0.2M/sec 136x slower

BaseModel layer: 546K model_validate/sec | 6.4M model_dump/sec


Install

pip install dhi          # Python (wheels for macOS arm64 + Linux x86_64)
npm install dhi          # TypeScript (Node 18+ / Bun / Deno)

Pure Python fallback included — no native extension required to get started.


Full Zod 4 Feature Parity

dhi implements 100% of the Zod 4 API, including all new Zod 4 features:

Top-Level String Format Shortcuts (New in Zod 4)

z.email()      z.uuid()       z.url()        z.ipv4()       z.ipv6()
z.jwt()        z.nanoid()     z.ulid()       z.cuid()       z.cuid2()
z.base64()     z.e164()       z.mac()        z.cidrv4()     z.cidrv6()
z.hex()        z.hostname()   z.hash('sha256')

ISO Namespace & Number Formats (New in Zod 4)

// ISO namespace
z.iso.datetime()  z.iso.date()  z.iso.time()  z.iso.duration()

// Number formats
z.int()     z.float()    z.float32()   z.float64()
z.int8()    z.uint8()    z.int16()     z.uint16()
z.int32()   z.uint32()   z.int64()     z.uint64()

Additional Zod 4 Features

z.stringbool()                           // "true"/"yes"/"1" → true
z.templateLiteral(['user-', z.number()]) // Template literal types
z.json()                                 // Recursive JSON schema
z.file().mime('image/png').max(5_000_000) // File validation
z.registry()                             // Schema registry
z.prettifyError(error)                   // Pretty error formatting

80+ Pydantic-compatible types

Category Types
Model BaseModel, Field(), @field_validator, @model_validator
Numeric PositiveInt, NegativeFloat, FiniteFloat, conint(), confloat()
String EmailStr, constr(), pattern, length, strip/lower/upper transforms
Network HttpUrl, AnyUrl, PostgresDsn, RedisDsn, MongoDsn, +8 DSN types
Special UUID4, FilePath, Base64Str, Json, ByteSize, SecretStr
Datetime PastDate, FutureDate, AwareDatetime, NaiveDatetime
Constraints Gt, Ge, Lt, Le, MultipleOf, MinLength, MaxLength, Pattern

Full model_validate(), model_dump(), model_dump_json(), model_json_schema(), model_copy() support.


Why is it this fast?

dhi is written in Zig — a systems language with compile-time code generation, no garbage collector, and direct hardware access. The same source compiles to:

Target What it does
libsatya.dylib/.so Python C extension — extracts from dicts, no copies
dhi.wasm (28KB) TypeScript — 128-bit SIMD, JIT-compiled schemas
Native .zig import Zig — zero-cost comptime validation, fully inlined

Key tricks:

  • Comptime models — Validation logic is generated at compile time. No vtables, no reflection, no hash lookups.
  • SIMD batch validation — Process 4 values per cycle on 256-bit vectors.
  • Single FFI call — Python batch validation crosses the FFI boundary once, not per-item.
  • No allocations — The happy path never allocates. Errors are stack-returned.
┌─────────────────────────────────────────────┐
│         Zig Core (comptime + SIMD)          │
└──────┬────────────────┬────────────────┬────┘
       │                │                │
  ┌────▼─────┐    ┌────▼─────┐    ┌────▼─────┐
  │  Python  │    │   WASM   │    │   Zig    │
  │  C ext   │    │  28KB    │    │  Native  │
  └────┬─────┘    └────┬─────┘    └────┬─────┘
       │                │                │
  BaseModel        z.object()       Model()
  Pydantic API     Zod 4 API      comptime API

Run the benchmarks yourself

# Python
git clone https://github.com/justrach/dhi-zig.git && cd dhi-zig
cd python-bindings && pip install -e . && python benchmark_batch.py

# TypeScript
cd js-bindings && bun install && bun run benchmark-vs-zod.ts

# Zig
zig build bench -Doptimize=ReleaseFast

The Experiment

dhi started as a question: can Zig's type system unify validation across language boundaries?

The hypothesis: Zig's comptime can generate the same validation semantics that Pydantic builds with metaclasses and Zod builds with method chains — but at compile time, with zero runtime cost, targeting any platform via its C ABI and WASM backends.

Results:

Claim Status
Pydantic-level DX in Zig Model("User", .{ .name = Str(.{}) }) — yes
One core, three ecosystems Python FFI + WASM + native Zig — yes
10-100x faster 136x (Python), 20x avg (TypeScript) — yes
Reasonable binary size 28KB WASM, ~200KB native — yes
Comptime replaces reflection No runtime type inspection needed — yes

License

MIT


dhi — the fastest validation library for every language you use.

About

⚡ Ultra-fast data validation for Python & TypeScript. Drop-in Pydantic/Zod replacement powered by Zig + SIMD WASM.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •