aio-libs/aiohttp · error · TransferEncodingError

Not enough data to satisfy transfer length header.

Error message

Not enough data to satisfy transfer length header.

What it means

TransferEncodingError raised in feed_eof when a chunked-encoded body stream ends without the terminating zero-length chunk. In PARSE_CHUNKED mode, feed_eof means EOF arrived before the `0\r\n\r\n` terminator.

Source

Thrown at aiohttp/http_parser.py:923

    def feed_eof(self) -> None:
        if self._type == ParseState.PARSE_UNTIL_EOF:
            self._eof_pending = True
            while self._more_data_available:
                if self._paused:
                    self._paused = False
                    return  # Will resume via feed_data(b"") later
                self._more_data_available = self.payload.feed_data(b"")
            self.payload.feed_eof()
            self.done = True
            self._eof_pending = False
        elif self._type == ParseState.PARSE_LENGTH:
            received = self._length_expected - self._length
            raise ContentLengthError(
                f"Not enough data to satisfy content length header "
                f"(received {received} of {self._length_expected} bytes)."
            )
        elif self._type == ParseState.PARSE_CHUNKED:
            raise TransferEncodingError(
                "Not enough data to satisfy transfer length header."
            )

    def feed_data(
        self, chunk: bytes, SEP: _SEP = b"\r\n", CHUNK_EXT: bytes = b";"
    ) -> tuple[PayloadState, bytes]:
        """Receive a chunk of data to process.

        Return:
            PayloadState - The current state of payload processing.
                           This function may be called with empty bytes after returning
                           PAYLOAD_HAS_PENDING_INPUT to continue processing after a pause.
            bytes - If payload is complete, this is the unconsumed bytes intended for the
                    next message/payload, b"" otherwise.
        """
        # Read specified amount of bytes
        if self._type == ParseState.PARSE_LENGTH:
            if self._chunk_tail:

View on GitHub (pinned to c0ef574e29)

Solutions

  1. Confirm the upstream sends the terminating `0\r\n\r\n` for chunked bodies.
  2. Check for intermediary timeouts killing the stream.
  3. Handle the error on the read side and close the response cleanly.
  4. Retry with a non-streaming request to isolate.
Defensive patterns

Strategy: try-catch

Try / catch

from aiohttp.http_exceptions import TransferEncodingError
try:
    async for data in resp.content.iter_chunked(1024):
        ...
except TransferEncodingError:
    log.warning('chunked stream truncated')

Prevention

When it happens

Trigger: Server uses Transfer-Encoding: chunked but the connection closes before sending the final zero-size chunk. feed_eof() in the PARSE_CHUNKED branch raises TransferEncodingError.

Common situations: Server crash mid-stream; proxy cutting a long chunked response; SSE/streaming endpoint that never sends a terminator; broken upstream that omits the final chunk.

Related errors


AI-assisted analysis of aio-libs/aiohttp@c0ef574e29 (2026-08-04). Data as JSON: /data/errors/24859c8172b3e955.json. Report an issue: GitHub.