Documentation
Cli
Reference docs for pydantic-fixturegen.
CLI: drive generation from commands
Learn every command, flag, and default so you can script generation with confidence. Full reference (and the examples below) live at https://pydantic-fixturegen.kitgrid.dev/ — the CLI prints the same URL in every
--helpscreen so you always have a breadcrumb back to the docs.
Global options
pfg --verbose # repeat -v to increase verbosity (info → debug)
pfg --quiet # repeat -q to reduce output (info → warning → error → silent)
pfg --log-json ... # prefix a specific command to emit structured logs; combine with jq for CI parsing
You can append -- --help after any proxy command to view native Typer help because pfg forwards arguments to sub-apps. The footer of every help screen links back to the hosted docs so newcomers can jump straight into walkthroughs and troubleshooting guides.
Command map (cheat sheet)
| Command | Purpose |
|---|---|
pfg list |
Discover models via AST/safe-import hybrids. |
pfg gen json / dataset / fixtures / schema |
Emit JSON/JSONL, CSV/Parquet/Arrow, pytest fixtures, or JSON Schema. |
pfg gen seed sqlmodel/beanie |
Populate SQLModel/SQLAlchemy or Beanie/MongoDB databases. |
pfg gen examples |
Inject deterministic examples into OpenAPI specs. |
pfg gen strategies |
Export Hypothesis strategies wired to strategy_for. |
pfg gen explain |
Visualise generation plans (tree or JSON). |
pfg gen polyfactory |
Scaffold Polyfactory classes that delegate to fixturegen. |
pfg polyfactory migrate |
Analyze Polyfactory factories and emit fixturegen override config. |
pfg fastapi smoke / serve |
Generate FastAPI smoke tests or launch a deterministic mock server. |
pfg anonymize |
Rewrite JSON/JSONL payloads via rule-driven strategies. |
pfg persist |
Stream generated payloads into HTTP/DB/custom handlers. |
pfg diff / check / doctor |
Compare artefacts, validate configs, audit coverage. |
pfg snapshot verify/write |
Verify or refresh stored snapshots outside pytest. |
pfg lock / verify |
Record and enforce coverage manifests in CI. |
pfg init / plugin |
Scaffold config files or custom pluggy providers. |
pfg list
pfg list ./models.py --include pkg.User --public-only
- Lists fully-qualified model names discovered via AST and safe-import by default.
- Use
--astto disable imports,--hybridto combine AST with sandbox inspection, or--timeout/--memory-limit-mbto tune sandbox guards. --json-errorsprints machine-readable payloads with error code20.
pfg gen
gen hosts multiple subcommands. Inspect them with pfg gen -- --help (note the docs URL at the bottom of the help text).
pfg gen json
pfg gen json ./models.py \
--out ./out/users.json \
--include pkg.User \
--n 10 \
--jsonl \
--indent 0 \
--orjson \
--shard-size 1000 \
--seed 42 \
--freeze-seeds \
--preset boundary
--outis required and supports templates ({model},{case_index},{timestamp}).- Control volume with
--n(records) and--shard-size(records per file). - Switch encoding with
--jsonl,--indent,--orjson/--no-orjson. - Determinism helpers:
--seed,--freeze-seeds,--freeze-seeds-file,--preset. - Relations: declare cross-model links with
--link Order.user_id=User.idand co-generate related records with--with-related User,Item(each JSON sample becomes a dict keyed by model name). - TypeAdapter mode: pass
--type "list[EmailStr]"to evaluate a Python type expression viaTypeAdapterwithout discovering a module first. Expressions can reference types from the target module when you also pass the module path, but you cannot combine--typewith--link,--with-related, or--freeze-seeds, and watch mode requires a module path so imports can be refreshed. - Validator enforcement: add
--respect-validatorsto retry on model/dataclass validator failures and--validator-max-retriesto cap the extra attempts. - Privacy bundles:
--profile pii-safemasks identifiers;--profile realisticrestores richer distributions. - Per-field overrides: repeat
--override/-O Model.field='{"value": "demo"}'(or{"factory": "pkg.module:callable"},{"ignore": true}, etc.) to apply the same Use/Ignore/Require/PostGenerated behaviours documented under configuration without touchingpyproject.toml. - Observability:
--json-errors,--watch,--watch-debounce,--now.
pfg gen dataset
pfg gen dataset ./models.py \
--out ./warehouse/users.csv \
--format parquet \
--compression zstd \
--n 1000000 \
--shard-size 200000 \
--include pkg.User \
--seed 7 \
--preset boundary-max
--formatselectscsv,parquet, orarrow. Each honours deterministic seeds and shares the same generation pipeline asgen json.- CSV output streams line-by-line (optionally via
--compression gzip), while Parquet and Arrow use PyArrow writers; install the[dataset]extra orpyarrowto enable columnar formats. --shard-sizesplits high-volume runs across multiple files without buffering the entire dataset in memory; templates with{case_index}apply per shard exactly likegen json.--override/-Oaccepts the same JSON payloads as[tool.pydantic_fixturegen.overrides]so you can pin individual fields, mark them asrequire, or run post-generation hooks for one-off datasets.- Cycle metadata is preserved via the
cyclescolumn so downstream checks can reason about recursion heuristics. - Determinism helpers mirror
gen json:--seed,--freeze-seeds,--preset,--profile,--now,--respect-validators,--validator-max-retries,--max-depth,--on-cycle,--rng-mode. - Observability:
--json-errors,--watch,--watch-debounce, and relation links via--link source.field=target.fieldstay consistent with other emitters.
pfg persist
pfg persist ./models.py \
--handler http-post \
--handler-config '{"url": "https://api.example.com/fixtures", "headers": {"Authorization": "Bearer ..."}}' \
--include app.models.User --n 100 --batch-size 25 --seed 7
- Required
--handler/-Hchooses a registered handler (http-post,http-post-async,sqlite-json, or names defined under[tool.pydantic_fixturegen.persistence.handlers]). Supply dotted paths likepkg.module:KafkaHandlerfor ad-hoc handlers. --handler-configaccepts a JSON object merged into the handler's keyword arguments (URL, headers, database paths, etc.). Configured defaults are merged first, CLI overrides win.--batch-size,--max-retries, and--retry-waitcontrol batching and retry semantics; fixturegen retries failed batches before raisingPersistenceError.- Generation flags mirror
pfg gen json:--include,--exclude,--seed,--preset,--profile,--field-hints,--override,--link,--with-related,--respect-validators,--validator-max-retries,--max-depth,--on-cycle,--rng-mode,--now. - Handler discovery: declare named handlers in config or register them via entry-point plugins (
pfg_register_persistence_handlers). Built-in HTTP/SQLite handlers require no extra dependencies and make for quick experiments. - Logging emits
persistence_batch/persistence_completeevents so you can trace throughput in CI alongside standard JSON logs.
pfg gen seed
pfg gen seed sqlmodel ./models.py \
--database sqlite:///seed.db \
--include app.User \
--n 100 \
--create-schema \
--batch-size 25 \
--truncate
pfg gen seed beanie ./models.py \
--database mongodb://localhost:27017/app \
--include app.Account \
--cleanup
sqlmodelseeds SQLModel/SQLAlchemy tables via transactional sessions. Use--databasefor the engine URL,--create-schemato runSQLModel.metadata.create_all(),--batch-sizeto control flush size,--truncateto clear existing rows,--rollbackto run rollback-only transactions, and--dry-runto log payloads without touching the database.beaniestreams deterministic documents into MongoDB;--cleanupdeletes inserted docs at the end so fixtures can reuse the same collections. Both commands honour--seed,--freeze-seeds,--preset,--profile,--link,--with-related,--respect-validators,--validator-max-retries,--max-depth,--on-cycle, and--rng-mode.- Database URLs are allowlisted by prefix (
sqlite://andmongodb://by default). Pass--allow-url prefix(repeatable) when you really want to point at another environment. - Install the
[seed]extra or the narrower[sqlmodel]/[beanie]extras to pull in SQLModel/SQLAlchemy, Beanie, Motor, and mongomock for local testing.
pfg gen fixtures
pfg gen fixtures ./models.py \
--out tests/fixtures/test_models.py \
--style factory \
--scope module \
--cases 3 \
--return-type dict \
--p-none 0.2 \
--seed 42
--outis required and can use templates.--stylecontrols structure (functions,factory,class).--scopesets fixture scope;--casesparametrises templates.--return-typechooses between returning the model or its dict representation.- Determinism flags mirror
gen json, and--profileapplies the same privacy bundles before fixture emission. - Per-field overrides:
--override/-O Model.field='{"value": "demo"}'(repeatable) honours Use/Ignore/Require/PostGenerated semantics at the CLI level, matching[tool.pydantic_fixturegen.overrides]. - Relations:
--link Order.user_id=User.idkeeps fixtures consistent and--with-related Userensures the related fixtures are emitted in the same module when you need bundles. - Validator enforcement mirrors
gen json:--respect-validatorsapplies bounded retries and--validator-max-retriesadjusts the ceiling.
pfg gen schema
pfg gen schema ./models.py --out ./schema --include pkg.User
- Requires
--outand writes JSON Schema files atomically. - Combine with
--include/--exclude,--json-errors,--watch,--now, and--profilewhen you want schema discovery to evaluate a specific privacy profile.
pfg gen examples
pfg gen examples openapi.yaml \
--route "GET /users" \
--route "POST /orders" \
--out openapi.examples.yaml \
--seed 7 \
--freeze-seeds
- Requires the
openapiextra sodatamodel-code-generatorand PyYAML are available. - Ingests an OpenAPI 3.x spec, isolates the referenced schemas per route, and injects deterministic
exampleblocks into responses and request bodies. --routeis repeatable; omit it to process every route/component. Use--include/--excludefor fine-grained schema control.- Determinism helpers mirror
gen json:--seed,--freeze-seeds,--profile,--respect-validators,--validator-max-retries,--max-depth,--on-cycle,--rng-mode. - Combine with
pfg gen openapiJSON emission to keep SDK examples and test fixtures aligned without hand-crafted payloads.
pfg gen explain
pfg gen explain ./models.py --tree --max-depth 2 --include pkg.User
--treeprints ASCII strategy diagrams.--jsonemits structured data suitable for tooling.- Limit depth with
--max-depth, filter models with--include/--exclude. - Use
--json-errorsfor machine-readable failures.
pfg gen polyfactory
pfg gen polyfactory ./models.py --out tests/polyfactory_factories.py --seed 11 --max-depth 4
- Emits a Python module full of
ModelFactorysubclasses whosebuild()methods delegate to a sharedInstanceGenerator, so existing Polyfactory consumers can migrate gradually while keeping deterministic fixturegen data. - Supports
--include/--exclude,--seed,--max-depth,--on-cycle,--rng-mode, and--watchjust like othergensubcommands. Pass--stdoutto stream the scaffold elsewhere. - Pair with the
[polyfactory]config block: the CLI respectsprefer_delegationand automatically registers any factories it exported the next time you rungen json,gen fixtures, or the FastAPI commands.
pfg polyfactory migrate
pfg polyfactory migrate ./models.py --include app.models.User --overrides-out overrides/polyfactory.toml
- Inspects Polyfactory
ModelFactorysubclasses, reports everyUse/Ignore/Require/PostGeneratedoverride, and shows which fixturegen provider would handle the same field. - Generates ready-to-paste
[tool.pydantic_fixturegen.overrides]snippets using helper adapters so existing callables continue to work after the migration. - Flags unsupported patterns (lambda callables, nested factories, non-serializable values) so you know exactly where manual follow-up is required. See the dedicated guide for more examples.
pfg gen strategies
pfg gen strategies ./models.py \
--include models.User \
--out tests/strategies/test_users.py \
--seed 123 \
--strategy-profile edge \
--stdout
- Exports Hypothesis strategies built on
pydantic_fixturegen.hypothesis.strategy_forso property-based tests reuse the same configuration (seed, RNG mode, cycle policy, presets) as the CLI. --strategy-profiletoggles value distributions (typical,edge,adversarial); combine with--presetand--profilefor complete control.- Add
--stdoutto stream the module directly into other tooling, or point--outat a Python file to commit alongside your tests. - The generated module includes helper functions to reseed strategies on demand, and each exported symbol carries docstrings describing the deterministic settings used.
pfg anonymize
pfg anonymize \
--rules anonymize.toml \
--profile pii-safe \
--entity-field account.id \
--salt rotate-2025-11 \
--report reports/anonymize.json \
--doctor-target app/models.py \
./data/users.json ./sanitized/users.json
--rulespoints to a TOML/YAML/JSON file describing field patterns and strategies (faker/hash/mask). Rules are evaluated in order, can be markedrequired, and inherit presets when you pass--profile.- Supply the destination as a positional argument (shown above) or via
--out/--outputif you prefer an explicit flag; directory inputs mirror their structure under the output directory. Becausepfgproxies commands through the root Typer app, place options before the positional arguments to keep Click happy. - Determinism helpers:
--saltcontrols the hash/key derivation,--entity-fieldpicks a dotted column used to derive stable pseudonyms, and--profilelayers in the same privacy bundles available to generation commands. - Observability:
--reportwrites a JSON summary containing before/after diff samples, per-strategy counts, and privacy budget metrics. Add--doctor-targetto reusepfg doctorgap detection after data is anonymized. - Budgets:
--max-required-missesand--max-rule-failuresoverride the thresholds defined in the rules file/profile so CI can fail fast when sensitive fields slip through unchanged. - Input/output flexibility: accept JSON arrays, standalone objects, JSONL/NDJSON streams, or directory trees (mirrored to the output directory). Every writer preserves determinism, so running the command twice with the same salt/entity key yields identical sanitized payloads.
pfg fastapi
Install the fastapi extra to enable FastAPI tooling.
pfg fastapi smoke
pfg fastapi smoke app.main:app \
--out tests/test_fastapi_smoke.py \
--seed 11 \
--dependency-override auth.get_current_user=fakes.allow_all \
--respect-validators
- Generates a pytest module with one smoke test per route. Each test issues a client request, asserts a 2xx response, and validates the response model using the same deterministic fixture engine.
--dependency-override original=stub(repeatable) bypasses expensive dependencies—perfect for auth/session providers or rate-limiters.- Honour the usual deterministic flags (
--seed,--preset,--profile,--link,--with-related,--max-depth,--on-cycle,--rng-mode) so smoke payloads stay in sync with the rest of your suite.
pfg fastapi serve
pfg fastapi serve app.main:app \
--port 8050 \
--seed 7 \
--host 0.0.0.0 \
--reload
- Spins up a deterministic mock server that mirrors your FastAPI routes but responds with fixture-generated payloads. Ideal for contract-first development, front-end demos, or QA sandboxes.
- Respects the same deterministic config as every other command, and supports Uvicorn flags like
--host,--port, and--reload. - Combine with
--dependency-overrideto stub external services when mocking.
pfg lock
pfg lock \
--lockfile .pfg-lock.json \
--include app.User \
./models.py
- Generates a deterministic coverage manifest (defaults to
.pfg-lock.json) that records discovery options, coverage ratios, provider assignments, and gap summaries pulled frompfg doctor. - Re-run
pfg lockafter intentional model changes; use--forceto overwrite even when nothing changed. Supports the same discovery flags aspfg doctor(--schema,--openapi,--route, etc.). - Because
pfgproxies commands through the root Typer app, place options before the positional module argument as shown above.
pfg verify
pfg verify --lockfile .pfg-lock.json ./models.py
- Recomputes the manifest with the current codebase and compares it to the stored lockfile (ignoring timestamps). Exit code
30indicates drift and prints a unified diff of the JSON payload. - Pair
pfg lockwithpfg verifyin CI or pre-commit hooks to block merges when coverage regresses or new models land without regenerated fixtures. - Pass options before the positional module argument to keep the root proxy satisfied.
pfg snapshot
| Subcommand | Purpose |
|---|---|
pfg snapshot verify |
Regenerate artifacts in-memory and fail with exit code 30 when drift is detected (without updating files). |
pfg snapshot write |
Regenerate artifacts and refresh on-disk snapshots using the same deterministic pipeline. |
pfg snapshot verify ./models.py \
--json-out artifacts/users.json \
--fixtures-out tests/fixtures/test_users.py \
--seed 42 \
--freeze-seeds
pfg snapshot write ./models.py \
--json-out artifacts/users.json \
--fixtures-out tests/fixtures/test_users.py \
--seed 42
- Mirrors
pfg diffoptions (--json-out,--fixtures-out,--schema-out,--include,--exclude,--seed,--preset,--link,--with-related,--respect-validators,--max-depth,--on-cycle,--rng-mode). - Designed for CI/lint workflows where you want “diff-only” vs “update in place” behaviour without invoking the pytest plugin.
- Pair with the pytest helper documented in testing.md to give individual tests opt-in updates (
pytest --pfg-update-snapshots=update) while CI stick topfg snapshot verify.
pfg diff
pfg diff ./models.py \
--json-out out/users.json \
--fixtures-out tests/fixtures/test_models.py \
--schema-out schema \
--show-diff
- Regenerates artifacts in-memory and compares them with existing files.
- Writes JSON summaries when you pass output paths.
--show-diffstreams unified diffs to stdout.- Determinism helpers:
--seed,--freeze-seeds, plus--profileto mirror the privacy bundle used in generation. - Relations:
--link source.field=target.fieldapplies the same linking policy that produced the artifacts so regenerated instances stay in sync. - Validator parity:
--respect-validators/--validator-max-retriesensure diff regeneration matches the validator policy used when the golden artifacts were created.
pfg check
pfg check ./models.py --json-errors --fixtures-out /tmp/fixtures.py
- Validates configuration, discovery, and emitter destinations without writing artifacts.
- Mirrors
diffoutput flags, including--json-out,--fixtures-out, and--schema-out. - Use it in CI to block invalid configs before generation.
pfg init
pfg init \
--pyproject-path pyproject.toml \
--yaml \
--yaml-path config/pydantic-fixturegen.yaml \
--seed 42 \
--union-policy weighted \
--enum-policy random \
--json-indent 2 \
--pytest-style functions \
--pytest-scope module
- Scaffolds configuration files and optional fixture directories.
- Accepts
--no-pyprojectif you only want YAML. - Adds
.gitkeepinsidetests/fixtures/unless you pass--no-fixtures-dir.
pfg plugin
pfg plugin new acme-colorizer \
--namespace acme.plugins \
--distribution acme-pfg-colorizer \
--entrypoint acme-colorizer \
--directory ./acme-colorizer
- Generates a pluggy provider project with
pyproject.toml, README, tests, and GitHub Actions workflow. --namespacebuilds a nested package layout (for examplesrc/acme/plugins/acme_colorizer).- Override packaging metadata with
--distribution,--entrypoint,--description, and--author. - Use
--forceto overwrite existing files when iterating on a scaffold in-place.
Editor integrations
- Workspace tasks and problem matchers for Visual Studio Code live under
.vscode/. - See docs/vscode.md for details on running
pfgcommands directly from the editor with diagnostics surfaced in the Problems panel.
pfg doctor
pfg doctor ./models.py --fail-on-gaps 0 --json-errors
- Audits coverage gaps (fields without providers), risky imports, and sandbox findings.
- Use
--fail-on-gapsto turn warnings into non-zero exits. - Combine with
--include/--excludeto focus on specific models.
pfg schema
pfg schema config --out schema/config.schema.json
- Dumps JSON Schemas describing configuration or model outputs.
- The
configsubcommand wraps the schema bundled underpydantic_fixturegen/schemas/config.schema.json.
Tips for scripting
- Append
--json-errorsanywhere you need machine-readable results; check exit codes for CI gating. - Use
--nowwhen you want reproducible “current time” values in generated data. --preset boundaryor--preset boundary-maxapplies opinionated strategies; combine with explicit overrides to fine-tune probability.- When piping commands, pass
--before flags for subcommands to avoid Typer proxy conflicts.
Continue to output paths for templating and logging for structured events that pair with automation.
Edit this page