FoundationAgents/MetaGPT · error · LineNumberError
Invalid start line number: {start}. Line numbers must be bet
Error message
Invalid start line number: {start}. Line numbers must be between 1 and {len(lines)} (inclusive). What it means
Raised by Editor._edit_impl when the resolved start line is outside 1..len(lines). start defaults to 1 when None, so this fires only for explicitly supplied values: 0, negative numbers, values past the end of the file, or non-numeric garbage that survives comparison.
Source
Thrown at metagpt/tools/libs/editor.py:461
Args:
lines: list[str]: The lines in the original file.
start: int: The start line number for editing.
end: int: The end line number for editing.
content: str: The content to replace the lines with.
Returns:
content: str: The new content of the file.
n_added_lines: int: The number of lines added to the file.
"""
# Handle cases where start or end are None
if start is None:
start = 1 # Default to the beginning
if end is None:
end = len(lines) # Default to the end
# Check arguments
if not (1 <= start <= len(lines)):
raise LineNumberError(
f"Invalid start line number: {start}. Line numbers must be between 1 and {len(lines)} (inclusive)."
)
if not (1 <= end <= len(lines)):
raise LineNumberError(
f"Invalid end line number: {end}. Line numbers must be between 1 and {len(lines)} (inclusive)."
)
if start > end:
raise LineNumberError(f"Invalid line range: {start}-{end}. Start must be less than or equal to end.")
# Split content into lines and ensure it ends with a newline
if not content.endswith("\n"):
content += "\n"
content_lines = content.splitlines(True)
# Calculate the number of lines to be added
n_added_lines = len(content_lines)
# Remove the specified range of lines and insert the new contentView on GitHub (pinned to 11cdf466d0)
Solutions
- Re-read the file and recompute line numbers immediately before editing
- Convert 0-based indices to 1-based (add 1)
- Validate 1 <= start <= len(lines) in your wrapper before delegating to the editor
Example fix
// before editor.edit(path, start=0, end=3, content='x = 1\n') # ValueError // after editor.edit(path, start=1, end=3, content='x = 1\n')
Defensive patterns
Strategy: validation
Validate before calling
n = len(editor.current_file.read_text().splitlines()) start = min(max(1, int(start)), n) end = min(max(start, int(end)), n) editor.edit(path, start=start, end=end, content=content)
Type guard
def valid_range(start: int, end: int, n: int) -> bool:
return 1 <= start <= end <= n Try / catch
try:
editor.edit(path, start=s, end=e, content=c)
except LineNumberError as err:
# re-read and recompute, then retry once with clamped values
raise Prevention
- Editor line numbers are 1-based and inclusive
- Re-read the file immediately before computing ranges
- In wrappers, clamp inputs instead of trusting upstream arithmetic
When it happens
Trigger: editor.edit(path, start=0, end=5, content=...) or start beyond EOF; stale line numbers from a previous longer version of the file; 0-based indexing passed by mistake.
Common situations: Edits computed against outdated file snapshots (the file shrank since it was read); LLM agents guessing line numbers; converting 0-based diff hunks to 1-based incorrectly.
Related errors
- Line number must be between 1 and {total_lines}
- Invalid line number: {start}. Line numbers must be between 1
- Invalid end line number: {end}. Line numbers must be between
- Invalid line range: {start}-{end}. Start must be less than o
- Invalid file name.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/9ae5691b5322dcff.
Report an issue: GitHub.