{"id":"c4770f35686c4dc8","repo":"psf/requests","slug":"chunk-size-must-be-an-int-it-is-instead-a-type-c","errorCode":null,"errorMessage":"chunk_size must be an int, it is instead a {type(chunk_size)}.","messagePattern":"chunk_size must be an int, it is instead a (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/requests/models.py","lineNumber":963,"sourceCode":"                    raise ConnectionError(e)\n                except SSLError as e:\n                    raise RequestsSSLError(e)\n            else:\n                # Standard file-like object.\n                while True:\n                    chunk = self.raw.read(chunk_size)\n                    if not chunk:\n                        break\n                    yield chunk\n\n            self._content_consumed = True\n\n        if self._content_consumed and isinstance(self._content, bool):\n            raise StreamConsumedError()\n        elif chunk_size is not None and not isinstance(\n            chunk_size, int\n        ):  # runtime guard for untyped callers\n            raise TypeError(\n                f\"chunk_size must be an int, it is instead a {type(chunk_size)}.\"\n            )\n\n        if self._content_consumed:\n            # simulate reading small chunks of the content\n            content = cast(bytes, self._content)\n            chunks = iter_slices(content, chunk_size)\n        else:\n            chunks = generate()\n\n        if decode_unicode:\n            chunks = stream_decode_response_unicode(chunks, self)\n\n        return chunks\n\n    @overload\n    def iter_lines(\n        self,","sourceCodeStart":945,"sourceCodeEnd":981,"githubUrl":"https://github.com/psf/requests/blob/414f0513c33883adf6f2b46901d4f0b38a455851/src/requests/models.py#L945-L981","documentation":"A `TypeError` raised by `Response.iter_content` when `chunk_size` is neither None nor an int. The chunk size is handed to `self.raw.read(chunk_size)` / `iter_slices`, which require an integer; the explicit guard produces a clear message instead of a confusing failure deep inside urllib3. It exists as a runtime check for untyped callers.","triggerScenarios":"`resp.iter_content(chunk_size='1024')` (string from config/CLI parsing), `chunk_size=1024.0` (float from arithmetic like `size/8`), or any non-int passed positionally: `resp.iter_content('1024')`. Also indirectly via `iter_lines(chunk_size=...)`, which forwards the value.","commonSituations":"Chunk size read from an env var, argparse without `type=int`, or a YAML/JSON config as a string; computing chunk size with `/` (true division yields float) instead of `//`; accidentally passing `decode_unicode` or another argument positionally into the chunk_size slot.","solutions":["Convert the value to int before the call: `resp.iter_content(chunk_size=int(cfg['chunk_size']))`.","If the value comes from division, use integer division: `chunk_size = total // parts`.","Use keyword arguments (`chunk_size=`, `decode_unicode=`) to avoid positional mix-ups."],"exampleFix":"# before\nchunk = os.environ.get('CHUNK_SIZE', '8192')\nfor part in resp.iter_content(chunk_size=chunk): ...\n\n# after\nchunk = int(os.environ.get('CHUNK_SIZE', '8192'))\nfor part in resp.iter_content(chunk_size=chunk): ...","handlingStrategy":"type-guard","validationCode":"if chunk_size is not None and not isinstance(chunk_size, int):\n    chunk_size = int(chunk_size)","typeGuard":"def valid_chunk_size(cs):\n    return cs is None or (isinstance(cs, int) and not isinstance(cs, bool) and cs > 0)","tryCatchPattern":"try:\n    for chunk in resp.iter_content(chunk_size=chunk_size):\n        ...\nexcept TypeError:\n    # chunk_size was a str/float (e.g. from config); coerce to int and retry once\n    raise","preventionTips":["Pass a literal int (or None) to iter_content/iter_lines — coerce config/env values with int() first","Watch for floats from arithmetic like size/10 — use // integer division","Default chunk_size=1 is valid; only override with a positive int"],"tags":["python","requests","iter-content","type-error","streaming"],"analyzedSha":"414f0513c33883adf6f2b46901d4f0b38a455851","analyzedAt":"2026-07-31T19:24:57.696Z","schemaVersion":2}