feat(environment): stream lines in ReadFileTool to prevent OOM on large files

Đang mở
#7,131 3 bình luận 0 reaction 1 người được giao Xem trên GitHub

@sanketpatil06 đang làm issue này rồi.

Từ ngày 16/9/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

needs review tools

Required Information

Is your feature request related to a specific problem?

Currently, ReadFileTool loads an entire file into memory as bytes via await self._environment.read_file(path) and then splits lines across the whole payload before slicing the requested range (start_line to end_line).

For very large files (hundreds of megabytes or gigabytes), reading the entire file into memory risks high memory usage or Out-Of-Memory (OOM) crashes in the agent environment, even when the caller only requests a small range of lines. There is an explicit TODO in src/google/adk/tools/environment/_read_file_tool.py:
# TODO: Avoid loading the entire file into memory to prevent OOM on large files.

Describe the Solution You'd Like
  1. Add a read_file_lines(path, start_line, end_line) method to BaseEnvironment with a backward-compatible default fallback that calls read_file.
  2. Implement an optimized, streaming read_file_lines on LocalEnvironment that iterates through the file lazily line-by-line in $O(1)$ memory, only collecting the requested range.
  3. Update ReadFileTool.run_async to call read_file_lines, eliminating the need to buffer the entire file into memory.
Impact on your work

Improves stability and performance of file inspection tools in agent environments handling large log files, datasets, and source files.

Willingness to contribute

Yes, I have an implementation ready with unit tests and pre-commit checks passing.


Recommended Information

Proposed API / Implementation
# BaseEnvironment
async def read_file_lines(
    self,
    path: str | Path,
    start_line: int = 1,
    end_line: int | None = None,
) -> tuple[list[bytes], int]: ...

# LocalEnvironment
@staticmethod
def _sync_read_lines(
    path: Path, start_line: int, end_line: int | None = None
) -> tuple[list[bytes], int]:
  selected_lines: list[bytes] = []
  total_lines = 0
  with open(path, 'rb') as f:
    for line in f:
      total_lines += 1
      if start_line <= total_lines and (
          end_line is None or total_lines <= end_line
      ):
        selected_lines.append(line)
  return selected_lines, total_lines
Ngôn ngữ chính
Python
Star
21.6k
Fork
4k
Merge trung bình
13 giờ 49 phút
Pull request đã merge (30 ngày)
10

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của google/adk-python

Tất cả issue của google/adk-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.