{"id":1631,"library":"polyfactory","title":"Polyfactory","description":"Polyfactory is a versatile Python library for generating mock data, primarily used for testing and development. It supports various data models, including Pydantic, SQLAlchemy, and Dataclasses, offering a flexible API for customization. The current version is 3.3.0, and it maintains an active release cadence with regular updates and feature enhancements.","status":"active","version":"3.3.0","language":"python","source_language":"en","source_url":"https://github.com/litestar-org/polyfactory","tags":["mocking","testing","data generation","factories","pydantic","sqlalchemy","dataclasses"],"install":[{"cmd":"pip install polyfactory","lang":"bash","label":"Core library"},{"cmd":"pip install 'polyfactory[pydantic]'","lang":"bash","label":"With Pydantic support (common)"}],"dependencies":[{"reason":"Commonly used for model definition, enabling ModelFactory functionality.","package":"pydantic","optional":true}],"imports":[{"wrong":"from polyfactory.factories import ModelFactory","symbol":"BaseFactory","correct":"from polyfactory import BaseFactory"}],"quickstart":{"code":"from polyfactory.factories import ModelFactory\nfrom pydantic import BaseModel\nfrom datetime import date\n\nclass User(BaseModel):\n    name: str\n    email: str\n    age: int\n    is_active: bool = True\n    joined_date: date\n\n# Build a single instance (in-memory, no persistence)\nuser_instance = ModelFactory.build(User)\nprint(f\"Generated User: {user_instance.model_dump_json(indent=2)}\")\n\n# Build multiple instances (in-memory list)\nusers_batch = ModelFactory.batch(User, size=3)\nprint(f\"\\nGenerated Users Batch (first item): {users_batch[0].model_dump_json(indent=2)}\")","lang":"python","description":"This example demonstrates how to use `ModelFactory` to generate mock Pydantic model instances. `ModelFactory.build()` creates a single instance in memory, while `ModelFactory.batch()` creates a list of instances. For persistence, a `PersistenceStrategy` would be required with `ModelFactory.create()`."},"warnings":[{"fix":"Update imports from `from polyfactory.factories.base import Factory` to `from polyfactory.factories import PolyFactory`.","message":"The generic `Factory` class was renamed to `PolyFactory`.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"When using `create()`, you must now pass a persistence strategy (e.g., `SyncPersistenceStrategy`) as an argument. The `use_api_client` parameter was also removed in v3.0.0.","message":"The `create()` method now explicitly requires a persistence strategy.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"`build()` generates an in-memory instance without any persistence, useful for simple object creation. `create()` is designed to persist the generated data and requires a `PersistenceStrategy` to do so (e.g., saving to a database or calling an API).","message":"Confusion between `build()` and `create()` methods.","severity":"gotcha","affected_versions":"All"},{"fix":"For generic factories, the `__model__` attribute should be defined as a class variable within the `PolyFactory` subclass itself, rather than being passed as an argument to `build()` or `create()` methods (which is now deprecated for this purpose).","message":"Defining the target model for generic `PolyFactory` instances.","severity":"gotcha","affected_versions":"All"},{"fix":"For Pydantic models, use `ModelFactory`. For Dataclasses, use `DataclassFactory`. These specific factories offer tailored functionality and better integration than configuring `PolyFactory` with `__model__`.","message":"Using the generic `PolyFactory` when a specific factory type is available.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'3.3.0':38 'activ':43 'api':31 'cadenc':45 'current':35 'custom':33 'data':11,21,54 'dataclass':27,59 'develop':17 'enhanc':51 'factori':56 'featur':50 'flexibl':30 'generat':9,55 'includ':23 'librari':7 'maintain':41 'mock':10,52 'model':22 'offer':28 'polyfactori':1,2 'primarili':12 'pydant':24,57 'python':6 'regular':47 'releas':44 'sqlalchemi':25,58 'support':19 'test':15,53 'updat':48 'use':13 'various':20 'versatil':5 'version':36","created_at":"2026-04-09T03:56:24.884387+00:00","updated_at":"2026-04-16T18:06:21.333484+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.3.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/litestar-org/polyfactory","docs":"https://github.com/litestar-org/polyfactory","changelog":"https://github.com/litestar-org/polyfactory/releases/","pypi":"https://pypi.org/project/polyfactory/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-08-27","next_check":"2026-07-30","install_tag":null}}