fix(files): wait for File API uploads to reach ACTIVE state before generation#2191
Open
Adewale-1 wants to merge 2 commits intogoogleapis:mainfrom
Open
fix(files): wait for File API uploads to reach ACTIVE state before generation#2191Adewale-1 wants to merge 2 commits intogoogleapis:mainfrom
Adewale-1 wants to merge 2 commits intogoogleapis:mainfrom
Conversation
…neration When a file is uploaded via the File API and immediately used in a generate_content request, the file may still be in PROCESSING state, causing the API to reject the request. This was the root cause of googleapis#864 where video understanding failed for File API uploads but worked for YouTube URLs. Add _ensure_file_active() which polls the file state until ACTIVE, and _process_contents_for_generation() which applies this check to all File objects in the content before each generate_content call. Also add FileProcessingError for clear error messaging when a file fails to become ACTIVE. Fixes googleapis#864
Tests were mocking api_client.call_api but implementation uses api_client.request with response.body as a JSON string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When a file is uploaded via the File API and immediately used in a
generate_contentrequest, the file may still be inPROCESSINGstate, causing the API to reject the request. This was the root cause of #864 where video understanding failed for File API uploads but worked for YouTube URLs.Solution
_ensure_file_active()which polls the file state untilACTIVE(with configurable retries and delay)_process_contents_for_generation()which applies this check to allFileobjects in the content before eachgenerate_contentcallFileProcessingErrortoerrors.pyfor clear error messaging when a file fails to becomeACTIVEBoth sync and async
_generate_content/_generate_content_streammethods are covered.Testing
Added
test_file_state_handling.pywith unit tests covering:PROCESSINGstate that transitions toACTIVEFAILEDACTIVEfiles (no polling)Fixes #864