Pydantic Factories
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.
Common errors
-
No factories for type <class 'your_module.YourCustomType'>
cause The `ModelFactory` does not know how to generate data for a custom type used in your Pydantic model because no explicit builder or `FactoryField` was provided.fixDefine a custom builder for `YourCustomType` directly on your `ModelFactory` class, or use `FactoryField(callable=your_generation_function)` on the Pydantic model field where `YourCustomType` is used. -
RecursionError: maximum recursion depth exceeded while calling a Python object
cause This typically occurs with self-referencing Pydantic models where `pydantic-factories` attempts to generate an infinitely nested structure.fixSet `__recursion_limit__` on your `ModelFactory` to a positive integer (e.g., `__recursion_limit__ = 2`) to limit the depth of recursion, or strategically use `FactoryField` to break the recursive generation path. -
pydantic.ValidationError: X validation error for YourModel
cause The mock data generated by `pydantic-factories` failed to pass the Pydantic validation rules defined in your model (e.g., `min_length`, `max_length`, `ge`, `le`, `regex`).fixInspect the `ValidationError` details to identify which field failed. Then, adjust your `ModelFactory` (or use `FactoryField`) to ensure the generated values satisfy the Pydantic field's constraints. For example, if a `str` field has `min_length=10`, ensure your factory generates strings of at least 10 characters.
Warnings
- breaking The `pydantic-factories` library is no longer actively maintained. Version 1.17.3 is the final release, and users are strongly advised to migrate to its successor, `polyfactory`.
- gotcha Default generators for basic types (e.g., `str`, `int`, `float`) might produce values that violate specific Pydantic model constraints (e.g., `min_length`, `ge`, `le`, `regex`).
- gotcha Generating data for complex or custom types (e.g., a custom class not derived from Pydantic `BaseModel` or `dataclass`) without explicit instructions will result in errors.
Install
-
pip install pydantic-factories
Imports
- ModelFactory
from pydantic_factories import ModelFactory
- FactoryField
from pydantic_factories.fields import FactoryField
Quickstart
from pydantic import BaseModel
from pydantic_factories import ModelFactory
from datetime import date
class User(BaseModel):
id: int
name: str
email: str
birth_date: date
# Define a factory for your Pydantic model
class UserFactory(ModelFactory):
__model__ = User
# Generate a single instance
user_instance = UserFactory.build()
print(f"Generated User: {user_instance}")
# Generate a batch of instances
users_batch = UserFactory.batch(size=3)
print(f"Generated Batch of Users: {len(users_batch)}")