Skip to content

Carry binary file content across the Gemini file upload activities - #180

Merged
brianstrauch merged 1 commit into
temporalio:mainfrom
avanish-garg:fix/google-genai-binary-file-upload
Oct 26, 2026
Merged

brianstrauch merged 1 commit into
temporalio:mainfrom
avanish-garg:fix/google-genai-binary-file-upload

Conversation

@avanish-garg

@avanish-garg avanish-garg commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

  • _GeminiUploadFileRequest and _GeminiUploadToFileSearchStoreRequest get a file_base64: str | None field. TemporalAsyncFiles.upload and TemporalAsyncFileSearchStores.upload_to_file_search_store fill it with the base64 text of the stream's content instead of setting file_bytes.
  • A new _upload_source helper in _gemini_activity.py picks what gemini_files_upload and gemini_file_search_stores_upload hand to the SDK: file_base64 decoded to a BytesIO, else file_bytes as a BytesIO, else file_path.
  • file_bytes stays on both models and is still read, so an upload activity that an earlier version scheduled and an upgraded worker attempts keeps working. It is no longer set.
  • No change to activity names, the public API, path uploads, downloads or register_files.

Why?

file_bytes: bytes goes through the plugin's PydanticPayloadConverter, which writes bytes as UTF-8 text, so a PDF, image or audio file raised invalid utf-8 sequence while the workflow encoded the activity input. That is a workflow task failure: no activity was scheduled and the workflow task was retried until the workflow timed out. Only content that was valid UTF-8 uploaded. Base64 text round-trips any bytes, and unlike a marker-tagged form it is a typed field, so no ordinary value can be mistaken for it.

Replay: only the activity input changes. The upload and download results are encoded and decoded as before, and Python replay does not compare activity inputs, so histories recorded before this change replay as before. I recorded one on 7eea646 with ASCII content (the only kind that version could upload) and replay it as a test. Keeping file_bytes covers an activity that was scheduled under the old shape and runs after the upgrade.

Checklist

  1. Closes [Bug] google_genai: files.upload and upload_to_file_search_store cannot upload binary content (PDF, image, audio) from an in-memory stream #179

  2. How was this tested:
    New tests/test_files_bytes.py runs real genai.Clients against a local HTTP server that speaks the resumable upload and download protocol and keeps the bodies it receives. A workflow uploads a 1x1 PNG, a non-UTF-8 byte string, all 256 byte values (repeated) and ASCII through files.upload and upload_to_file_search_store, downloads a binary file, and the test asserts the server received exactly the uploaded bytes and the download came back as the same bytes. The same payloads go through the real activities directly (ActivityEnvironment), plus an empty stream, and an input in the old shape (file_bytes, no file_base64) is run both directly and through a worker. One test reads the recorded activity input from the workflow history and checks it is base64 text; another replays tests/histories/legacy_ascii_files.json, recorded on 7eea646. In tests/test_gemini.py the existing upload tests check file_base64, and there is a new in-memory File Search store test. On main (install rebuilt with make sync and checked against the source) 16 tests fail, 14 of them new: the three workflow-level tests with non-UTF-8 content fail with the workflow task failure above, the rest fail because file_base64 does not exist there. With the change make lint is clean and make test passes (129, 19 of them new). Not run against the real Gemini API. Uploads over 1.5 MB or so still hit the payload size limit.

  3. Any docs updates needed?
    No. The upload docstrings already say an io.IOBase is sent across the activity boundary; the README table needs no change.

TemporalAsyncFiles.upload and TemporalAsyncFileSearchStores.upload_to_file_search_store put the bytes of an in-memory stream into the activity input model as a bytes field. The Pydantic payload converter writes bytes as UTF-8 text, so a PDF, image or audio file raised "invalid utf-8 sequence" while the workflow encoded the activity input. That is a workflow task failure: the workflow retried it until it timed out and no activity was ever scheduled. Only bytes that happened to be valid UTF-8 worked.

The content now travels as base64 text in a new file_base64 field of both request models, and the activities decode it before handing a BytesIO to the SDK. file_bytes stays on the models and is still read, so an upload activity that an earlier version scheduled and an upgraded worker attempts keeps working; it is no longer set. Downloads already returned a raw binary payload, and generate_content carries image bytes as the base64 text the SDK writes into the request, so neither needed a change.

Tests upload a 1x1 PNG, a non-UTF-8 byte string, every byte value and ASCII through a workflow and through the real activities against a local server that speaks the resumable upload protocol, and assert that the server received the same bytes. A history recorded on 7eea646 with the old input shape replays, and an activity input in the old shape still runs.
@avanish-garg
avanish-garg requested a review from a team as a code owner October 26, 2026 15:39

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@brianstrauch
brianstrauch enabled auto-merge (squash) October 26, 2026 18:10
@brianstrauch
brianstrauch merged commit 4623b6c into temporalio:main Oct 26, 2026
25 checks passed
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.

[Bug] google_genai: files.upload and upload_to_file_search_store cannot upload binary content (PDF, image, audio) from an in-memory stream

2 participants