From pandas DataFrames to validated field schemas.
MLschema is a Python library for deriving validated field schemas from pandas DataFrames. It reads column names, dtypes, categories and nullability, then validates the inferred fields with Pydantic and returns a JSON-compatible list.
Use the result with MLform, include it in MLbundle, or consume it in your own frontend. Start with built-in inference, refine individual fields, or add domain-specific builders and kinds.
Why infer a field schema?
The data pipeline already knows whether an input is a number, a date or a category. Writing those definitions again in a frontend creates another place for the model and its inputs to drift apart.
MLschema turns that information into a starting contract with explicit backend mappings. Your application supplies product-specific labels, bounds and defaults; the same validation path checks both inferred and customized fields. Inference runs locally and the resulting list is ordinary data, independent of a rendering framework.
Installation
MLschema is pre-1.0. Public contracts may change between minor releases. Requires Python 3.14 (
>=3.14,<3.15); supported pandas 3 and Pydantic 2 versions are installed as dependencies.
uv add mlschema
Or install from PyPI with pip install mlschema.
See the release and upgrade notes when updating an existing integration. Version 0.3 changes the series field contract.
Quick start
Create a DataFrame with the dtypes your inputs should use:
import json
import pandas as pd
from mlschema import infer_schema
frame = pd.DataFrame(
{
"age": [28, 41],
"plan": pd.Categorical(["free", "pro"]),
"active": [True, False],
}
)
fields = infer_schema(frame)
print(json.dumps(fields, indent=2))
The result is a field list, ready to serialize:
[
{"kind": "number", "label": "age", "required": true, "mappedTo": "age", "step": 1.0},
{"kind": "category", "label": "plan", "required": true, "mappedTo": "plan", "options": ["free", "pro"]},
{"kind": "boolean", "label": "active", "required": true, "mappedTo": "active"}
]
Pass it to MLform as the schema's fields property. Your application owns reports, transport and model execution.
Choose your customization
| API | Use it to |
|---|---|
infer_schema(frame) |
Infer fields using the built-in kinds. |
overrides={...} |
Refine a column's label, bounds or other supported properties before validation. |
builders=[...] |
Supply inference logic for an existing kind, before the built-in builders. |
kinds=[kind(model=..., infer=...)] |
Add a strict field model and the builder that produces it. |
with_positional_mappings(fields, feature_names) |
Copy a field list and convert mappings into an explicit model input order. |
For example, refine a label and bounds, then adapt the mappings for a model that consumes a feature vector:
from mlschema import with_positional_mappings
fields = infer_schema(
frame,
overrides={"age": {"label": "Age", "min": 0, "max": 120}},
)
positional = with_positional_mappings(fields, ["plan", "age", "active"])
The age field keeps its displayed label Age; its target changes from "age" to position 1 in the returned copy. The helper rewrites mappings, preserving field order, series columns and one-hot grouping. It leaves the original list unchanged.
All application-facing functions and extension types come from mlschema. Public inference exceptions come from mlschema.core; see the Python API.
What is included?
- Inference for text, numbers, categories, booleans, dates and two-axis series.
- One-hot groups from named binary columns such as
color__redandcolor__blue. - Explicit backend mappings: names for named columns, positions for positional columns.
- Series output with ordered
columns[], compatible with MLform. - Strict field validation, overrides, custom builders and custom kinds.
- A typed, function-first API that runs entirely in your Python process.
DataFrame dtypes matter: categorical options come from category, and date inference accepts specific datetime dtypes. Columns unclaimed by earlier builders reach the text fallback. See the field-kind reference for supported inputs and limits.
Some notes
MLschema owns field inference, validation and explicit mappings. Your application owns rendering, report definitions, model training and execution. The output is a field list, not a JSON Schema document or a complete MLform schema.
Empty DataFrames are rejected, and invalid builder output or overrides fail validation. Inference does not establish that a dataset is suitable for a model. The returned list can contain category values, so treat it as part of your data contract when sharing it.
Documentation
Full documentation lives at docs.mlschema.eshkra.dev.
Try the examples in the Python playground. It runs the repository's Python code in an experimental browser environment; its dependency limits are documented in the playground guide.
Contributing
Python development uses uv, Ruff, Pyright and pytest:
uv sync --locked --group dev
uv run ruff check .
uv run ruff format --check .
uv run pyright
uv run pytest
uv build
The landing page, documentation and playground use Vite+ (vp) with a pnpm workspace. This tooling is separate from the Python package:
vp install --frozen-lockfile
vp run build
Read CONTRIBUTING.md for local development and release setup.
Run vp run dev:playground to serve the local Python playground at http://127.0.0.1:10011. The playground guide covers examples and verification commands.
License
MIT
Metadata
Release files for mlschema 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mlschema-0.3.0.tar.gz | 755.4 kB | Without provenance | Details for mlschema-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mlschema-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for mlschema-0.3.0-py3-none-any.whl |
Total release size: 792.4 kB
Release files / mlschema-0.3.0.tar.gz
| Download URL | mlschema-0.3.0.tar.gz |
|---|---|
| Size | 755.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f201a71ec926f7891e149368f5ddd537652086cedcb2ae0e6cd68bee83800cc
|
|
BLAKE2b-256 checksum How to use checksums |
c3706d5c5e76dd5aff506364acf1e52b76cf93c72f3720a5767e1d96d50ad4e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / mlschema-0.3.0-py3-none-any.whl
| Download URL | mlschema-0.3.0-py3-none-any.whl |
|---|---|
| Size | 37.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
49982ff201781a60b8e63951de6feac03fca1ce4b4c1b9e5a1b6a8895085438d
|
|
BLAKE2b-256 checksum How to use checksums |
dae44230a97e095738c461dece0c2b2829e29291130892588bddd32d0b6cdf05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for mlschema 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.