{"id":10088,"library":"pydantic-factories","title":"Pydantic Factories","description":"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.","status":"deprecated","version":"1.17.3","language":"en","source_language":"en","source_url":"https://github.com/starlite-api/pydantic-factories","tags":["pydantic","factory","mock","testing","data generation","dataclasses","deprecated","polyfactory-migration"],"install":[{"cmd":"pip install pydantic-factories","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for Pydantic model factories.","package":"pydantic","optional":false}],"imports":[{"symbol":"ModelFactory","correct":"from pydantic_factories import ModelFactory"},{"symbol":"FactoryField","correct":"from pydantic_factories.fields import FactoryField"}],"quickstart":{"code":"from pydantic import BaseModel\nfrom pydantic_factories import ModelFactory\nfrom datetime import date\n\nclass User(BaseModel):\n    id: int\n    name: str\n    email: str\n    birth_date: date\n\n# Define a factory for your Pydantic model\nclass UserFactory(ModelFactory):\n    __model__ = User\n\n# Generate a single instance\nuser_instance = UserFactory.build()\nprint(f\"Generated User: {user_instance}\")\n\n# Generate a batch of instances\nusers_batch = UserFactory.batch(size=3)\nprint(f\"Generated Batch of Users: {len(users_batch)}\")\n","lang":"python","description":"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`."},"warnings":[{"fix":"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.","message":"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`.","severity":"breaking","affected_versions":"All (from 1.17.3 onwards)"},{"fix":"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)`.","message":"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`).","severity":"gotcha","affected_versions":"All"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Define 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.","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.","error":"No factories for type <class 'your_module.YourCustomType'>"},{"fix":"Set `__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.","cause":"This typically occurs with self-referencing Pydantic models where `pydantic-factories` attempts to generate an infinitely nested structure.","error":"RecursionError: maximum recursion depth exceeded while calling a Python object"},{"fix":"Inspect 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.","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`).","error":"pydantic.ValidationError: X validation error for YourModel"}]}