{"record":{"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":"exception","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/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/models.py#L945-L981","documentation":"Raised by Response.iter_content() when chunk_size is neither an int nor None. This is a runtime type guard added for untyped callers who pass the wrong type — the type signature already requires int | None. Without it, a bad value (e.g. a string) would propagate into urllib3's raw.read() or iter_slices() and produce a confusing downstream TypeError.","triggerScenarios":"Calling response.iter_content(chunk_size=\"1024\"), response.iter_content(chunk_size=10.5) (float), response.iter_content(chunk_size=True) (bool is a subclass of int but edge cases exist), or passing any non-int/non-None value as chunk_size.","commonSituations":"Reading chunk_size from an environment variable or config file without converting from string; passing a float from a computed value (e.g. math.ceil result in some contexts); copy-pasting from documentation that shows a string literal.","solutions":["Ensure chunk_size is an int literal or an explicitly converted int variable: int(my_var).","Pass None when you want all data in a single chunk (stream=False behavior).","If reading from config, wrap with int(): response.iter_content(chunk_size=int(config_value))."],"exampleFix":"# before\nresp.iter_content(chunk_size=\"8192\")\n\n# after\nresp.iter_content(chunk_size=8192)","handlingStrategy":"type-guard","validationCode":"if chunk_size is not None and not isinstance(chunk_size, int):\n    raise TypeError(f'chunk_size must be int or None, got {type(chunk_size)}')\nfor chunk in resp.iter_content(chunk_size=chunk_size):\n    ...","typeGuard":"def is_valid_chunk_size(v) -> bool:\n    return v is None or (isinstance(v, int) and not isinstance(v, bool))","tryCatchPattern":null,"preventionTips":["Always pass int literals or explicitly converted int values to chunk_size.","Use a type checker (mypy/pyright) to catch type mismatches before runtime.","When reading chunk_size from config/env, always wrap with int()."],"tags":["type-error","response-streaming","chunk-size","input-validation"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}