django/django · error · TypeError

Dimension of value does not match.

Error message

Dimension of value does not match.

What it means

Raised in GEOSCoordSeq.__setitem__ when the length of the assigned sequence does not match the coordinate sequence's active dimension count (n_args). n_args is 2 for 2D, 3 for XYZ or XYM, and 4 for XYZM, computed from self.dims, self._z, and self.hasm. The check fires after the type check but before the index check.

Source

Thrown at django/contrib/gis/geos/coordseq.py:73

        else:
            raise TypeError(
                "Must set coordinate with a sequence (list, tuple, or numpy array)."
            )
        # Checking the dims of the input
        if self.dims == 3 and self._z:
            n_args = 3
            point_setter = self._set_point_3d
        elif self.dims == 3 and self.hasm:
            n_args = 3
            point_setter = self._set_point_3d_m
        elif self.dims == 4 and self._z and self.hasm:
            n_args = 4
            point_setter = self._set_point_4d
        else:
            n_args = 2
            point_setter = self._set_point_2d
        if len(value) != n_args:
            raise TypeError("Dimension of value does not match.")
        self._checkindex(index)
        point_setter(index, value)

    # #### Internal Routines ####
    def _checkindex(self, index):
        "Check the given index."
        if not (0 <= index < self.size):
            raise IndexError(f"Invalid GEOS Geometry index: {index}")

    def _checkdim(self, dim):
        "Check the given dimension."
        if dim < 0 or dim > 3:
            raise GEOSException(f'Invalid ordinate dimension: "{dim:d}"')

    def _get_x(self, index):
        return capi.cs_getx(self.ptr, index, byref(c_double()))

    def _get_y(self, index):

View on GitHub (pinned to ae25a40be0)

Solutions

  1. Match the tuple length to the sequence dimension: inspect seq.dims, seq.hasz, seq.hasm first
  2. Construct the geometry with the correct dimension from the start (Point(x,y,z=True))
  3. Downcast coordinates before assigning: value[:2] for 2D targets

Example fix

// before
seq[0] = (10, 20)  # but seq is 3D (XYZ)
// after
seq[0] = (10, 20, 0)
# or rebuild as 2D
GEOSGeometry('POINT(10 20)')
Defensive patterns

Strategy: validation

Validate before calling

def dims_for(seq):
    if seq.dims == 3 and seq._z:
        return 3
    if seq.dims == 3 and seq.hasm:
        return 3
    if seq.dims == 4 and seq._z and seq.hasm:
        return 4
    return 2

def validate_coord_for(seq, value):
    n = dims_for(seq)
    if len(value) != n:
        raise ValueError(f'expected {n} ordinates, got {len(value)}')
    return value

Type guard

def matches_dims(seq, value) -> bool:
    return len(value) == dims_for(seq)

Try / catch

try:
    seq[i] = value
except TypeError as e:
    if 'Dimension' in str(e):
        seq[i] = tuple(value[:dims_for(seq)])
    raise

Prevention

When it happens

Trigger: Assigning seq[i] = (x, y) to a 3D sequence (needs (x,y,z)); assigning (x,y,z) to a 2D sequence; assigning a 4-tuple to a non-M geometry; mixing Z and M dimensions. Also triggered when hasm raises NotImplementedError on GEOS<3.14 inside the dimension detection.

Common situations: Building a Point/LineString and forgetting the Z value; assuming all sequences are 2D; copying coordinates from a 3D source into a 2D target.

Related errors


AI-assisted analysis of django/django@ae25a40be0 (2026-08-06). Data as JSON: /api/errors/9d0681ab0fe160ca. Report an issue: GitHub.