{"id":850,"library":"dacite","title":"Dacite","description":"Dacite is a Python library that simplifies the creation of data class instances from dictionaries. It focuses on converting raw dictionary data (e.g., from HTTP requests or databases) into robust, type-hinted dataclass objects, leveraging PEP 557 dataclasses. The library is actively maintained, with version 1.9.2 released recently, and receives regular updates including performance improvements and new feature support like generics, forward references, and unions.","status":"active","version":"1.9.2","language":"python","source_language":"en","source_url":"https://github.com/konradhalas/dacite","tags":["dataclass","deserialization","type-conversion","mapping","dictionary"],"install":[{"cmd":"pip install dacite","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary function for converting dictionaries to dataclasses.","symbol":"from_dict","correct":"from dacite import from_dict"},{"note":"Used to customize the conversion process, e.g., for type hooks or case conversion.","symbol":"Config","correct":"from dacite import from_dict, Config"}],"quickstart":{"code":"from dataclasses import dataclass\nfrom dacite import from_dict, Config\n\n@dataclass\nclass User:\n    name: str\n    age: int\n    is_active: bool\n\n@dataclass\nclass Address:\n    street: str\n    city: str\n\n@dataclass\nclass Profile:\n    user: User\n    address: Address\n    preferences: dict\n\ndata = {\n    'user': {'name': 'Jane Doe', 'age': 28, 'is_active': False},\n    'address': {'street': '123 Main St', 'city': 'Anytown'},\n    'preferences': {'theme': 'dark', 'notifications': True}\n}\n\n# Basic conversion\nprofile = from_dict(data_class=Profile, data=data)\nprint(profile)\n# Expected: Profile(user=User(name='Jane Doe', age=28, is_active=False), address=Address(street='123 Main St', city='Anytown'), preferences={'theme': 'dark', 'notifications': True})\n\n# Example with a type hook (e.g., converting all strings to uppercase)\n@dataclass\nclass Item:\n    id: str\n    value: int\n\ndef uppercase_str(s: str) -> str:\n    return s.upper()\n\nitem_data = {'id': 'item-abc', 'value': 100}\nconfig_with_hook = Config(type_hooks={str: uppercase_str})\nitem = from_dict(data_class=Item, data=item_data, config=config_with_hook)\nprint(item)\n# Expected: Item(id='ITEM-ABC', value=100)\n","lang":"python","description":"This quickstart demonstrates how to convert a dictionary into a nested dataclass structure using `from_dict`. It also shows how to use the `Config` object to apply custom type hooks for transformation during the conversion process."},"warnings":[{"fix":"Integrate with a data validation library (e.g., Pydantic, Marshmallow) before passing data to `dacite.from_dict` if validation is required.","message":"Dacite is a data-to-object mapping library, not a data validation library. It primarily focuses on converting dictionaries to dataclass instances based on type hints. For robust data validation, it's recommended to combine Dacite with a dedicated validation library.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure all required dictionary keys are present, or provide default values for optional fields in your dataclass definitions.","message":"By default, Dacite expects all non-optional fields in the target dataclass to have corresponding keys in the input dictionary. If a required field is missing from the input dictionary and does not have a default value in the dataclass, a `MissingValueError` will be raised.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `Config(cast=[YourType])` to enable casting for specific types, or `Config(type_hooks={YourType: lambda v: YourType(v)})` for custom transformations. Note that `cast` works for base types and their subtypes.","message":"Dacite performs basic type checking but does not automatically coerce types by default (e.g., converting a string '123' to an integer 123 for an `int` field), which can lead to `WrongTypeError`. Automatic casting needs to be explicitly enabled.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully define `Union` types to avoid ambiguity or handle `StrictUnionMatchError` if `strict_unions_match` is enabled. Consider the order of types in the Union if multiple types could potentially match the input data without `strict_unions_match`.","message":"When using `typing.Union`, Dacite by default tries to find the first matching type. If `Config(strict_unions_match=True)` is used, it will raise a `StrictUnionMatchError` if more than one type in the Union could match the input data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your dataclass definition accurately reflects all fields you intend to deserialize. If you have genuinely dynamic keys, consider using `dict` types within your dataclass for those sections.","message":"Dacite does not dynamically add fields to dataclasses that are not predefined in the dataclass definition. If your input dictionary contains keys not present in the dataclass, those keys will be ignored during conversion unless `Config(check_types=False)` is used (which is not recommended for type safety).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T20:22:15.220Z","next_check":"2026-06-27T00:00:00.000Z","problems":[{"fix":"Ensure the input data types precisely match the dataclass type hints, or enable casting for specific types using `dacite.Config(cast=[<TypeToCast>])` or provide a `type_hook` for complex conversions. For example, to allow integers for a float field, use `Union[int, float]` or configure casting for floats.","cause":"The type of the input value in the dictionary does not match the expected type hint defined in the dataclass field, and dacite's default behavior does not perform automatic casting.","error":"dacite.exceptions.WrongTypeError: wrong value type for field \"<field_name>\" - should be \"<expected_type>\" instead of value \"<value>\" of type \"<actual_type>\""},{"fix":"Ensure all required fields are present in the input dictionary, or make the dataclass field optional using `typing.Optional[<Type>]` (e.g., `Optional[str]`) or by providing a default value (e.g., `field: str = 'default'`).","cause":"A required field in the dataclass (one without a default value or `Optional` annotation) is not present in the input dictionary provided to `from_dict`.","error":"dacite.exceptions.MissingValueError: missing value for field \"<field_name>\""},{"fix":"Verify that the input data conforms to one of the types in the `Union`. For complex union types, ensure nested structures or values correctly align with one of the union's member types. Sometimes, adjusting the order of types in `Union` or providing specific `type_hooks` can help resolve ambiguities.","cause":"The input value for a field annotated with `typing.Union` does not successfully match any of the types specified within that union.","error":"dacite.exceptions.UnionMatchError: can not match type \"<actual_type>\" to any type of \"<field_name>\" union: <Union[Type1, Type2]>"},{"fix":"Either remove the extraneous keys from the input dictionary, or set `strict=False` in the `dacite.Config` to allow dacite to ignore unexpected keys: `from_dict(MyDataClass, data, config=Config(strict=False))`.","cause":"The `dacite.Config` was set with `strict=True`, but the input dictionary contains keys that are not defined as fields in the target dataclass.","error":"dacite.exceptions.UnexpectedDataError: unexpected keys in input data: <key1>, <key2>"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.9.2","cli_name":"","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","installed_version":"1.9.2","pypi_latest":"1.9.2","is_stale":false,"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.03,"mem_mb":1.7,"disk_size":"17.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.7,"disk_size":"17.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.02,"mem_mb":1.7,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":1.7,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.06,"mem_mb":1.9,"disk_size":"19.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.07,"mem_mb":1.9,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.9,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.05,"mem_mb":1.7,"disk_size":"11.6M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":1.7,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.05,"mem_mb":1.7,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":1.7,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.05,"mem_mb":2,"disk_size":"11.4M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.06,"mem_mb":2,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.05,"mem_mb":1.8,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":1.8,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.03,"mem_mb":1.6,"disk_size":"17.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.6,"disk_size":"17.4M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.02,"mem_mb":1.6,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"dacite","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.6,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":null,"tag_description":null,"results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}