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 and idx are True simultaneously. LEN returns only the length of the match while IDX returns match positions; requesting both in STRALGO is ambiguous/unsupported, so the client rejects it before sending. It is a DataError.

Solutions

  1. Use idx=True alone; the IDX output already includes match length information when withmatchlen=True is also set.
  2. If you only need the numeric length, use len=True alone.

Example fix

# before
client.stralgo('LCS', v1, v2, len=True, idx=True)

# after
client.stralgo('LCS', v1, v2, idx=True, withmatchlen=True)
Defensive patterns

Strategy: validation

Validate before calling

if len and idx:
    raise ValueError('Use idx=True with withmatchlen=True; do not pass len=True together with idx')
client.stralgo('LCS', v1, v2, len=len, idx=idx, withmatchlen=idx)

Try / catch

from redis.exceptions import DataError
try:
    client.stralgo('LCS', v1, v2, len=True, idx=True)
except DataError as e:
    if 'len and idx' in str(e):
        client.stralgo('LCS', v1, v2, idx=True, withmatchlen=True)

Prevention

When it happens

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

Common situations: Wanting both the match length and positions in one call, or copying idx=True from one call and len=True from another into the same invocation.

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/45784d1172d5ac59. Report an issue: GitHub.

Appendix: 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 6a6b581b48)