{"record":{"id":"a2b29df075e7deea","repo":"invoke-ai/InvokeAI","slug":"invalid-cfg-scale-end-step-out-of-range-cfg-sca","errorCode":null,"errorMessage":"Invalid cfg_scale_end_step. Out of range: {cfg_scale_end_step}.","messagePattern":"Invalid cfg_scale_end_step\\. Out of range: (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/flux_denoise.py","lineNumber":655,"sourceCode":"        elif isinstance(cfg_scale, list):\n            cfg_scale_list = cfg_scale\n        else:\n            raise ValueError(f\"Unsupported cfg_scale type: {type(cfg_scale)}\")\n        assert len(cfg_scale_list) == num_steps\n\n        # Handle negative indices for cfg_scale_start_step and cfg_scale_end_step.\n        start_step_index = cfg_scale_start_step\n        if start_step_index < 0:\n            start_step_index = num_steps + start_step_index\n        end_step_index = cfg_scale_end_step\n        if end_step_index < 0:\n            end_step_index = num_steps + end_step_index\n\n        # Validate the start and end step indices.\n        if not (0 <= start_step_index < num_steps):\n            raise ValueError(f\"Invalid cfg_scale_start_step. Out of range: {cfg_scale_start_step}.\")\n        if not (0 <= end_step_index < num_steps):\n            raise ValueError(f\"Invalid cfg_scale_end_step. Out of range: {cfg_scale_end_step}.\")\n        if start_step_index > end_step_index:\n            raise ValueError(\n                f\"cfg_scale_start_step ({cfg_scale_start_step}) must be before cfg_scale_end_step \"\n                + f\"({cfg_scale_end_step}).\"\n            )\n\n        # Set values outside the start and end step indices to 1.0. This is equivalent to disabling cfg_scale for those\n        # steps.\n        clipped_cfg_scale = [1.0] * num_steps\n        clipped_cfg_scale[start_step_index : end_step_index + 1] = cfg_scale_list[start_step_index : end_step_index + 1]\n\n        return clipped_cfg_scale\n\n    def _prep_inpaint_mask(self, context: InvocationContext, latents: torch.Tensor) -> torch.Tensor | None:\n        \"\"\"Prepare the inpaint mask.\n\n        - Loads the mask\n        - Resizes if necessary","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux_denoise.py#L637-L673","documentation":"Analogous to the start-step check: after resolving negative indices, cfg_scale_end_step must satisfy 0 <= end < num_steps. Otherwise it would fall outside the per-step cfg list and the invocation raises with the invalid value.","triggerScenarios":"Calling prep_cfg_scale with end_step >= num_steps or < -num_steps (e.g. end_step=20 for a 4-step Schnell run); the end-step slider retaining a value from a longer run.","commonSituations":"Reducing step count in the UI without adjusting the end step; editing workflow JSON manually; reusing a shared CFG-schedule node across graphs with different step counts.","solutions":["Set cfg_scale_end_step within 0 <= end < num_steps for the current run.","Use -1 to denote the final step instead of a hard-coded index.","Clamp: end_index = min(max(cfg_scale_end_step, -num_steps), num_steps - 1) before calling."],"exampleFix":"// before\nprep_cfg_scale(num_steps=4, cfg_scale_end_step=20) // out of range\n// after\nprep_cfg_scale(num_steps=4, cfg_scale_end_step=-1) // final step","handlingStrategy":"validation","validationCode":"if not (0 <= cfg_scale_end_step < num_steps or -num_steps <= cfg_scale_end_step < 0):\n    raise ValueError('cfg_scale_end_step out of range')","typeGuard":"def is_valid_end_step(step: int, num_steps: int) -> bool:\n    return -num_steps <= step < num_steps","tryCatchPattern":"try:\n    cfg_list = denoise.prep_cfg_scale(cfg_scale, num_steps, cfg_scale_start_step=s, cfg_scale_end_step=e)\nexcept ValueError as e:\n    if 'Invalid cfg_scale_end_step' in str(e):\n        cfg_list = denoise.prep_cfg_scale(cfg_scale, num_steps, cfg_scale_start_step=s, cfg_scale_end_step=-1)\n    else:\n        raise","preventionTips":["Default the end step to -1 (last step) rather than an absolute index","Sync end-step fields with the step-count slider in the UI","Clamp indices before calling prep_cfg_scale"],"tags":["cfg-scale","range-error","flux","validation"],"backgroundTag":"index-out-of-range","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}