pytorch/pytorch · error · TypeError
expected a dimension specifyer but found {repr(s)}
Error message
expected a dimension specifyer but found {repr(s)} What it means
Inside Tensor.index(), each entry of the dims argument is normalized with _wrap_dim; if the result 'is none' (the value could not be interpreted as a positional int or a Dim), TypeError('expected a dimension specifyer but found {s}') is raised. Valid specifyers are ints and Dim objects.
Source
Thrown at functorch/dim/__init__.py:738
| list[int | slice | torch.Tensor],
) -> _Tensor:
"""
Index tensor using first-class dimensions.
"""
from ._dim_entry import _match_levels
from ._getsetitem import getsetitem_flat, invoke_getitem
from ._wrap import _wrap_dim
# Helper to check if obj is a dimpack (tuple/list) and extract items
def maybe_dimpack(obj: Any, check_first: bool = False) -> tuple[Any, bool]:
if isinstance(obj, (tuple, list)):
return list(obj), True
return None, False
def parse_dim_entry(s: Any) -> Any:
d = _wrap_dim(s, self.ndim, False)
if d.is_none():
raise TypeError(f"expected a dimension specifyer but found {repr(s)}")
return d
# Helper for dimension not present errors
def dim_not_present(d: Any) -> None:
if d.is_positional():
raise TypeError(
f"dimension {d.position() + self.ndim} not in tensor of {self.ndim} dimensions"
)
else:
raise TypeError(f"dimension {repr(d.dim())} not in tensor")
dims_list: list[int | Dim] = []
indices_list: list[int | slice | torch.Tensor] = []
lhs_list = isinstance(dims, (tuple, list))
rhs_list = isinstance(indices, (tuple, list))
if lhs_list and rhs_list:View on GitHub (pinned to dcd2ecae77)
Solutions
- Pass only int positions or Dim objects created by dims()/dimlists()
- Convert numpy scalars: t.index(int(np_axis), ...)
- For named axes use actual dims: batch = dims(1); t.index(batch, 0)
Example fix
# before
out = t.index('batch', 0) # TypeError: string not a dimension specifyer
# after
batch = dims(1)
out = t.index(batch, 0) Defensive patterns
Strategy: type-guard
Validate before calling
from functorch.dim import Dim
if not isinstance(s, (int, Dim)):
raise TypeError(f'dimension specifyer must be int or Dim, got {type(s).__name__}') Type guard
from functorch.dim import Dim
import numpy as np
def is_dim_specifyer(s) -> bool:
if isinstance(s, np.integer):
s = int(s)
return isinstance(s, (int, Dim)) and not isinstance(s, bool) Prevention
- Only pass ints and Dim objects as dims to index()
- Convert numpy scalars with int() at the boundary
- Do not use string axis names; create real dims with dims()
When it happens
Trigger: Passing None, a string, a float, a numpy scalar, or other objects as a dimension to tensor.index(...) / the dims part of first-class-dimension indexing, e.g. t.index('batch', 0) or t.index(None, slice(None)).
Common situations: Mixing string axis names (pandas/xarray habits) with functorch dims, passing unwrapped numpy ints, or None leaking in from optional config for a dim name.
Related errors
- dimension {d.position() + self.ndim} not in tensor of {self.
- dimension {repr(d.dim())} not in tensor
- dims ({len(dims_seq)}) and indices ({len(indices_seq)}) must
- expected a sequence
- expected an int or a slice
AI-assisted analysis of pytorch/pytorch@dcd2ecae77 (2026-08-14).
Data as JSON: /api/errors/9cd87803f06c0c00.
Report an issue: GitHub.