{"id":"387fe90bbcafebf0","repo":"redis/redis-py","slug":"xread-count-must-be-a-positive-integer","errorCode":null,"errorMessage":"XREAD count must be a positive integer","messagePattern":"XREAD count must be a positive integer","errorType":"validation","errorClass":"DataError","httpStatus":null,"severity":"error","filePath":"redis/commands/core.py","lineNumber":7913,"sourceCode":"\n        max_size: if set, a soft cap on the total server reply size in bytes\n                  across all streams combined. Measured server-side including\n                  protocol overhead, so it is not an exact application-payload\n                  size guarantee; a single available entry larger than the cap\n                  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\")","sourceCodeStart":7895,"sourceCodeEnd":7931,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/commands/core.py#L7895-L7931","documentation":"Raised by xread() when the per-stream count argument is not an int or is less than 1. count caps entries returned per stream; the library enforces int >= 1 client-side. Strict isinstance(count, int) check — strings and floats are rejected.","triggerScenarios":"Call client.xread(streams, count=...) with count as a float, string, zero, negative number, or non-int. Pass None or omit for no per-stream cap.","commonSituations":"Reusing a count variable that also feeds max_count; reading count from a config file as string; passing 0 to mean 'none'.","solutions":["Pass an int >= 1, or None for no per-stream limit.","Coerce explicitly: count = int(count) if count else None.","Remember count is per-stream; for a cumulative cap use max_count (Redis >= 8.10.0)."],"exampleFix":"// before\nclient.xread({\"s\": \"0\"}, count=10.0)\n// after\nclient.xread({\"s\": \"0\"}, count=10)","handlingStrategy":"validation","validationCode":"def normalize_count(v):\n    if v is None:\n        return None\n    if not isinstance(v, int) or isinstance(v, bool):\n        raise TypeError(f\"count must be int, got {type(v)}\")\n    if v < 1:\n        raise ValueError(f\"count must be >= 1, got {v}\")\n    return v\n\ncount = normalize_count(count)\nclient.xread({\"s\": \"0\"}, count=count)","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":"from redis.exceptions import DataError\ntry:\n    client.xread({\"s\": \"0\"}, count=count)\nexcept DataError as e:\n    if \"XREAD count\" in str(e):\n        client.xread({\"s\": \"0\"}, count=int(count))\n    else:\n        raise","preventionTips":["Coerce count once at the boundary: int(count) if provided.","Remember xread count is per-stream, not cumulative.","Reuse the same normalization helper across xrange/xread/xpending."],"tags":["redis-streams","validation","xread","dataerror"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}