twentyhq/twenty · error · ValueError
Slide index {idx} out of range (0-{total_slides - 1})
Error message
Slide index {idx} out of range (0-{total_slides - 1}) What it means
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.
Source
Thrown at packages/twenty-server/src/engine/core-modules/code-interpreter/sandbox-scripts/pptx/rearrange.py:170
Args:
template_path: Path to template PPTX file
output_path: Path for output PPTX file
slide_sequence: List of slide indices (0-based) to include
"""
# Copy template to preserve dimensions and theme
if template_path != output_path:
shutil.copy2(template_path, output_path)
prs = Presentation(output_path)
else:
prs = Presentation(template_path)
total_slides = len(prs.slides)
# Validate indices
for idx in slide_sequence:
if idx < 0 or idx >= total_slides:
raise ValueError(f"Slide index {idx} out of range (0-{total_slides - 1})")
# Track original slides and their duplicates
slide_map = [] # List of actual slide indices for final presentation
duplicated = {} # Track duplicates: original_idx -> [duplicate_indices]
# Step 1: DUPLICATE repeated slides
print(f"Processing {len(slide_sequence)} slides from template...")
for i, template_idx in enumerate(slide_sequence):
if template_idx in duplicated and duplicated[template_idx]:
# Already duplicated this slide, use the duplicate
slide_map.append(duplicated[template_idx].pop(0))
print(f" [{i}] Using duplicate of slide {template_idx}")
elif slide_sequence.count(template_idx) > 1 and template_idx not in duplicated:
# First occurrence of a repeated slide - create duplicates
slide_map.append(template_idx)
duplicates = []
count = slide_sequence.count(template_idx) - 1
print(View on GitHub (pinned to 1f5dd2bbd2)
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.
Example fix
# before
rearrange('deck.pptx', [0, 1, 5, 2], 'out.pptx') # 5 out of range for a 4-slide deck
# after
total = len(Presentation('deck.pptx').slides)
seq = [i for i in [0, 1, 5, 2] if 0 <= i < total]
rearrange('deck.pptx', seq, 'out.pptx') Defensive patterns
Strategy: validation
Validate before calling
from pptx import Presentation
def clamp_sequence(pptx_path: str, sequence: list[int]) -> list[int]:
total = len(Presentation(pptx_path).slides)
valid = [i for i in sequence if 0 <= i < total]
if len(valid) != len(sequence):
dropped = [i for i in sequence if not 0 <= i < total]
raise ValueError(f'Sequence indices out of range 0-{total - 1}: {dropped}')
return valid Type guard
def is_valid_slide_sequence(pptx_path: str, sequence: list[int]) -> bool:
total = len(__import__('pptx').Presentation(pptx_path).slides)
return all(0 <= i < total for i in sequence) Try / catch
try:
rearrange(template, sequence, output)
except ValueError as e:
if 'out of range' in str(e):
# recover by clamping to valid range and retrying once
total = len(Presentation(template).slides)
sequence = [i for i in sequence if 0 <= i < total]
rearrange(template, sequence, output)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Found {len(errors)} validation error(s)
- Found {len(overflow_errors)} overflow error(s) and {len(warn
- {input_dir} is not a directory
- {output_file} must be a .docx, .pptx, or .xlsx file
- Duplicate key found in JSON: '{key}'
AI-assisted analysis of twentyhq/twenty@1f5dd2bbd2 (2026-08-12).
Data as JSON: /api/errors/d04c13bde3418acf.
Report an issue: GitHub.