{"record":{"id":"d04c13bde3418acf","repo":"twentyhq/twenty","slug":"slide-index-idx-out-of-range-0-total-slides","errorCode":null,"errorMessage":"Slide index {idx} out of range (0-{total_slides - 1})","messagePattern":"Slide index (.+?) out of range \\(0-(.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/twenty-server/src/engine/core-modules/code-interpreter/sandbox-scripts/pptx/rearrange.py","lineNumber":170,"sourceCode":"\n    Args:\n        template_path: Path to template PPTX file\n        output_path: Path for output PPTX file\n        slide_sequence: List of slide indices (0-based) to include\n    \"\"\"\n    # Copy template to preserve dimensions and theme\n    if template_path != output_path:\n        shutil.copy2(template_path, output_path)\n        prs = Presentation(output_path)\n    else:\n        prs = Presentation(template_path)\n\n    total_slides = len(prs.slides)\n\n    # Validate indices\n    for idx in slide_sequence:\n        if idx < 0 or idx >= total_slides:\n            raise ValueError(f\"Slide index {idx} out of range (0-{total_slides - 1})\")\n\n    # Track original slides and their duplicates\n    slide_map = []  # List of actual slide indices for final presentation\n    duplicated = {}  # Track duplicates: original_idx -> [duplicate_indices]\n\n    # Step 1: DUPLICATE repeated slides\n    print(f\"Processing {len(slide_sequence)} slides from template...\")\n    for i, template_idx in enumerate(slide_sequence):\n        if template_idx in duplicated and duplicated[template_idx]:\n            # Already duplicated this slide, use the duplicate\n            slide_map.append(duplicated[template_idx].pop(0))\n            print(f\"  [{i}] Using duplicate of slide {template_idx}\")\n        elif slide_sequence.count(template_idx) > 1 and template_idx not in duplicated:\n            # First occurrence of a repeated slide - create duplicates\n            slide_map.append(template_idx)\n            duplicates = []\n            count = slide_sequence.count(template_idx) - 1\n            print(","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/twentyhq/twenty/blob/1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6/packages/twenty-server/src/engine/core-modules/code-interpreter/sandbox-scripts/pptx/rearrange.py#L152-L188","documentation":"Raised by rearrange.py when validating a slide reordering sequence against the source presentation. After loading the template and counting slides (0-based), it checks every index in slide_sequence before doing any duplication, so an out-of-range index aborts before the deck is mutated. The upper bound in the message is total_slides - 1 because indices are 0-based.","triggerScenarios":"Passing a slide_sequence containing an index < 0 or >= total_slides. Common when the sequence was computed against a different deck (e.g. an inventory JSON from another file), when an LLM hardcodes an index assuming a minimum slide count, or when off-by-one confusion mixes 1-based user input with the 0-based API.","commonSituations":"Code-interpreter scripts that call rearrange() with indices derived from inventory output without clamping; LLM-generated reorder plans that assume 1-based indexing; passing the same index list to decks of differing lengths.","solutions":["Clamp every value in slide_sequence to [0, total_slides - 1] before calling, or filter out invalid indices explicitly.","If your input is 1-based, subtract 1: `seq = [i - 1 for i in user_seq]`.","Regenerate the inventory from the exact deck you will rearrange so indices match its current slide count.","Handle the ValueError and report which index was out of range to the caller."],"exampleFix":"# before\nrearrange('deck.pptx', [0, 1, 5, 2], 'out.pptx')   # 5 out of range for a 4-slide deck\n# after\ntotal = len(Presentation('deck.pptx').slides)\nseq = [i for i in [0, 1, 5, 2] if 0 <= i < total]\nrearrange('deck.pptx', seq, 'out.pptx')","handlingStrategy":"validation","validationCode":"from pptx import Presentation\n\ndef clamp_sequence(pptx_path: str, sequence: list[int]) -> list[int]:\n    total = len(Presentation(pptx_path).slides)\n    valid = [i for i in sequence if 0 <= i < total]\n    if len(valid) != len(sequence):\n        dropped = [i for i in sequence if not 0 <= i < total]\n        raise ValueError(f'Sequence indices out of range 0-{total - 1}: {dropped}')\n    return valid","typeGuard":"def is_valid_slide_sequence(pptx_path: str, sequence: list[int]) -> bool:\n    total = len(__import__('pptx').Presentation(pptx_path).slides)\n    return all(0 <= i < total for i in sequence)","tryCatchPattern":"try:\n    rearrange(template, sequence, output)\nexcept ValueError as e:\n    if 'out of range' in str(e):\n        # recover by clamping to valid range and retrying once\n        total = len(Presentation(template).slides)\n        sequence = [i for i in sequence if 0 <= i < total]\n        rearrange(template, sequence, output)\n    else:\n        raise","preventionTips":["Generate the reorder sequence from the same deck you will rearrange — never reuse an inventory from a different file.","Convert 1-based user input to 0-based before passing.","Clamp or filter out-of-range indices explicitly rather than relying on the throw."],"tags":["python","pptx","code-interpreter","validation","off-by-one"],"backgroundTag":null,"analyzedSha":"1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6","analyzedAt":"2026-08-12T15:37:27.593Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}