aio-libs/aiohttp · error · LineTooLong

Got more than {limit} bytes when reading: {line!r}.

Error message

Got more than {limit} bytes when reading: {line!r}.

What it means

Raised by HeadersParser.parse_headers (aiohttp/http_parser.py:210) only in lax mode while accumulating obsolete line-folding continuation lines (RFC 9112 section 5.2, deprecated since RFC 7230). If the running length of a folded header value plus its continuation lines exceeds max_field_size (default 8190), the parser aborts with LineTooLong. The strict request parser never processes continuation lines, so this is response-parser specific.

Source

Thrown at aiohttp/http_parser.py:210

            if not TOKENRE.fullmatch(name):
                raise InvalidHeader(bname)

            # next line
            lines_idx += 1
            line = lines[lines_idx]

            # consume continuation lines
            continuation = self._lax and line and line[0] in (32, 9)  # (' ', '\t')

            # Deprecated: https://www.rfc-editor.org/rfc/rfc9112.html#name-obsolete-line-folding
            if continuation:
                header_length = len(bvalue)
                bvalue_lst = [bvalue]
                while continuation:
                    header_length += len(line)
                    if header_length > self.max_field_size:
                        header_line = bname + b": " + b"".join(bvalue_lst)
                        raise LineTooLong(
                            header_line[:100] + b"...", self.max_field_size
                        )
                    bvalue_lst.append(line)

                    # next line
                    lines_idx += 1
                    if lines_idx < line_count:
                        line = lines[lines_idx]
                        if line:
                            continuation = line[0] in (32, 9)  # (' ', '\t')
                    else:
                        line = b""
                        break
                bvalue = b"".join(bvalue_lst)

            bvalue = bvalue.strip(b" \t")
            value = bvalue.decode("utf-8", "surrogateescape")

View on GitHub (pinned to c0ef574e29)

Solutions

  1. Raise max_field_size when constructing the response parser / ClientSession if the upstream legitimately sends large folded headers.
  2. If you control the server, send single-line header values instead of relying on deprecated line folding.
  3. If you cannot change the upstream, treat the response as unrecoverable and retry or fall back.

Example fix

# before - default limit (8190) too small for upstream's folded header
async with aiohttp.ClientSession() as s:
    async with s.get(url) as r:
        ...
# after - raise the field-size limit
async with aiohttp.ClientSession(
    response_class=lambda *a, **kw: aiohttp.ClientResponse(*a, **kw)
) as s:
    ...  # configure max_field_size via a custom protocol/parser if needed,
         # or filter at the proxy layer
Defensive patterns

Strategy: try-catch

Try / catch

from aiohttp import http_exceptions
try:
    async with session.get(url) as resp:
        body = await resp.read()
except http_exceptions.LineTooLong as e:
    log.warning("upstream folded header too long (limit=%d)", e.args[1])
    raise

Prevention

When it happens

Trigger: An HTTP response whose header uses obsolete line folding (a continuation line beginning with space or tab) and whose folded value is longer than max_field_size bytes. Only the lax HttpResponseParser (the default for responses) enters the continuation loop.

Common situations: Legacy servers that still fold long header values; very large Set-Cookie or WWW-Authenticate values that some intermediary folds across lines; or non-compliant gateways.

Related errors


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