GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: prevent OOM by using file handles and chunked base64 encoding by anthonymq · Pull Request #1951 · googleapis/python-genai

Conversation

@anthonymq

@anthonymq anthonymq commented Jan 14, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Summary

This PR fixes memory spikes that can cause OOM (Out of Memory) errors in the _interactions layer when handling file uploads and inline data.

Problem

Several functions in the _interactions layer were loading entire files into memory before processing, causing OOM errors for large files.

Note: The main aio.files.upload() API (files.py → _api_client.py) is already memory-efficient - it uses 8MB chunked streaming via anyio.Path().open('rb'). The OOM issues occur in the experimental _interactions layer.

Root Causes Identified

  1. _interactions/_files.py: Used path.read_bytes() to load entire files before passing to httpx
  2. _interactions/_utils/_transform.py: Base64 encoding loaded entire files with read_bytes() before encoding
  3. _interactions/_utils/_utils.py: file_from_path() loaded entire files with read_bytes()

Solution

1. File Handle Streaming (_interactions/_files.py)

  • Return open file handles (open(path, 'rb')) instead of loading bytes with read_bytes()
  • httpx natively supports IO[bytes] file handles, so this is a drop-in fix
  • Applies to both sync and async code paths

2. Chunked Base64 Encoding (_interactions/_utils/_transform.py)

  • Implement chunked reading with 3MB chunks for base64 encoding
  • Chunk size is a multiple of 3 (required for correct base64 encoding without padding issues)
  • Reduces peak memory from O(file_size) to O(chunk_size)

3. File Handle in Utility (_interactions/_utils/_utils.py)

  • file_from_path() now returns a file handle instead of loaded bytes

Memory Flow (Before vs After)

Before:

File path → read_bytes() (🔴 entire file in memory) → process

After:

File path → open() → file handle → stream in chunks (✅ memory-efficient)

Testing

  • Verified Python syntax compiles correctly
  • Verified file handle approach works with httpx's expected types
  • Verified chunked base64 encoding produces identical output to original implementation

Backwards Compatibility

This is a fully backwards-compatible change:

  • Public API remains unchanged
  • Return types are compatible (httpx accepts both bytes and IO[bytes])
  • Base64 output is identical (chunked encoding with multiples of 3 produces same result)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@janasangeetha janasangeetha self-assigned this Jan 16, 2026
@janasangeetha janasangeetha added the size:XL Code changes > 100 lines label Jan 16, 2026
@janasangeetha

janasangeetha commented Jan 16, 2026

Copy link
Copy Markdown

Hey @anthonymq
Thanks for contributing!
The branch is out-of-date. Kindly update the same.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@janasangeetha janasangeetha added the status:awaiting user response label Jan 16, 2026
@anthonymq
anthonymq force-pushed the fix/memory-efficient-file-uploads branch from bf6914f to ccf8a52 Compare January 16, 2026 08:41
@anthonymq

anthonymq commented Jan 16, 2026

Copy link
Copy Markdown
Author

Branch updated,
Cheers

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@janasangeetha janasangeetha removed the status:awaiting user response label Jan 16, 2026
@anthonymq
anthonymq force-pushed the fix/memory-efficient-file-uploads branch from 46e2584 to af3d954 Compare January 18, 2026 07:34
anthonymq added 2 commits March 11, 2026 10:34
@anthonymq
fix: prevent OOM by using file handles and chunked base64 encoding
636d746
This change addresses memory spikes that can cause OOM errors when
uploading large files to the Gemini File API.

Changes:
1. _interactions/_files.py: Return open file handles instead of loading
   entire files into memory with read_bytes(). httpx supports IO[bytes]
   directly, so there's no need to pre-load file contents.

2. _interactions/_utils/_transform.py: Implement chunked base64 encoding
   using 3MB chunks (must be multiple of 3 for base64 correctness) to
   reduce peak memory usage when encoding files for inline data.

The existing chunked upload mechanism in _api_client.py (8MB chunks)
was already correct, but files were being loaded into memory before
reaching that code path. This fix ensures memory-efficient handling
from the start of the upload flow.
@anthonymq
fix: use file handle in file_from_path to prevent OOM
fcbb70d
Additional fix for the _interactions layer - file_from_path was loading
entire files with read_bytes() when it can return a file handle instead.
@anthonymq
anthonymq force-pushed the fix/memory-efficient-file-uploads branch from af3d954 to fcbb70d Compare March 11, 2026 09:34
@ascillitoe ascillitoe mentioned this pull request Nov 26, 2026
generate_content with inline bytes allocates 4.3× the file size to build a 1.33× request body #3424
Open

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@janasangeetha janasangeetha

Labels

size:XL Code changes > 100 lines

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@anthonymq @janasangeetha
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.