{"record":{"id":"4909db87b8663ae6","repo":"huggingface/transformers","slug":"missing-required-field-f-name","errorCode":null,"errorMessage":"Missing required field - '{f.name}'","messagePattern":"Missing required field - '(.+?)'","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/transformers/configuration_utils.py","lineNumber":129,"sourceCode":"        dataclass_fields = {f.name for f in fields(cls)}\n        standard_kwargs = {k: v for k, v in kwargs.items() if k in dataclass_fields}\n\n        # We need to call bare `__init__` without `__post_init__` but the `original_init` of\n        # any dataclas contains a call to post-init at the end (without kwargs)\n        if len(args) > 0:\n            raise ValueError(\n                f\"{cls.__name__} accepts only keyword arguments, but found `{len(args)}` positional args.\"\n            )\n\n        for f in fields(cls):  # type: ignore\n            if f.name in standard_kwargs:\n                setattr(self, f.name, standard_kwargs[f.name])\n            elif f.default is not MISSING:\n                setattr(self, f.name, f.default)\n            elif f.default_factory is not MISSING:\n                setattr(self, f.name, f.default_factory())\n            else:\n                raise TypeError(f\"Missing required field - '{f.name}'\")\n\n        # Pass any additional kwargs to `__post_init__` and let the object\n        # decide whether to set the attr or use for different purposes (e.g. BC checks)\n        additional_kwargs = {}\n        for name, value in kwargs.items():\n            if name not in dataclass_fields:\n                additional_kwargs[name] = value\n\n        self.__post_init__(**additional_kwargs)\n\n    cls.__init__ = __init__\n    return cls\n\n\n@dataclass_transform(kw_only_default=True)\n@strict(accept_kwargs=True)\n@dataclass(repr=False)\nclass PreTrainedConfig(PushToHubMixin, RotaryEmbeddingConfigMixin, HeterogeneousConfigMixin):","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/configuration_utils.py#L111-L147","documentation":"TypeError raised by the strict dataclass init wrapper when a required field (no default, no default_factory) was not supplied. The wrapper iterates all declared fields and fails on the first missing mandatory one, naming it explicitly.","triggerScenarios":"Constructing a strict config while omitting a field declared without a default, e.g. MyConfig(hidden_size=768) when num_heads is also required. Also when a typo'd keyword silently lands in additional kwargs instead of the field.","commonSituations":"New config versions adding required fields, breaking older construction code; copy-pasting a partial config from docs; typos (num_head vs num_heads) that bypass field matching and leave the real field unset.","solutions":["Supply the missing field named in the message: MyConfig(hidden_size=768, num_heads=12)","Check for typos in keyword names — a misspelled kwarg does not fill the field it was meant for","If upgrading transformers, diff the config class fields for newly required members"],"exampleFix":"# before\ncfg = MyConfig(hidden_size=768)  # num_heads required\n# after\ncfg = MyConfig(hidden_size=768, num_heads=12)","handlingStrategy":"validation","validationCode":"from dataclasses import fields, MISSING\nrequired = [f.name for f in fields(MyConfig) if f.default is MISSING and f.default_factory is MISSING]\nmissing = [r for r in required if r not in params]\nassert not missing, f'supply required config fields: {missing}'\ncfg = MyConfig(**params)","typeGuard":"def has_all_required(cls, params: dict) -> bool:\n    return all(\n        f.name in params or f.default is not MISSING or f.default_factory is not MISSING\n        for f in fields(cls)\n    )","tryCatchPattern":"try:\n    cfg = MyConfig(**params)\nexcept TypeError as e:\n    m = re.search(r\"Missing required field - '(\\w+)'\", str(e))\n    if m:\n        raise ValueError(f'config needs value for {m.group(1)!r}') from e\n    raise","preventionTips":["Validate required fields before construction when configs come from user input","Watch for kwarg typos — misspelled names neither fill the field nor raise here","Re-check required fields after upgrading transformers"],"tags":["config","dataclass","validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}