huggingface/pytorch-image-models · error · TypeError

Item {i} is not a sequence: {item!r}

Error message

Item {i} is not a sequence: {item!r}

What it means

Thrown by resolve_ns_coefficients in timm's Muon optimizer when the coefficients argument is treated as a list of triples but one of its elements is not itself a sequence. Each item must be indexable as a 3-sequence.

Source

Thrown at timm/optim/muon.py:1052

        if not is_seq(seq) or len(seq) == 0:
            raise ValueError(f"Preset '{value}' is empty or invalid")
        return [as_coeff(item) for item in seq]  # validate & cast

    if not is_seq(value):
        raise TypeError(
            "Coefficients must be a preset name (str), a 3-sequence (a,b,c), "
            "or a sequence of 3-sequences."
        )

    # Decide single triple vs list-of-triples by structure
    if len(value) == 3 and all(is_real(v) for v in value):  # type: ignore[index]
        return [as_coeff(value)]  # single triple -> wrap

    # Otherwise treat as list/tuple of triples
    out = []
    for i, item in enumerate(value):  # type: ignore[assignment]
        if not is_seq(item):
            raise TypeError(f"Item {i} is not a sequence: {item!r}")
        out.append(as_coeff(item))
    if not out:
        raise ValueError("Coefficient list cannot be empty")
    return out

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Wrap scalars into triples: [(0.9, 0.95, 0.5)] instead of [0.9, 0.95, 0.5]
  2. If a single triple is intended, pass the tuple directly not inside a list
  3. Check each item with isinstance(item, (list, tuple)) before constructing the argument

Example fix

# before
Muon(params, ns_coefficients=[0.9, 0.95, 0.5])

# after
Muon(params, ns_coefficients=[(0.9, 0.95, 0.5)])
Defensive patterns

Strategy: type-guard

Validate before calling

assert all(isinstance(item, (list, tuple)) and len(item) == 3 for item in ns_coefficients), 'each item must be a 3-tuple'

Type guard

def is_list_of_triples(v) -> bool:
    return isinstance(v, (list, tuple)) and all(isinstance(i, (list, tuple)) for i in v)

Prevention

When it happens

Trigger: Passing ns_coefficients=[0.9, 0.95, 0.5, 1.0] (four scalars, so not detected as a single triple) — the first item 0.9 is not a sequence, raising TypeError with i=0. Also triggered by lists mixing floats and tuples.

Common situations: Passing a flat list of numbers whose length is not 3 or whose items are all scalars; nesting mistakes when building per-layer coefficient lists.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/b4a9136062206bc9. Report an issue: GitHub.