redis/redis-py · error · DataError

``count`` is required when ``mode`` or ``ordering`` is set

Error message

``count`` is required when ``mode`` or ``ordering`` is set

What it means

lmovem() atomically moves list elements. The multi-element form requires count, and mode/ordering are only meaningful when count is given. The guard at core.py:3671 raises DataError when count is None but mode or ordering was supplied, since those args have no effect for a single-element move.

Source

Thrown at redis/commands/core.py:3672

        ``LEFT`` (head) or ``RIGHT`` (tail).

        When ``count`` is given, ``mode`` selects how many elements to move:
        ``COUNT`` (the default) moves up to ``count`` elements, while
        ``EXACTLY`` moves exactly ``count`` elements or nothing at all. When
        ``count`` is not given a single element is moved.

        ``ordering`` controls the order at the destination: ``OBO`` pushes each
        element as it is popped (reversing block order, stack semantics) while
        ``BULK`` preserves the original relative order (queue semantics). It is
        required whenever ``count`` is given.

        Returns the array of moved elements, or ``None`` if nothing was moved.

        For more information, see https://redis.io/commands/lmovem
        """
        if count is None:
            if mode is not None or ordering is not None:
                raise DataError(
                    "``count`` is required when ``mode`` or ``ordering`` is set"
                )
        elif ordering is None:
            raise DataError(
                "``ordering`` (OBO or BULK) is required when ``count`` is set"
            )
        pieces: list[EncodableT] = [first_list, second_list, src, dest]
        if count is not None:
            pieces.extend([mode or "COUNT", count, ordering])
        return self.execute_command("LMOVEM", *pieces)

    @overload
    def blmovem(
        self: SyncClientProtocol,
        first_list: str,
        second_list: str,
        timeout: float,
        src: str = "LEFT",

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. If you want multi-element move, supply count and ordering together.
  2. If you want a single-element move, omit mode and ordering entirely.
  3. Validate that mode/ordering are None whenever count is None before calling.

Example fix

# before
r.lmovem('src', 'dst', mode='COUNT')

# after
r.lmovem('src', 'dst', count=3, ordering='OBO')  # multi-element
# or for single element:
r.lmovem('src', 'dst')
Defensive patterns

Strategy: validation

Validate before calling

if count is None and (mode is not None or ordering is not None):
    raise ValueError('lmovem: mode/ordering require count')
r.lmovem('src', 'dst', count=count, mode=mode, ordering=ordering)

Type guard

def valid_lmovem_args(count, mode, ordering) -> bool:
    if count is None:
        return mode is None and ordering is None
    return True

Try / catch

from redis.exceptions import DataError
try:
    r.lmovem('src', 'dst', mode=mode, ordering=ordering)
except DataError as e:
    if 'count is required' in str(e):
        r.lmovem('src', 'dst')  # single-element move
    else:
        raise

Prevention

When it happens

Trigger: r.lmovem('src','dst', mode='COUNT') or r.lmovem('src','dst', ordering='OBO') without count. The single-element path (no count) ignores both.

Common situations: Building a move helper that always forwards mode/ordering; misunderstanding which args belong to the count variant.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/2cd86443d1a4b1a8.json. Report an issue: GitHub.