{"record":{"id":"02b0ef858b772d4d","repo":"redis/redis-py","slug":"xadd-maxlen-must-be-non-negative-integer","errorCode":null,"errorMessage":"XADD maxlen must be non-negative integer","messagePattern":"XADD maxlen must be non-negative integer","errorType":"validation","errorClass":"DataError","httpStatus":null,"severity":"error","filePath":"redis/commands/core.py","lineNumber":7041,"sourceCode":"\n        if ref_policy is not None and ref_policy not in {\"KEEPREF\", \"DELREF\", \"ACKED\"}:\n            raise DataError(\"XADD ref_policy must be one of: KEEPREF, DELREF, ACKED\")\n\n        if nomkstream:\n            pieces.append(b\"NOMKSTREAM\")\n        if ref_policy is not None:\n            pieces.append(ref_policy)\n        if idmpauto is not None:\n            pieces.extend([b\"IDMPAUTO\", idmpauto])\n        if idmp is not None:\n            if not isinstance(idmp, tuple) or len(idmp) != 2:\n                raise DataError(\n                    \"XADD idmp must be a tuple of (producer_id, idempotent_id)\"\n                )\n            pieces.extend([b\"IDMP\", idmp[0], idmp[1]])\n        if maxlen is not None:\n            if not isinstance(maxlen, int) or maxlen < 0:\n                raise DataError(\"XADD maxlen must be non-negative integer\")\n            pieces.append(b\"MAXLEN\")\n            if approximate:\n                pieces.append(b\"~\")\n            pieces.append(str(maxlen))\n        if minid is not None:\n            pieces.append(b\"MINID\")\n            if approximate:\n                pieces.append(b\"~\")\n            pieces.append(minid)\n        if limit is not None:\n            pieces.extend([b\"LIMIT\", limit])\n        pieces.append(id)\n        if not isinstance(fields, dict) or len(fields) == 0:\n            raise DataError(\"XADD fields must be a non-empty dict\")\n        for pair in fields.items():\n            pieces.extend(pair)\n        return self.execute_command(\"XADD\", name, *pieces)\n","sourceCodeStart":7023,"sourceCodeEnd":7059,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/core.py#L7023-L7059","documentation":"Raised by xadd() when maxlen is not an int or is negative. MAXLEN trimming requires a non-negative integer count; negative counts and string values like '1000' are rejected client-side before building the command. It is a DataError.","triggerScenarios":"Calling client.xadd('s', fields, maxlen=-5), maxlen='1000' (string), or maxlen=1.5 (float).","commonSituations":"Reading maxlen from a config file as a string, computing it from an expression that can go negative, or using a float from division.","solutions":["Pass maxlen as a non-negative int (e.g. maxlen=1000).","Coerce config values with int() and clamp to >= 0 before calling xadd."],"exampleFix":"# before\nclient.xadd('s', {'f': 'v'}, maxlen='1000')\n\n# after\nclient.xadd('s', {'f': 'v'}, maxlen=1000)","handlingStrategy":"type-guard","validationCode":"if maxlen is not None and (not isinstance(maxlen, int) or isinstance(maxlen, bool) or maxlen < 0):\n    raise ValueError('maxlen must be a non-negative int')\nclient.xadd(name, fields, maxlen=maxlen)","typeGuard":"def is_valid_maxlen(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0\n\n# usage\nif maxlen is not None and not is_valid_maxlen(maxlen):\n    raise TypeError('maxlen must be a non-negative integer')","tryCatchPattern":"from redis.exceptions import DataError\ntry:\n    client.xadd(name, fields, maxlen=maxlen)\nexcept DataError as e:\n    if 'maxlen must be non-negative' in str(e):\n        client.xadd(name, fields, maxlen=int(maxlen))","preventionTips":["Coerce config-sourced maxlen with int() and clamp to >= 0.","Watch for bool values: isinstance(True, int) is True, so guard against accidental booleans."],"tags":["streams","xadd","trimming","type-check","validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}