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
commented
Jan 14, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
_interactions/_files.py: Used path.read_bytes() to load entire files before passing to httpx
_interactions/_utils/_transform.py: Base64 encoding loaded entire files with read_bytes() before encoding
_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.
|
Hey @anthonymq
Thanks for contributing!
The branch is out-of-date. Kindly update the same.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Additional fix for the _interactions layer - file_from_path was loading
entire files with read_bytes() when it can return a file handle instead.
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
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.
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.