pathwaycom/pathway · error · IndexError

Index n

Error message

Index n

What it means

Raised as IndexError('Index n') when a Tuple-typed column is accessed with an index expression that is not a compile-time integer constant. Pathway's type interpreter must resolve tuple element types statically, so dynamic (row-dependent) or non-integer indices into a Tuple are not supported.

Source

Thrown at python/pathway/internals/type_interpreter.py:570

            assert not isinstance(object_dtype.args, EllipsisType)
            dtypes = object_dtype.args

            if (
                expression._const_index is None
            ):  # no specified position, index is an Expression
                assert isinstance(dtypes[0], dt.DType)
                return_dtype = dtypes[0]
                for dtype in dtypes[1:]:
                    if isinstance(dtype, dt.DType):
                        return_dtype = dt.types_lca(return_dtype, dtype, raising=False)
                if expression._check_if_exists:
                    return_dtype = dt.types_lca(
                        return_dtype, default_dtype, raising=False
                    )
                return _wrap(expression, return_dtype)

            if not isinstance(expression._const_index, int):
                raise IndexError("Index n")

            try:
                try_ret = dtypes[expression._const_index]
                return _wrap(expression, try_ret)
            except IndexError:
                message = (
                    f"Index {expression._const_index} out of range for a tuple of"
                    + f" type {object_dtype.typehint}."
                )
                if expression._check_if_exists:
                    expression_info = get_expression_info(expression)
                    warnings.warn(
                        message
                        + " It refers to the following expression:\n"
                        + expression_info
                        + "Consider using just the default value without .get()."
                    )
                    return _wrap(expression, default_dtype)

View on GitHub (pinned to fa2f74a464)

Solutions

  1. Use a constant int index for Tuple columns: t.tuple_col.get(1)
  2. If the index must be dynamic, retype the column as pw.List or pw.Array (homogeneous elements) so dynamic int indexing is allowed
  3. Convert a computed index to a constant via apply/apply_with_type returning the element directly

Example fix

// before
res = t.select(x=t.tup.get(t.i))  # t.i is a column -> not a const int

// after (dynamic index needs List, not Tuple)
schema_tup = pw.schema_from_types(tup=pw.List[int])
# or constant index:
res = t.select(x=t.tup.get(1))
Defensive patterns

Strategy: validation

Validate before calling

import pathway as pw

def tuple_const_get(tup_col, index):
    # only constant ints are allowed on Tuple columns
    assert isinstance(index, int) and not isinstance(index, bool), (
        'Tuple access requires a constant int index'
    )
    return tup_col.get(index)

Type guard

def is_const_int_index(expr) -> bool:
    ci = getattr(expr, '_const_index', None)
    return isinstance(ci, int) and not isinstance(ci, bool)

Try / catch

try:
    res = t.select(x=t.tup.get(idx))
except IndexError as e:
    if str(e) == 'Index n':
        raise ValueError('Dynamic indices need pw.List, not pw.Tuple; retype the column') from e
    raise

Prevention

When it happens

Trigger: Calling t.tuple_col.get(t.some_column) or t.tuple_col[i_as_str] where the tuple column is typed as a heterogeneous Tuple (e.g. Tuple[int, str]) and the index is a column expression, float, or string instead of an int literal. Dynamic indexing works only for List/Array (homogeneous), not Tuple.

Common situations: Developer indexes a Tuple with a per-row column value; schema changed from List to Tuple and previously working dynamic access now fails; index computed as float (e.g. from division) instead of int.

Related errors


AI-assisted analysis of pathwaycom/pathway@fa2f74a464 (2026-08-15). Data as JSON: /api/errors/cacbb10045e9a608. Report an issue: GitHub.