oraios/serena · error · ValueError

Invalid Content-Length header: {value!r}

Error message

Invalid Content-Length header: {value!r}

What it means

This ValueError is raised by the LSP protocol handler when parsing the stdout stream of a language server process. The handler reads HTTP-style headers and expects a 'Content-Length: <int>' header; if the numeric part cannot be converted to an integer, the stream is considered corrupt and this error is thrown.

Source

Thrown at src/solidlsp/lsp_protocol_handler/server.py:160

        body,
    )


class MessageType:
    error = 1
    warning = 2
    info = 3
    log = 4


def content_length(line: bytes) -> int | None:
    if line.startswith(b"Content-Length: "):
        _, value = line.split(b"Content-Length: ")
        value = value.strip()
        try:
            return int(value)
        except ValueError:
            raise ValueError(f"Invalid Content-Length header: {value!r}")
    return None

View on GitHub (pinned to 7fcbca7e62)

Solutions

  1. Inspect the language server's stdout directly (run the server binary manually) to see what malformed Content-Length line it emits
  2. Verify the correct, LSP-compliant server binary is installed and on PATH (check version; reinstall if corrupt or mismatched)
  3. Check for wrapper scripts or environment hooks (shell profiles, proxies, logging wrappers) that pollute the server's stdout
  4. If the server is known to emit stray output, patch or upgrade solidlsp's handler to skip non-conforming header lines instead of raising
  5. Ensure the server process is launched with stdout used exclusively for LSP protocol (disable any verbose/log-to-stdout options)

Example fix

// before: handler raises on any non-integer Content-Length value
value = value.strip()
try:
    return int(value)
except ValueError:
    raise ValueError(f"Invalid Content-Length header: {value!r}")

// after (library-side hardening): ignore malformed header lines and keep reading
value = value.strip()
try:
    return int(value)
except ValueError:
    log.warning(f"Ignoring invalid Content-Length header: {value!r}")
    return None
Defensive patterns

Strategy: validation

Validate before calling

import re

def has_valid_content_length(line: bytes) -> bool:
    if line.startswith(b"Content-Length: "):
        value = line.split(b"Content-Length: ", 1)[1].strip()
        return value.isdigit() and int(value) >= 0
    return True

# Before feeding header lines to the parser / launching the server:
# assert all(has_valid_content_length(l) for l in header_lines)

Type guard

def parse_content_length(line: bytes) -> int | None:
    if not line.startswith(b"Content-Length: "):
        return None
    value = line.split(b"Content-Length: ", 1)[1].strip()
    if not value.isdigit():
        return None
    return int(value)

Try / catch

try:
    length = content_length(line)
except ValueError as e:
    logging.warning("Malformed LSP header from server: %s", e)
    length = None  # skip line and continue reading the stream

Prevention

When it happens

Trigger: The language server process writes a line starting with 'Content-Length: ' whose value is not a valid integer (e.g. 'Content-Length: abc', 'Content-Length: ??', or an empty/whitespace-only value). This happens in content_length(), called from _read_ls_process_stdout/_read_loop while reading the server's stdout.

Common situations: A misbehaving or non-LSP-compliant language server binary is on PATH (wrong version, wrong executable selected); the server emits diagnostics/log lines that accidentally match the header format; stdout is contaminated by shell wrappers, scripts, or proxy processes; or the binary prints localized/garbage output on startup due to environment/locale issues.

Related errors


AI-assisted analysis of oraios/serena@7fcbca7e62 (2026-08-29). Data as JSON: /api/errors/f6886475e69a27cc. Report an issue: GitHub.