{"record":{"id":"bdcebd9a2ee60773","repo":"Comfy-Org/ComfyUI","slug":"expected-key-target-to-instantiate","errorCode":null,"errorMessage":"Expected key `target` to instantiate.","messagePattern":"Expected key `target` to instantiate\\.","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"comfy/ldm/util.py","lineNumber":79,"sourceCode":"    Take the mean over all non-batch dimensions.\n    \"\"\"\n    return tensor.mean(dim=list(range(1, len(tensor.shape))))\n\n\ndef count_params(model, verbose=False):\n    total_params = sum(p.numel() for p in model.parameters())\n    if verbose:\n        logging.info(f\"{model.__class__.__name__} has {total_params*1.e-6:.2f} M params.\")\n    return total_params\n\n\ndef instantiate_from_config(config):\n    if \"target\" not in config:\n        if config == '__is_first_stage__':\n            return None\n        elif config == \"__is_unconditional__\":\n            return None\n        raise KeyError(\"Expected key `target` to instantiate.\")\n    return get_obj_from_str(config[\"target\"])(**config.get(\"params\", dict()))\n\n\ndef get_obj_from_str(string, reload=False):\n    module, cls = string.rsplit(\".\", 1)\n    if reload:\n        module_imp = importlib.import_module(module)\n        importlib.reload(module_imp)\n    return getattr(importlib.import_module(module, package=None), cls)\n\n\nclass AdamWwithEMAandWings(optim.Optimizer):\n    # credit to https://gist.github.com/crowsonkb/65f7265353f403714fce3b2595e0b298\n    def __init__(self, params, lr=1.e-3, betas=(0.9, 0.999), eps=1.e-8,  # TODO: check hyperparameters before using\n                 weight_decay=1.e-2, amsgrad=False, ema_decay=0.9999,   # ema decay to match previous code\n                 ema_power=1., param_names=()):\n        \"\"\"AdamW that saves EMA versions of the parameters.\"\"\"\n        if not 0.0 <= lr:","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy/ldm/util.py#L61-L97","documentation":"instantiate_from_config() builds an object from a config dict by importing the class named under the \"target\" key. The only exempt values are the sentinel strings '__is_first_stage__' and '__is_unconditional__' (which return None); any other dict without \"target\" raises KeyError. This is a config-schema error: the dict cannot describe what to instantiate.","triggerScenarios":"instantiate_from_config(config) where config is a dict lacking \"target\" — e.g. a checkpoint's JSON/YAML config missing the first-stage/unet entry, or passing params directly instead of wrapping them in {\"target\": ..., \"params\": ...}.","commonSituations":"Editing or truncating a model config JSON; a checkpoint config that uses a different schema version; passing the wrong sub-dict (config[\"params\"] instead of config).","solutions":["Ensure the dict has a \"target\" key with a fully qualified class path, e.g. {\"target\": \"comfy.ldm.seedvr.vae.AutoencoderKLSeedVR\", \"params\": {...}}.","If None is intended for an optional stage, use the sentinels '__is_first_stage__' or '__is_unconditional__' or skip the call.","Inspect the checkpoint's config file for a truncated/malformed stage entry."],"exampleFix":"# before\nmodel = instantiate_from_config({\"params\": {\"embed_dim\": 4}})\n# after\nmodel = instantiate_from_config({\"target\": \"comfy.ldm....Decoder\", \"params\": {\"embed_dim\": 4}})","handlingStrategy":"validation","validationCode":"SENTINELS = {\"__is_first_stage__\", \"__is_unconditional__\"}\nif isinstance(config, dict) and \"target\" not in config and config not in SENTINELS:\n    raise KeyError(\"config lacks 'target'; wrap as {'target': ..., 'params': ...}\")\nobj = instantiate_from_config(config)","typeGuard":"def is_instantiable_config(config) -> bool:\n    return config in (\"__is_first_stage__\", \"__is_unconditional__\") or (isinstance(config, dict) and \"target\" in config)","tryCatchPattern":"try:\n    obj = instantiate_from_config(stage_cfg)\nexcept KeyError as e:\n    if \"target\" in str(e):\n        logging.warning(\"skipping stage without target: %r\", stage_cfg)\n        obj = None\n    else:\n        raise","preventionTips":["Schema-check config files for a 'target' key per stage before loading.","Don't pass params dicts directly; wrap them with their target class path."],"tags":["config","serialization","instantiate","key-error"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}