invoke-ai/InvokeAI · error · ValueError
missing _class_name or architectures field
Error message
missing _class_name or architectures field
What it means
NotAMatchError raised by get_class_name_from_config_dict_or_raise when a successfully loaded config dict contains neither a '_class_name' key (diffusers-style configs) nor an 'architectures' key (transformers-style configs), so no architecture marker can be extracted. This is wrapped into NotAMatchError with the 'unable to determine class name' message (1039), whose cause chain shows this ValueError.
Solutions
- Add "architectures": ["Gemma2ForCausalLM"] (or the appropriate class) to config.json, or "_class_name" for diffusers-style configs
- Re-download config.json from the original HuggingFace repo instead of a hand-made one
- Point the importer at the correct config file — you may be reading a secondary config (e.g. tokenizer_config.json-style file) that lacks these keys
Example fix
// before
{ "hidden_size": 2304, "model_type": "gemma2" }
// after
{ "architectures": ["Gemma2ForCausalLM"], "hidden_size": 2304, "model_type": "gemma2" } Defensive patterns
Strategy: validation
Validate before calling
import json
from pathlib import Path
def config_has_class_name(model_dir: str | Path) -> bool:
p = Path(model_dir) / "config.json"
try:
cfg = json.loads(p.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
return False
archs = cfg.get("architectures")
return isinstance(cfg.get("_class_name"), str) or (
isinstance(archs, list) and len(archs) > 0 and isinstance(archs[0], str)
) Type guard
def extract_class_name(cfg: dict) -> str | None:
if isinstance(cfg.get("_class_name"), str):
return cfg["_class_name"]
archs = cfg.get("architectures")
if isinstance(archs, list) and archs and isinstance(archs[0], str):
return archs[0]
return None Try / catch
try:
import_model(model_dir)
except NotAMatchError as e:
if "unable to determine class name" in str(e):
print("config.json lacks _class_name/architectures — restore the original from the HF repo") Prevention
- Never hand-write config.json from scratch — copy it from the source repo
- Ensure 'architectures' is a non-empty list of strings, not a string or []
- Keep both _class_name (diffusers) and architectures (transformers) when converting between formats
When it happens
Trigger: get_class_name_from_config_dict_or_raise / raise_for_class_name / from_model_on_disk receiving a config dict (e.g. model_index.json, custom config.json, or a hand-written JSON) lacking both keys — commonly a bare {"model_type": ...}-only transformers config or an empty {} dict.
Common situations: Custom model exports that omit _class_name; older or minimal transformers configs without 'architectures'; user-authored placeholder config.json; configs trimmed by download managers that only keep model_type.
Related errors
- unable to determine class name from config file
- unable to load config file(s)
- could not read text_encoder/config.json
- directory does not contain Gemma2 tokenizer files…
- directory looks like a full diffusers pipeline (has…
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/801c66576eb0d2bd.
Report an issue: GitHub.
Appendix: source
Thrown at invokeai/backend/model_manager/configs/identification_utils.py:104
Returns:
The class name from the config file.
Raises:
NotAMatch if the config file is missing or does not contain a valid class name.
"""
if not isinstance(config, dict):
config = get_config_dict_or_raise(config)
try:
if "_class_name" in config:
# This is a diffusers-style config
config_class_name = config["_class_name"]
elif "architectures" in config:
# This is a transformers-style config
config_class_name = config["architectures"][0]
else:
raise ValueError("missing _class_name or architectures field")
except Exception as e:
raise NotAMatchError(f"unable to determine class name from config file: {config}") from e
if not isinstance(config_class_name, str):
raise NotAMatchError(f"_class_name or architectures field is not a string: {config_class_name}")
return config_class_name
def raise_for_class_name(config: Path | set[Path] | dict[str, Any], class_name: str | set[str]) -> None:
"""Get the class name from the config file and raise NotAMatch if it is not in the expected set.
Args:
config_path: The path to the config file, or a set of paths to try.
class_name: The expected class name, or a set of expected class names.
Raises:
NotAMatch if the class name is not in the expected set.View on GitHub (pinned to 0b6a024f2f)