Skip to main content

Template-driven synthetic data generation engine.

Project description

pysynthgen

Template-driven synthetic data generation engine.

pysynthgen takes a validated JSON template describing a dataset and yields synthetic rows as an Iterator[dict], writing them to a configurable sink (JSON, CSV, Parquet, or Avro).

Design

  • Engine is standalone and dependency-light. It takes a validated template and returns an iterator of rows; the heavy format dependencies are optional extras.
  • Templates are validated with Pydantic, not raw dict parsing. Each field type is its own model in a discriminated union keyed on type, so validation is type-specific and mistakes are caught up front.
  • Generation is seeded and reproducible. A single seed at the template level drives all randomness (numeric, Faker, regex), so a run is byte-for-byte repeatable.
  • Streaming = chunked generation. The engine exposes iter_rows() and iter_batches(batch_size); the consumer (a sink or a file writer) decides how to persist batches.
  • Relationships are out of scope for now but the schema is shaped to grow into multi-table generation without a rewrite (a future entities: {name: template} wrapper, and cross-table reference fields).

Template format

{
  "row_count": 100000,
  "seed": 42,
  "fields": [
    {"name": "user_id", "type": "uuid"},
    {"name": "signup_date", "type": "date", "start": "2023-01-01", "end": "2026-01-01"},
    {"name": "age", "type": "int", "distribution": "normal", "mean": 35, "stddev": 10, "min": 18},
    {"name": "country", "type": "category", "values": ["US", "NL", "DE"], "weights": [0.5, 0.3, 0.2]},
    {"name": "email", "type": "faker", "provider": "email"},
    {"name": "sku", "type": "regex", "pattern": "[A-Z]{3}-\\d{4}"},
    {"name": "referrer_id", "type": "reference", "field": "user_id", "null_probability": 0.7}
  ],
  "constraints": [
    {"type": "unique", "fields": ["user_id"]}
  ]
}

Field types

type Produces Key params
uuid UUIDv4 string
date date between bounds start, end
datetime datetime between bounds start, end
int integer distribution (uniform/normal), min/max or mean/stddev
float float same as int
category value from a set values, optional weights (sum to 1)
faker Faker output provider (e.g. email), optional max_length
regex string matching a pattern pattern, optional max_length
reference copy of an earlier field in the same row field

String-producing fields (faker, regex) accept an optional max_length that truncates the generated value. For regex, truncation may break the pattern match.

Every field also accepts null_probability (0–1): the chance of emitting null for that field on a given row.

Constraints

  • unique — the listed field(s) form a unique key across generated rows.

Usage

from pysynthgen import load_and_validate_template, SynthEngine, build_sink

spec = load_and_validate_template("template.json")
engine = SynthEngine(spec)

# iterate rows directly
for row in engine.iter_rows():
    ...

# or write the whole dataset to a sink in one call (batches internally)
sink = build_sink("parquet", "out.parquet")
path = sink.write(engine.iter_rows())  # -> "out.parquet"

# for finer control, drive the batches yourself
sink = build_sink("parquet", "out.parquet")
for batch in engine.iter_batches(batch_size=10_000):
    sink.write_batch(batch)
path = sink.finalize()

Command line:

python -m pysynthgen template.json                     # validate + echo the normalized spec
python -m pysynthgen template.json --rows 20           # print 20 sample rows as JSON
python -m pysynthgen template.json --out data.parquet  # generate full dataset to a file
python -m pysynthgen template.json --out data --format avro

Sinks

A sink consumes rows in batches and writes one output artifact. All sinks implement write_batch(rows) / finalize() -> path, and build_sink(format, path) selects one by name.

format extension dependency
json .json stdlib (streamed JSON array)
csv .csv stdlib (delimiter/quotechar configurable)
parquet .parquet, .pq pyarrowpysynthgen[parquet]
avro .avro fastavropysynthgen[avro]

Notes: parquet/avro infer their schema from the first batch with all columns nullable; avro writes naive datetimes as UTC so output is deterministic across machines. Install both format deps with pysynthgen[all].

Benchmarks

benchmarks/bench_sinks.py streams a wide dataset into each sink format and reports throughput, output size, and peak resident memory sampled while writing. Because the engine yields rows lazily and sinks write in batches, memory stays flat as row count grows — a multi-GB file is written with tens of MB of RSS.

python benchmarks/bench_sinks.py --rows 200000          # quick pass
python benchmarks/bench_sinks.py --rows 10000000        # full ~10M-row target (slow, multi-GB)
python benchmarks/bench_sinks.py --rows 1000000 --formats parquet avro --keep

Development

This repo uses uv:

uv venv
uv pip install -e ".[dev]"
uv run pytest        # tests
uv run ruff check .  # lint
uv run mypy src      # types

Contributing & releases

Development happens on short-lived branches merged into main via squash-merge, with Conventional Commit PR titles. Merging to main runs an automated pipeline that computes the next semantic version, tags a release, and publishes to PyPI. See CONTRIBUTING.md.

Status

  • ✅ Template schema + loader (Pydantic, validated)
  • ✅ Generation engine — generators + SynthEngine
  • ✅ File sinks — json, csv, parquet, avro
  • ⬜ More sinks (DB table, Kafka)
  • ⬜ Multi-table / linked-entity generation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysynthgen-0.1.2.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysynthgen-0.1.2-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file pysynthgen-0.1.2.tar.gz.

File metadata

  • Download URL: pysynthgen-0.1.2.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pysynthgen-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0228c1f42df8a240da95ee5159cff40dc6c1d531bf945d79fc20bb7ab69dceb2
MD5 2795063759630cc1af2ad5fd8e394fc4
BLAKE2b-256 6e943649c778740bfebd286891e482a0c5a6130373ca3d6da5c5736fee4548c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysynthgen-0.1.2.tar.gz:

Publisher: release.yml on mohsensafari/pysynthgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pysynthgen-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pysynthgen-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pysynthgen-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8efec59df34dcbe5948111b0acfbdd3b435c9442392111627c4eb6fe579fb4ad
MD5 6b23029d2846770f619541d364b41d0f
BLAKE2b-256 565b4ffb93e1b912132b6c9983f71e5809bbe346757aa70afb53402761ad80a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysynthgen-0.1.2-py3-none-any.whl:

Publisher: release.yml on mohsensafari/pysynthgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page