huggingface/transformers · error · TypeError
Missing required field - '{f.name}'
Error message
Missing required field - '{f.name}' What it means
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.
Source
Thrown at src/transformers/configuration_utils.py:129
dataclass_fields = {f.name for f in fields(cls)}
standard_kwargs = {k: v for k, v in kwargs.items() if k in dataclass_fields}
# We need to call bare `__init__` without `__post_init__` but the `original_init` of
# any dataclas contains a call to post-init at the end (without kwargs)
if len(args) > 0:
raise ValueError(
f"{cls.__name__} accepts only keyword arguments, but found `{len(args)}` positional args."
)
for f in fields(cls): # type: ignore
if f.name in standard_kwargs:
setattr(self, f.name, standard_kwargs[f.name])
elif f.default is not MISSING:
setattr(self, f.name, f.default)
elif f.default_factory is not MISSING:
setattr(self, f.name, f.default_factory())
else:
raise TypeError(f"Missing required field - '{f.name}'")
# Pass any additional kwargs to `__post_init__` and let the object
# decide whether to set the attr or use for different purposes (e.g. BC checks)
additional_kwargs = {}
for name, value in kwargs.items():
if name not in dataclass_fields:
additional_kwargs[name] = value
self.__post_init__(**additional_kwargs)
cls.__init__ = __init__
return cls
@dataclass_transform(kw_only_default=True)
@strict(accept_kwargs=True)
@dataclass(repr=False)
class PreTrainedConfig(PushToHubMixin, RotaryEmbeddingConfigMixin, HeterogeneousConfigMixin):View on GitHub (pinned to a597f97485)
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
Example fix
# before cfg = MyConfig(hidden_size=768) # num_heads required # after cfg = MyConfig(hidden_size=768, num_heads=12)
Defensive patterns
Strategy: validation
Validate before calling
from dataclasses import fields, MISSING
required = [f.name for f in fields(MyConfig) if f.default is MISSING and f.default_factory is MISSING]
missing = [r for r in required if r not in params]
assert not missing, f'supply required config fields: {missing}'
cfg = MyConfig(**params) Type guard
def has_all_required(cls, params: dict) -> bool:
return all(
f.name in params or f.default is not MISSING or f.default_factory is not MISSING
for f in fields(cls)
) Try / catch
try:
cfg = MyConfig(**params)
except TypeError as e:
m = re.search(r"Missing required field - '(\w+)'", str(e))
if m:
raise ValueError(f'config needs value for {m.group(1)!r}') from e
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- {cls.__name__} accepts only keyword arguments, but found `{l
- out_indices must be a list, got {type(self._out_indices)}
- out_indices must be valid indices for stage_names {self.stag
- out_indices must not contain any duplicates, got {self._out_
- out_indices must be in the same order as stage_names, expect
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/4909db87b8663ae6.
Report an issue: GitHub.