Pydantic Factories

1.17.3 · deprecated · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

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`.

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)}")

view raw JSON →