{"record":{"id":"d7997d5baf4be9bb","repo":"psf/requests","slug":"the-content-for-this-response-was-already-consumed","errorCode":null,"errorMessage":"The content for this response was already consumed","messagePattern":"The content for this response was already consumed","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/requests/models.py","lineNumber":1041,"sourceCode":"\n            if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:\n                pending = lines.pop()\n            else:\n                pending = None\n\n            yield from lines\n\n        if pending is not None:\n            yield pending\n\n    @property\n    def content(self) -> bytes:\n        \"\"\"Content of the response, in bytes.\"\"\"\n\n        if self._content is False:\n            # Read the contents.\n            if self._content_consumed:\n                raise RuntimeError(\"The content for this response was already consumed\")\n\n            if self.status_code == 0 or self.raw is None:\n                self._content = None\n            else:\n                self._content = b\"\".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b\"\"\n\n        self._content_consumed = True\n        # don't need to release the connection; that's been handled by urllib3\n        # since we exhausted the data.\n        return self._content  # type: ignore[return-value]\n\n    @property\n    def text(self) -> str:\n        \"\"\"Content of the response, in unicode.\n\n        If Response.encoding is None, encoding will be guessed using\n        ``charset_normalizer`` or ``chardet``.\n","sourceCodeStart":1023,"sourceCodeEnd":1059,"githubUrl":"https://github.com/psf/requests/blob/8068356288978c4f54661ae6f95afe0e0831885e/src/requests/models.py#L1023-L1059","documentation":"Raised by the Response.content property when the underlying stream has already been consumed (e.g. via iter_content with stream=True) but _content was never populated. This is a RuntimeError, not the more specific StreamConsumedError (which is raised from iter_content itself). It signals an irreversible state: the socket data was read and discarded, so .content cannot materialize the bytes retroactively.","triggerScenarios":"Calling resp.iter_content() on a streaming response (stream=True) and then accessing resp.content afterwards; iterating over resp.iter_lines() and then calling resp.content; calling resp.content after resp.content on a streaming response whose _content is still False but _content_consumed was set True by a prior generator iteration.","commonSituations":"Mixing streaming and non-streaming access patterns on the same Response object; a helper function that calls iter_content() internally followed by code that reads .content; refactoring from stream=False to stream=True without updating downstream .content access.","solutions":["Decide on ONE access pattern: either use stream=False and read .content, or use stream=True and fully consume iter_content() — never both.","If you already streamed and need the bytes, collect them during iteration: data = b''.join(resp.iter_content(8192)).","Buffer the stream into a variable early if you need to re-read: raw = resp.content before any streaming call."],"exampleFix":"# before (stream=True)\nfor chunk in resp.iter_content(8192):\n    process(chunk)\nbody = resp.content  # RuntimeError\n\n# after — collect during iteration\nbody = b''.join(resp.iter_content(8192))","handlingStrategy":"validation","validationCode":"if resp._content_consumed and isinstance(resp._content, bool):\n    raise RuntimeError('Content already consumed via streaming')\nbody = resp.content","typeGuard":null,"tryCatchPattern":"try:\n    body = resp.content\nexcept RuntimeError:\n    # Stream was consumed; re-fetch or use buffered data\n    body = cached_stream_data","preventionTips":["Never mix iter_content/iter_lines with .content on the same Response.","When using stream=True, collect all bytes during iteration: body = b''.join(resp.iter_content(8192)).","Document which functions consume a response's stream to prevent accidental double-reads."],"tags":["response-streaming","content-consumed","state-error","runtime-error"],"backgroundTag":null,"analyzedSha":"8068356288978c4f54661ae6f95afe0e0831885e","analyzedAt":"2026-08-11T20:11:09.238Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}