Rust-backed schema inference from Sequence[Mapping] with Pydantic model emission
Project description
infermodel
Rust-backed schema inference from Sequence[Mapping] data (e.g. list of dicts) with Pydantic model emission.
Infer a schema from a sequence of mappings (e.g. list or tuple of dicts), then convert that inferred schema into a Pydantic model on the Python side—without hardcoding schema logic in application code.
Features
- Rust core: Performance-critical traversal, merge logic, and required/nullable tracking
- Python ergonomics: Pydantic v2 model creation via
infer_model(...) - Conservative defaults: Strings stay strings; int+float promotes to float; incompatible mixes become
Any - Required vs nullable: Tracks presence (missing key → optional) and explicit
None(nullable) separately
Installation
From the project root (with a virtualenv activated):
pip install -e .
# or: maturin develop
Requirements: Python 3.10+, Pydantic v2. Build requires Rust (for the extension).
Quick start
from infermodel import infer_schema, infer_model
data = [
{"id": 1, "name": "Alice"},
{"id": 2, "name": None},
{"id": 3}, # missing "name" -> optional
]
# Get introspectable schema dict
schema = infer_schema(data)
# {"type": "model", "fields": {"id": {"type": "int", "required": True, "nullable": False}, ...}}
# Get a Pydantic model class
Model = infer_model(data, model_name="Record")
instance = Model(id=1, name="Alice")
API
-
infer_schema(data, config=None)
Returns a nested dict withtype,fields, and per-fieldtype,required,nullable. -
infer_model(data, model_name="InferredModel", config=None)
Infers the schema and returns a dynamic Pydantic model class. -
InferConfig
Dataclass for policy options (e.g.incompatible_scalar_policy,string_date_policy). V1 uses built-in policies only. -
model_from_schema(schema, model_name="InferredModel")
Build a Pydantic model from an existing schema dict (e.g. frominfer_schema).
Required vs nullable
- Required: field present in every row.
- Optional: field missing in at least one row.
- Nullable: at least one row had explicit
Nonefor that field.
These are independent: a field can be required and nullable, or optional and not nullable.
Development
# Create venv and install in editable mode with Rust extension
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
maturin develop
# Run tests
pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file infermodel-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: infermodel-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 239.0 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7ba8f66aed8f97e358469f445371fa212e5924be23f0e79eade927dcab07033
|
|
| MD5 |
8e3adca94514d827013c208c6f21d8d6
|
|
| BLAKE2b-256 |
9ab352fb0c763cf28adf41b895ab72700d55a64913ea1783c53dcb67c1e0d4f0
|