langchain-ai/deepagents · error · ValueError

ReadResult.start_line and end_line must be set together or b

Error message

ReadResult.start_line and end_line must be set together or both left unset

What it means

ReadResult is a dataclass with invariants enforced in __post_init__: start_line and end_line must both be set or both be None, because a half-specified window would produce a next_offset that silently skips source lines. Backends constructing ReadResult incorrectly trigger this ValueError immediately.

Source

Thrown at libs/deepagents/deepagents/backends/protocol.py:250

    """

    def __post_init__(self) -> None:
        """Reject malformed pagination-field combinations at construction.

        The window fields are not independent: `start_line`/`end_line` are a
        pair, and neither `next_offset` nor `total_lines` describes anything
        without the window it refers to. Beyond co-presence, the values must
        agree numerically: a window runs forward (`1 <= start_line <=
        end_line`), the file is at least as long as the window
        (`total_lines >= end_line`), and the resume point is the 0-indexed line
        immediately after the last one shown (`next_offset == end_line`, since
        `end_line` is 1-indexed). Fail loudly here to keep a backend from
        emitting a `next_offset` that would silently skip unshown source lines
        once it reaches the middleware.
        """
        if (self.start_line is None) != (self.end_line is None):
            msg = "ReadResult.start_line and end_line must be set together or both left unset"
            raise ValueError(msg)
        if self.no_lines_requested and (
            self.error is not None or self.start_line is not None or self.next_offset is not None or self.total_lines is not None
        ):
            msg = "ReadResult.no_lines_requested describes an uninspected window; it cannot be combined with error or pagination fields"
            raise ValueError(msg)
        if self.next_offset is not None and self.start_line is None:
            msg = "ReadResult.next_offset requires start_line and end_line to be set"
            raise ValueError(msg)
        if self.total_lines is not None and self.start_line is None:
            msg = "ReadResult.total_lines requires start_line and end_line to be set"
            raise ValueError(msg)

        # Numeric consistency of a present window. `start_line`/`end_line` are
        # bound together above, so testing `start_line` covers both.
        if self.start_line is not None and self.end_line is not None:
            if self.start_line < 1 or self.end_line < self.start_line:
                msg = f"ReadResult window must satisfy 1 <= start_line <= end_line, got start_line={self.start_line}, end_line={self.end_line}"
                raise ValueError(msg)

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Set both start_line and end_line together when returning a paginated window
  2. Leave both unset when the read is not line-windowed
  3. Review the ReadResult docstring/protocol invariants and update your backend construction
  4. Add a unit test constructing your ReadResult values so post_init failures surface in CI

Example fix

# before
return ReadResult(content=body, start_line=1, next_offset=101)
# after
return ReadResult(content=body, start_line=1, end_line=100, next_offset=101)
Defensive patterns

Strategy: type-guard

Validate before calling

def valid_read_result_kwargs(**kw) -> bool:
    return (kw.get("start_line") is None) == (kw.get("end_line") is None)

Type guard

def has_full_window(r: ReadResult) -> bool:
    return r.start_line is not None and r.end_line is not None

Try / catch

try:
    result = ReadResult(content=body, start_line=s, end_line=e, next_offset=n)
except ValueError:
    result = ReadResult(content=body)  # no window, no pagination

Prevention

When it happens

Trigger: A custom backend calling ReadResult(..., start_line=1) without end_line, or setting end_line without start_line.

Common situations: Writing a custom FilesystemBackend or middleware that paginates reads and only fills in one of the two bounds; refactors that changed field names but not both call sites.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/42e11c4143ba203a. Report an issue: GitHub.