fix(video): resolve File API video understanding issue (#864) #916
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.
Description
This PR fixes issue #864 where video understanding fails for videos uploaded through the File API but works fine when the same video is provided as a YouTube URL.
Root Cause
After investigation, I found two issues contributing to the problem:
File Processing Status: Files uploaded through the API need time to be processed before they can be analyzed. If a file is still in the PROCESSING state when it's used in a content generation request, the API rejects it.
Error Handling: The error from the API when using a file in the PROCESSING state was not clearly indicating the issue, making it hard to diagnose.
Solution
The solution implemented in this PR:
Added a
_ensure_file_active
function that:Enhanced the
_process_contents_for_generation
function to:_ensure_file_active
function to each fileAdded a
FileProcessingError
class to provide better error messages when file processing failsAdded comprehensive unit tests to verify the solution works correctly
Testing
The solution was tested with both:
Note for Reviewers
This approach is more robust than simply adding a fixed delay because it: