Skip to content

Conversation

@PR-HARIHARAN
Copy link

[fix] Resolve Gemini video file upload error

Description

This PR fixes the video file upload error when using Gemini models with file inputs, addressing issue #4857.

Problem

Users were encountering a 400 INVALID_ARGUMENT error when trying to upload video files to Gemini models. The error occurred because:

  1. The cookbook example was incorrectly using Video(content=video_file) instead of Video(url=video_file.uri)
  2. The Google genai library has a bug where it sometimes doesn't include the 'file' key in upload responses, causing KeyError exceptions

Solution

1. Fixed Video Constructor Usage:

  • Changed Video(content=video_file) to Video(url=video_file.uri) in the cookbook example
  • The Video class expects a URL reference to uploaded files, not the file object itself

2. Added Robust Error Handling:

  • Implemented comprehensive error handling for the Google library's KeyError bug
  • Added graceful fallback using files.list() when file information is missing from upload responses
  • Enhanced logging to help users understand what's happening during file uploads

Changes Made

Files Modified:

  • cookbook/models/google/gemini/video_input_file_upload.py: Fixed Video constructor usage
  • libs/agno/agno/models/google/gemini.py: Added error handling for Google library bugs

Key Changes:

  1. Cookbook Fix: Updated video usage from Video(content=video_file) to Video(url=video_file.uri)
  2. Error Handling: Added try-catch blocks around video and audio upload operations
  3. Recovery Mechanism: Implemented fallback to retrieve file info when Google's library fails
  4. Better Logging: Added informative error messages for debugging

Testing

  • Verified original issue is resolved (400 INVALID_ARGUMENT error fixed)
  • Tested error handling with Google library KeyError scenarios
  • Validated end-to-end video upload and analysis workflow
  • Confirmed code formatting with ruff
  • Validated with mypy type checking
  • Ran test suite to ensure no regressions

Impact

  • Backward Compatible: No breaking changes to existing APIs
  • Improved Reliability: Framework now handles upstream library bugs gracefully
  • Better User Experience: Clear error messages and automatic recovery when possible
  • Documentation: Fixed cookbook example that users follow

Related Issues

Fixes #4857

Checklist

  • Code follows the project's coding standards
  • Code has been formatted using ./scripts/format.bat
  • Code has been validated using ./scripts/validate.bat
  • Tests pass locally
  • Changes are backward compatible
  • PR title follows format: [fix] Resolve Gemini video file upload error
  • PR description references the issue using fixes #4857
  • Added comprehensive error handling
  • Updated cookbook example with correct usage

- Fix Video constructor usage in cookbook example
- Use Video(url=video_file.uri) instead of Video(content=video_file)
- Add error handling for Google library KeyError bug in file uploads
- Improve robustness with graceful fallback when file info is missing

fixes agno-agi#4857
@PR-HARIHARAN PR-HARIHARAN requested a review from a team as a code owner October 24, 2025 07:06
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] Error when using files upload in video input using gemini models

1 participant