Skip to content

Add offset and size parameters to read_file tool#19

Merged
Rikul merged 3 commits into
mainfrom
claude/readfile-offset-size-8gl8l7
Jul 12, 2026
Merged

Add offset and size parameters to read_file tool#19
Rikul merged 3 commits into
mainfrom
claude/readfile-offset-size-8gl8l7

Conversation

@Rikul

@Rikul Rikul commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Summary

Extends the read_file tool to support partial file reads via optional offset and size parameters, enabling efficient reading of specific byte ranges from files.

Key Changes

  • Tool signature: Added offset (int, default 0) and size (int | None, default None) parameters to read_file.call()
  • Implementation: Changed file reading from text mode to binary mode ("rb") to support byte-level offset/size operations, then decode the result to UTF-8
  • File seeking: Uses f.seek(offset) to position the file pointer before reading
  • Flexible reading: Respects size parameter when provided; reads to EOF if size is None
  • Schema update: Updated OpenAI function spec to document the new parameters and updated description
  • Comprehensive tests: Added 5 new test cases covering:
    • Reading with offset only
    • Reading with size only
    • Reading with both offset and size
    • Offset beyond file end (returns empty string)
    • Size larger than remaining bytes (returns available data)

Implementation Details

  • Binary mode ("rb") allows precise byte-level control via seek() and read(size)
  • UTF-8 decoding happens after reading, maintaining the original text interface
  • Graceful handling of edge cases: offsets beyond EOF and sizes exceeding remaining content
  • Logging updated to include offset and size parameters for debugging

https://claude.ai/code/session_017kEbY4DcgDd5sKMSauxyWf

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the read_file tool to support partial reads by introducing optional offset and size parameters, along with corresponding unit tests. The feedback highlights potential issues with byte-level slicing of multi-byte UTF-8 characters, which could lead to decoding errors, and suggests handling this gracefully. Additionally, the reviewer recommends validating that offset and size are non-negative to prevent unexpected behavior, and adding tests to cover these edge cases.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread app/tools/read_file.py Outdated
Comment thread app/tools/read_file.py
Comment thread tests/test_read_file.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 891aa7f93f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/tools/read_file.py Outdated
if offset:
f.seek(offset)
data = f.read(size) if size is not None else f.read()
return data.decode("utf-8")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle UTF-8 chunks split at byte boundaries

When the requested byte window cuts through a multi-byte UTF-8 character, this decode raises UnicodeDecodeError and the tool returns an error even though the file is valid UTF-8; for example size=1 on a file starting with é, or an offset into that character, fails. Since the new API exposes arbitrary byte offsets and sizes, partial reads of non-ASCII text only work when callers happen to choose character-aligned ranges, so the tool should tolerate or adjust boundary splits instead of treating them as read failures.

Useful? React with 👍 / 👎.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copilot finished work on behalf of Rikul July 6, 2026 04:12
@Rikul Rikul merged commit fbbdd5e into main Jul 12, 2026
2 checks passed
@Rikul Rikul deleted the claude/readfile-offset-size-8gl8l7 branch July 12, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants