FoundationAgents/MetaGPT · error · ValueError

Line number must be between 1 and {total_lines}

Error message

Line number must be between 1 and {total_lines}

What it means

Raised by Editor.open_file when the line_number argument is not an int, is below 1, or exceeds the file's total line count. The editor counts the file's lines first and requires line_number to land within [1, total_lines] before it positions the window there.

Source

Thrown at metagpt/tools/libs/editor.py:304

        Args:
            path: str: The path to the file to open, preferred absolute path.
            line_number: int | None = 1: The line number to move to. Defaults to 1.
            context_lines: int | None = 100: Only shows this number of lines in the context window (usually from line 1), with line_number as the center (if possible). Defaults to 100.
        """
        if context_lines is None:
            context_lines = self.window

        path = self._try_fix_path(path)

        if not path.is_file():
            raise FileNotFoundError(f"File {path} not found")

        self.current_file = path
        with path.open() as file:
            total_lines = max(1, sum(1 for _ in file))

        if not isinstance(line_number, int) or line_number < 1 or line_number > total_lines:
            raise ValueError(f"Line number must be between 1 and {total_lines}")
        self.current_line = line_number

        # Override WINDOW with context_lines
        if context_lines is None or context_lines < 1:
            context_lines = self.window

        output = self._cur_file_header(path, total_lines)
        output += self._print_window(path, self.current_line, self._clamp(context_lines, 1, 2000))
        self.resource.report(path, "path")
        return output

    def goto_line(self, line_number: int) -> str:
        """Moves the window to show the specified line number.

        Args:
            line_number: int: The line number to move to.
        """
        self._check_current_file()

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Clamp line_number to 1..total_lines before the call (count lines yourself, or just use the default line_number=1)
  2. Coerce string arguments to int before calling
  3. For 0-based line numbers from another tool, add 1 before passing

Example fix

// before
editor.open_file(path, line_number=0)  # ValueError

// after
line_number = max(1, int(line_number))
editor.open_file(path, line_number=line_number)
Defensive patterns

Strategy: validation

Validate before calling

line_number = max(1, int(line_number))
total = sum(1 for _ in open(path))
line_number = min(line_number, total)
editor.open_file(path, line_number=line_number)

Type guard

def valid_line(n, total: int) -> bool:
    return isinstance(n, int) and 1 <= n <= total

Try / catch

try:
    editor.open_file(path, line_number=n)
except ValueError:
    editor.open_file(path)  # fall back to default line 1

Prevention

When it happens

Trigger: editor.open_file(path, line_number=0), line_number=-3, line_number=500 on a 120-line file, or line_number='10' (a string, failing the isinstance int check). Also line_number=1 on an empty file is fine (total is clamped to 1), but any value >1 on a 1-line file fails.

Common situations: LLM agents emitting 0-based line numbers or hallucinated large line numbers; off-by-one when translating an editor/IDE cursor position; passing line numbers as strings from JSON tool arguments.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/89d03d4c1fec9c57. Report an issue: GitHub.