Pydantic Factories (pydantic-factories) is a Python library designed to generate mock data for Pydantic models and Python dataclasses. Its features include custom field builders, nested model support, and type-safe data generation, making it suitable for testing and prototyping. Version 1.17.3 is the final release; all future development and maintenance have transitioned to the `polyfactory` library.
pip install pydantic-factoriesVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining a simple Pydantic model and creating a corresponding `ModelFactory` to generate single instances or batches of mock data. This is the primary usage pattern for `pydantic-factories`.
Install `polyfactory` (`pip install polyfactory`) and refactor your code. While the APIs are similar, `polyfactory` offers enhanced features and better maintenance. Refer to the `polyfactory` documentation for migration guidance.
Explicitly define generation parameters using `FactoryField` on the model field, or define custom builders within your factory to ensure generated data conforms to your Pydantic model's validation rules. Example: `name: str = FactoryField(min_length=5, max_length=20)`.
Provide a custom builder method on your `ModelFactory` for the specific type, or use `FactoryField(callable=...)` on the Pydantic field definition to tell the factory how to generate that type.