huggingface/smolagents · error · InterpreterError

Could not index {value} with '{index}': {type(e).__name__}:

Error message

Could not index {value} with '{index}': {type(e).__name__}: {e}

What it means

A subscript operation x[index] raised KeyError, IndexError, or TypeError inside the sandboxed interpreter; the message wraps the original exception and, for string keys on Mapping values, suggests close-match key names via difflib.

Source

Thrown at src/smolagents/local_python_executor.py:938

def evaluate_subscript(
    subscript: ast.Subscript,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> Any:
    index = evaluate_ast(subscript.slice, state, static_tools, custom_tools, authorized_imports)
    value = evaluate_ast(subscript.value, state, static_tools, custom_tools, authorized_imports)
    try:
        return value[index]
    except (KeyError, IndexError, TypeError) as e:
        error_message = f"Could not index {value} with '{index}': {type(e).__name__}: {e}"
        if isinstance(index, str) and isinstance(value, Mapping):
            close_matches = difflib.get_close_matches(index, list(value.keys()))
            if len(close_matches) > 0:
                error_message += f". Maybe you meant one of these indexes instead: {str(close_matches)}"
        raise InterpreterError(error_message) from e


def evaluate_name(
    name: ast.Name,
    state: dict[str, Any],
    static_tools: dict[str, Callable],
    custom_tools: dict[str, Callable],
    authorized_imports: list[str],
) -> Any:
    if name.id in state:
        return state[name.id]
    elif name.id in static_tools:
        return safer_func(static_tools[name.id], static_tools=static_tools, authorized_imports=authorized_imports)
    elif name.id in custom_tools:
        return custom_tools[name.id]
    elif name.id in ERRORS:
        return ERRORS[name.id]
    close_matches = difflib.get_close_matches(name.id, list(state.keys()))

View on GitHub (pinned to 30bb116109)

Solutions

  1. Read the appended 'Maybe you meant' suggestion and fix the key name
  2. Guard with key in d / try-except KeyError / .get(key, default)
  3. Check the container is not None and the index is within range before subscripting

Example fix

# before
val = record['nam']
# after
val = record.get('name', None)
if val is None:
    raise KeyError('name missing')
Defensive patterns

Strategy: try-catch

Validate before calling

if key not in mapping:
    raise KeyError(f'{key!r} missing; available: {sorted(mapping)}')
val = mapping[key]

Type guard

def can_index(obj, idx) -> bool:
    if isinstance(obj, dict):
        return idx in obj
    if isinstance(obj, (list, tuple, str)):
        return isinstance(idx, int) and -len(obj) <= idx < len(obj)
    return False

Try / catch

try:
    evaluate_python(code, ...)
except InterpreterError as e:
    if 'Could not index' in str(e):
        # parse suggested close matches, fix key, retry

Prevention

When it happens

Trigger: dict['missing_key'], list[10] on a short list, indexing an int/None, or mixing types (e.g. d[0] on a dict with string keys).

Common situations: Agent code assumes keys exist (df['cloumn'] typo — the close-match hint fires); index computed from len arithmetic that is off by one; the value is None because a previous step failed.

Related errors


AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28). Data as JSON: /api/errors/ce48e343eb71e0c5. Report an issue: GitHub.