Registry / testing / polyfactory

polyfactory

JSON →
library3.3.0pypypi✓ verified 27d ago

Polyfactory is a versatile Python library for generating mock data, primarily used for testing and development. It supports various data models, including Pydantic, SQLAlchemy, and Dataclasses, offering a flexible API for customization. The current version is 3.3.0, and it maintains an active release cadence with regular updates and feature enhancements.

pip install polyfactory
INSTALL
IMPORT
SIG · POLYFACTORY
P
polyfactory
testingpythonv3.3.0
Install
4.1s avg
Import
567ms
Disk
55MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.575s · 57MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.1s · import 0.559s · 57MB
55MB installed
● package 55MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BaseFactory
from polyfactory import BaseFactory
from polyfactory.factories import ModelFactory

This example demonstrates how to use `ModelFactory` to generate mock Pydantic model instances. `ModelFactory.build()` creates a single instance in memory, while `ModelFactory.batch()` creates a list of instances. For persistence, a `PersistenceStrategy` would be required with `ModelFactory.create()`.

from polyfactory.factories import ModelFactory from pydantic import BaseModel from datetime import date class User(BaseModel): name: str email: str age: int is_active: bool = True joined_date: date # Build a single instance (in-memory, no persistence) user_instance = ModelFactory.build(User) print(f"Generated User: {user_instance.model_dump_json(indent=2)}") # Build multiple instances (in-memory list) users_batch = ModelFactory.batch(User, size=3) print(f"\nGenerated Users Batch (first item): {users_batch[0].model_dump_json(indent=2)}")
Debug
Known issues
breakingThe generic `Factory` class was renamed to `PolyFactory`.
fix
Update imports from `from polyfactory.factories.base import Factory` to `from polyfactory.factories import PolyFactory`.
affects: <3.0.0
breakingThe `create()` method now explicitly requires a persistence strategy.
fix
When using `create()`, you must now pass a persistence strategy (e.g., `SyncPersistenceStrategy`) as an argument. The `use_api_client` parameter was also removed in v3.0.0.
affects: <3.0.0
gotchaConfusion between `build()` and `create()` methods.
fix
`build()` generates an in-memory instance without any persistence, useful for simple object creation. `create()` is designed to persist the generated data and requires a `PersistenceStrategy` to do so (e.g., saving to a database or calling an API).
affects: All
gotchaDefining the target model for generic `PolyFactory` instances.
fix
For generic factories, the `__model__` attribute should be defined as a class variable within the `PolyFactory` subclass itself, rather than being passed as an argument to `build()` or `create()` methods (which is now deprecated for this purpose).
affects: All
gotchaUsing the generic `PolyFactory` when a specific factory type is available.
fix
For Pydantic models, use `ModelFactory`. For Dataclasses, use `DataclassFactory`. These specific factories offer tailored functionality and better integration than configuring `PolyFactory` with `__model__`.
affects: All
Upgrade
Version history
3.3.0latest on PyPI · released Feb 22, 2026
Audit
Dependencies
pydanticoptionalCommonly used for model definition, enabling ModelFactory functionality.
Agent activity
17 hits · last 30 days
node
16
Resources