invoke-ai/InvokeAI · error · ValueError
Invalid CFG scale type: {type(self.cfg_scale)}
Error message
Invalid CFG scale type: {type(self.cfg_scale)} What it means
`_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.
Source
Thrown at invokeai/app/invocations/krea2_denoise.py:215
KREA2_LATENT_CHANNELS,
int(height) // LATENT_SCALE_FACTOR,
int(width) // LATENT_SCALE_FACTOR,
device=rand_device,
dtype=torch.float32,
generator=torch.Generator(device=rand_device).manual_seed(seed),
).to(device=device, dtype=dtype)
def _prepare_cfg_scale(self, num_timesteps: int) -> list[float]:
if isinstance(self.cfg_scale, float):
return [self.cfg_scale] * num_timesteps
if isinstance(self.cfg_scale, list):
if len(self.cfg_scale) != num_timesteps:
raise ValueError(
f"cfg_scale list has {len(self.cfg_scale)} values but the model is configured for "
f"{num_timesteps} steps. Provide one CFG value per configured step (or a single float)."
)
return self.cfg_scale
raise ValueError(f"Invalid CFG scale type: {type(self.cfg_scale)}")
@staticmethod
def _should_apply_cfg_for_step(cfg_scale: float, *, has_negative_conditioning: bool) -> bool:
return has_negative_conditioning and cfg_scale > 1.0
@staticmethod
def _validate_effective_schedule(*, start_idx: int, end_idx: int) -> None:
if end_idx <= start_idx:
raise ValueError(
"The requested denoising range does not contain any effective denoising steps at the configured "
"step count. Increase denoising_end, decrease denoising_start, or increase steps."
)
def _validate_inputs(self) -> None:
if self.denoising_start >= self.denoising_end:
raise ValueError("denoising_start must be less than denoising_end.")
if self.denoise_mask is not None and self.latents is None:
raise ValueError("Initial latents are required when a denoise mask is provided.")View on GitHub (pinned to 0b6a024f2f)
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).
Example fix
// before
cfg_scale="3.5" # str from JSON
cfg_scale=4 # int
// after
cfg_scale=float("3.5") # 3.5
cfg_scale=4.0 Defensive patterns
Strategy: type-guard
Validate before calling
if not (isinstance(cfg_scale, float) or (isinstance(cfg_scale, list) and all(isinstance(v, float) for v in cfg_scale))):
raise TypeError(f"cfg_scale must be float or list[float], got {type(cfg_scale)}") Type guard
def is_valid_cfg_type(cfg_scale) -> bool:
if isinstance(cfg_scale, float):
return True
return isinstance(cfg_scale, list) and all(isinstance(v, float) for v in cfg_scale) Try / catch
try:
out = invoke_krea2_denoise(cfg_scale=cfg_scale)
except (ValueError, TypeError) as e:
if "Invalid CFG scale type" in str(e):
cfg_scale = float(cfg_scale)
out = invoke_krea2_denoise(cfg_scale=cfg_scale)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Programmatic invocation construction passing raw JSON values without type coercion; UI integrations sending strings; custom scripts passing ints because pydantic coercion is bypassed.
Related errors
- cfg_scale list has {len(self.cfg_scale)} values but the mode
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
- Expected PreTrainedModel for Gemma encoder, got {type(gemma_
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
- Expected PidNet for PiD decoder, got {type(pid_net).__name__
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/c504086257aadefc.
Report an issue: GitHub.