{"record":{"id":"21429cfb89b883f8","repo":"redis/redis-py","slug":"xread-max-count-must-be-a-positive-integer","errorCode":null,"errorMessage":"XREAD max_count must be a positive integer","messagePattern":"XREAD max_count must be a positive integer","errorType":"validation","errorClass":"DataError","httpStatus":null,"severity":"error","filePath":"redis/commands/core.py","lineNumber":7918,"sourceCode":"                  may still be returned. Must be a positive integer. Requires\n                  Redis >= 8.10.0.\n\n        For more information, see https://redis.io/commands/xread\n        \"\"\"\n        pieces = []\n        if block is not None:\n            if not isinstance(block, int) or block < 0:\n                raise DataError(\"XREAD block must be a non-negative integer\")\n            pieces.append(b\"BLOCK\")\n            pieces.append(str(block))\n        if count is not None:\n            if not isinstance(count, int) or count < 1:\n                raise DataError(\"XREAD count must be a positive integer\")\n            pieces.append(b\"COUNT\")\n            pieces.append(str(count))\n        if max_count is not None:\n            if not isinstance(max_count, int) or max_count < 1:\n                raise DataError(\"XREAD max_count must be a positive integer\")\n            if count is not None and max_count < count:\n                raise DataError(\n                    \"XREAD max_count must be greater than or equal to count\"\n                )\n            pieces.append(b\"MAXCOUNT\")\n            pieces.append(str(max_count))\n        if max_size is not None:\n            if not isinstance(max_size, int) or max_size < 1:\n                raise DataError(\"XREAD max_size must be a positive integer\")\n            pieces.append(b\"MAXSIZE\")\n            pieces.append(str(max_size))\n        if not isinstance(streams, dict) or len(streams) == 0:\n            raise DataError(\"XREAD streams must be a non empty dict\")\n        pieces.append(b\"STREAMS\")\n        keys, values = zip(*streams.items())\n        pieces.extend(keys)\n        pieces.extend(values)\n        response = self.execute_command(\"XREAD\", *pieces, keys=keys)","sourceCodeStart":7900,"sourceCodeEnd":7936,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/commands/core.py#L7900-L7936","documentation":"Raised by xread() (redis/commands/core.py:7918) as a DataError when max_count is not an int or is < 1. max_count is the cumulative MAXCOUNT cap across all streams (unlike per-stream count) and requires Redis >= 8.10.0. A separate check at line 7919 also enforces max_count >= count when both are set.","triggerScenarios":"Calling client.xread(streams, max_count=0), max_count=-1, max_count='500', or max_count=100.5. Also raised indirectly if max_count < count. Pass None to disable.","commonSituations":"Using max_count against an older Redis (< 8.10.0) where the server rejects MAXCOUNT, setting max_count smaller than count (logical contradiction), or env-sourced string values.","solutions":["Pass an int >= 1 for max_count, or None to disable the cumulative cap.","Ensure max_count >= count when both are set (the library enforces this at line 7919).","Verify your Redis server is >= 8.10.0 before using MAXCOUNT/MAXSIZE."],"exampleFix":"# before\nclient.xread(streams, count=100, max_count=50)\n# after\nclient.xread(streams, count=50, max_count=100)","handlingStrategy":"validation","validationCode":"def safe_xread_max_count(count, max_count):\n    if max_count is None:\n        return None\n    if not isinstance(max_count, int) or isinstance(max_count, bool) or max_count < 1:\n        raise DataError('XREAD max_count must be a positive int')\n    if count is not None and max_count < count:\n        raise DataError('XREAD max_count must be >= count')\n    return max_count","typeGuard":"def is_valid_max_count(count, mc) -> bool:\n    return isinstance(mc, int) and not isinstance(mc, bool) and mc >= 1 and (count is None or mc >= count)","tryCatchPattern":"from redis.exceptions import DataError\ntry:\n    client.xread(streams, count=c, max_count=mc)\nexcept DataError as e:\n    if 'max_count' in str(e):\n        client.xread(streams, count=c)  # drop the cumulative cap\n    else:\n        raise","preventionTips":["Verify Redis server version >= 8.10.0 before using MAXCOUNT/MAXSIZE.","Always set max_count >= count when both are used.","Treat max_count as optional - omit it for per-stream-only limiting."],"tags":["streams","xread","argument-validation","dataerror","version-gated"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}