{"id":"efe0c08e8859dd4a","repo":"aio-libs/aiohttp","slug":"cannot-compute-fallback-encoding-of-a-not-yet-read","errorCode":null,"errorMessage":"Cannot compute fallback encoding of a not yet read body","messagePattern":"Cannot compute fallback encoding of a not yet read body","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"aiohttp/client_reqrep.py","lineNumber":730,"sourceCode":"\n    def get_encoding(self) -> str:\n        ctype = self.headers.get(hdrs.CONTENT_TYPE, \"\").lower()\n        mimetype = parse_mimetype(ctype)\n\n        encoding = mimetype.parameters.get(\"charset\")\n        if encoding:\n            with contextlib.suppress(LookupError, ValueError):\n                return codecs.lookup(encoding).name\n\n        if mimetype.type == \"application\" and (\n            mimetype.subtype == \"json\" or mimetype.subtype == \"rdap\"\n        ):\n            # RFC 7159 states that the default encoding is UTF-8.\n            # RFC 7483 defines application/rdap+json\n            return \"utf-8\"\n\n        if self._body is None:\n            raise RuntimeError(\n                \"Cannot compute fallback encoding of a not yet read body\"\n            )\n\n        return self._resolve_charset(self, self._body)\n\n    async def text(self, encoding: str | None = None, errors: str = \"strict\") -> str:\n        \"\"\"Read response payload and decode.\"\"\"\n        await self.read()\n\n        if encoding is None:\n            encoding = self.get_encoding()\n\n        return self._body.decode(encoding, errors=errors)  # type: ignore[union-attr]\n\n    async def json(\n        self,\n        *,\n        encoding: str | None = None,","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/client_reqrep.py#L712-L748","documentation":"Raised as RuntimeError in ClientResponse.get_encoding() when charset fallback logic needs to inspect the body (via _resolve_charset) but self._body is still None. get_encoding() normally reads charset from Content-Type or defaults to utf-8 for JSON; the body-based fallback only works after the body has been read into memory.","triggerScenarios":"Fires at line 729-732 when get_encoding() is called directly before read(). Note text() and json() both call await self.read() before get_encoding(), so this only triggers when a user calls get_encoding() prematurely, or when a custom _resolve_charset callback path is invoked on an unread response.","commonSituations":"Calling response.get_encoding() manually before response.read(); custom subclasses that invoke charset detection early; misordered helper code in tests or middleware.","solutions":["Call `await response.read()` before calling `response.get_encoding()`.","Prefer response.text() / response.json() which handle reading + encoding internally.","If charset is known, pass it explicitly to text(encoding=...) to bypass auto-detection."],"exampleFix":"# before\nenc = response.get_encoding()      # body not yet read\nbody = await response.read()\n# after\nbody = await response.read()\nenc = response.get_encoding()","handlingStrategy":"validation","validationCode":"body = await response.read()\nencoding = response.get_encoding()  # safe now","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call await resp.read() before get_encoding().","Prefer resp.text() / resp.json() which handle ordering for you.","Do not expose get_encoding() in helper APIs without first reading the body."],"tags":["encoding","charset","response-handling","http-client"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}