{"record":{"id":"c504086257aadefc","repo":"invoke-ai/InvokeAI","slug":"invalid-cfg-scale-type-type-self-cfg-scale-c50408","errorCode":null,"errorMessage":"Invalid CFG scale type: {type(self.cfg_scale)}","messagePattern":"Invalid CFG scale type: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/krea2_denoise.py","lineNumber":215,"sourceCode":"            KREA2_LATENT_CHANNELS,\n            int(height) // LATENT_SCALE_FACTOR,\n            int(width) // LATENT_SCALE_FACTOR,\n            device=rand_device,\n            dtype=torch.float32,\n            generator=torch.Generator(device=rand_device).manual_seed(seed),\n        ).to(device=device, dtype=dtype)\n\n    def _prepare_cfg_scale(self, num_timesteps: int) -> list[float]:\n        if isinstance(self.cfg_scale, float):\n            return [self.cfg_scale] * num_timesteps\n        if isinstance(self.cfg_scale, list):\n            if len(self.cfg_scale) != num_timesteps:\n                raise ValueError(\n                    f\"cfg_scale list has {len(self.cfg_scale)} values but the model is configured for \"\n                    f\"{num_timesteps} steps. Provide one CFG value per configured step (or a single float).\"\n                )\n            return self.cfg_scale\n        raise ValueError(f\"Invalid CFG scale type: {type(self.cfg_scale)}\")\n\n    @staticmethod\n    def _should_apply_cfg_for_step(cfg_scale: float, *, has_negative_conditioning: bool) -> bool:\n        return has_negative_conditioning and cfg_scale > 1.0\n\n    @staticmethod\n    def _validate_effective_schedule(*, start_idx: int, end_idx: int) -> None:\n        if end_idx <= start_idx:\n            raise ValueError(\n                \"The requested denoising range does not contain any effective denoising steps at the configured \"\n                \"step count. Increase denoising_end, decrease denoising_start, or increase steps.\"\n            )\n\n    def _validate_inputs(self) -> None:\n        if self.denoising_start >= self.denoising_end:\n            raise ValueError(\"denoising_start must be less than denoising_end.\")\n        if self.denoise_mask is not None and self.latents is None:\n            raise ValueError(\"Initial latents are required when a denoise mask is provided.\")","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/krea2_denoise.py#L197-L233","documentation":"`_prepare_cfg_scale` only accepts float or list[float] for cfg_scale. Any other type (int, str, dict, etc.) reaches the final fallback raise and produces this ValueError naming the offending Python type.","triggerScenarios":"Passing cfg_scale as an int (e.g. cfg_scale=4 rather than 4.0 from a non-coercing caller), a string from JSON deserialization, or None from an unbound input field.","commonSituations":"Programmatic invocation construction passing raw JSON values without type coercion; UI integrations sending strings; custom scripts passing ints because pydantic coercion is bypassed.","solutions":["Convert cfg_scale to float before invoking: cfg_scale=float(value).","If passing a schedule, ensure it is a list of floats with length == steps.","Check the caller/serialization layer for values that skip pydantic validation (raw dict construction)."],"exampleFix":"// before\ncfg_scale=\"3.5\"          # str from JSON\ncfg_scale=4              # int\n// after\ncfg_scale=float(\"3.5\")   # 3.5\ncfg_scale=4.0","handlingStrategy":"type-guard","validationCode":"if not (isinstance(cfg_scale, float) or (isinstance(cfg_scale, list) and all(isinstance(v, float) for v in cfg_scale))):\n    raise TypeError(f\"cfg_scale must be float or list[float], got {type(cfg_scale)}\")","typeGuard":"def is_valid_cfg_type(cfg_scale) -> bool:\n    if isinstance(cfg_scale, float):\n        return True\n    return isinstance(cfg_scale, list) and all(isinstance(v, float) for v in cfg_scale)","tryCatchPattern":"try:\n    out = invoke_krea2_denoise(cfg_scale=cfg_scale)\nexcept (ValueError, TypeError) as e:\n    if \"Invalid CFG scale type\" in str(e):\n        cfg_scale = float(cfg_scale)\n        out = invoke_krea2_denoise(cfg_scale=cfg_scale)\n    else:\n        raise","preventionTips":["Coerce values to float at the serialization boundary (float(value)).","Avoid bypassing pydantic validation by building invocations from raw dicts.","Annotate and type-check cfg_scale in helper scripts with mypy."],"tags":["invokeai","krea2","type-error","cfg"],"backgroundTag":"invalid-parameter-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}