Getting Started
A flexible, schema-driven fake data generator built on top of Pydantic v2.
Documentation · Github · PyPI
Generate valid fake data for your Pydantic models with ease. Perfect for testing, prototyping, and anywhere you need valid mock data.
⚡ Quick Example
from typing import Annotated, List, Set, Literal
from pydantic import BaseModel, Field
from pyfake import fake
from rich import print
class Playlist(BaseModel):
track_ids: List[int]
genre: Literal["rock", "pop", "jazz"]
tags: Annotated[List[str], Field(min_length=2, max_length=5)]
unique_ratings: Set[int]
result = fake(Playlist, as_dict=True)
print(result)
{
"track_ids": [28, 25, 95, 40],
"genre": "pop",
"tags": ["CJKHILHXTN", "qkhhjDJYiV"],
"unique_ratings": {17, 49}
}
✨ Why Pyfake?
| Problem | Most fake data generators | Pyfake |
|---|---|---|
| Random but not structured | ❌ Generates random data without understanding the schema | ✅ Reads your Pydantic models to produce structured, schema-aware data |
| Structured but not valid | ❌ Generates strings for emails, URLs or IP addresses that fail validation | ✅ Format-aware generators for EmailStr, HttpUrl, DSNs, IP addresses, UUIDs and regex patterns |
| Support for constraints | ❌ Ignores field constraints like min_length, gt, multiple_of |
✅ Respects Pydantic constraints: bounds, lengths, multiple_of, max_digits/decimal_places, pattern, and conint/constr-style types |
| Support for python types | ❌ Limited support for complex types like Decimal, UUID, datetime |
✅ Supports Decimal, UUID, datetime, timedelta, bytes, enums, literals, nested and self-referencing models, and more |
| Clear failures | ❌ Silently returns invalid data | ✅ Raises an error that names the field when a type or constraint can't be satisfied |
| Reproducibility | ❌ No built-in way to generate the same fake data across runs | ✅ Supports seeding for reproducible fake data generation |
Installation
pyfake is a lightweight and flexible fake data generation library built for developers, data scientists, and testers. You can install it using your preferred Python package manager.
uv is a fast Python package manager and installer. It is the recommended way to install pyfake for better performance and dependency resolution.
# Add pyfake to your project
uv add pyfake
Non-project environment
If you're working in a non-project environment, you can use uv to install pyfake directly:
uv pip install pyfake
💡
uvis significantly faster than pip and handles virtual environments automatically.
You can install pyfake using the standard Python package manager:
pip install pyfake
To install a specific version:
pip install pyfake==0.0.x
If you're working in a virtual environment (recommended):
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install pyfake
If you want the latest development version or plan to contribute, you can install directly from GitHub:
pip install git+https://github.com/Mukhopadhyay/pyfake.git
For a local development
If you want to clone the repository and install in editable mode for development:
git clone https://github.com/Mukhopadhyay/pyfake.git
cd pyfake
pip install -e .
This installs pyfake in editable mode, so changes to the source code are reflected immediately.
Requirements
- Python 3.10+
- Pydantic 2.12+ (installed with the
emailandtimezoneextras) - NumPy 2.2+
How It Works
Pyfake reads your Pydantic schema and:
- Inspects field types and constraints
- Picks a generator for each field (realistic names and addresses are on the roadmap)
- Validates the result with your model, so every value it returns is valid
flowchart LR
A[Pydantic Model] --> B[Schema Parser]
B --> C[Generator Engine]
C --> D[Validated Fake Data]
Read more about the design and implementation in the how it works section.