redis/redis-py · error · DataError

len and idx cannot be provided together.

Error message

len and idx cannot be provided together.

What it means

Raised by stralgo() when both len=True and idx=True are passed. LEN returns only the numeric length of the longest common substring match, while IDX returns the match positions; the two output modes are incompatible and Redis rejects combining them. The library enforces this client-side.

Source

Thrown at redis/commands/core.py:4624

        will be keys or strings. strings is the default.
        ``len`` Returns just the len of the match.
        ``idx`` Returns the match positions in each string.
        ``minmatchlen`` Restrict the list of matches to the ones of a given
        minimal length. Can be provided only when ``idx`` set to True.
        ``withmatchlen`` Returns the matches with the len of the match.
        Can be provided only when ``idx`` set to True.

        For more information, see https://redis.io/commands/stralgo
        """
        # check validity
        supported_algo = ["LCS"]
        if algo not in supported_algo:
            supported_algos_str = ", ".join(supported_algo)
            raise DataError(f"The supported algorithms are: {supported_algos_str}")
        if specific_argument not in ["keys", "strings"]:
            raise DataError("specific_argument can be only keys or strings")
        if len and idx:
            raise DataError("len and idx cannot be provided together.")

        pieces: list[EncodableT] = [algo, specific_argument.upper(), value1, value2]
        if len:
            pieces.append(b"LEN")
        if idx:
            pieces.append(b"IDX")
        try:
            int(minmatchlen)
            pieces.extend([b"MINMATCHLEN", minmatchlen])
        except TypeError:
            pass
        if withmatchlen:
            pieces.append(b"WITHMATCHLEN")

        return self.execute_command(
            "STRALGO",
            *pieces,
            len=len,

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Choose either len=True (length only) or idx=True (positions), not both.
  2. If you need both, make two stralgo()/lcs() calls and combine the results client-side.

Example fix

# before
await r.stralgo('LCS', v1, v2, len=True, idx=True)
# after
await r.stralgo('LCS', v1, v2, idx=True)
Defensive patterns

Strategy: validation

Validate before calling

def validate_len_idx(length: bool, idx: bool) -> None:
    if length and idx:
        raise ValueError('len and idx cannot both be True for stralgo')

Prevention

When it happens

Trigger: Calling client.stralgo('LCS', v1, v2, len=True, idx=True).

Common situations: Reusing a kwargs dict that toggles both flags; wanting both the length and indices in one call (not supported by the command).

Related errors


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