Skip to main content
Uploads a file to the Gemini File API, making it available for use in generation requests with large files, videos, or audio.

Method Signature

Parameters

string | PathLike | IOBase
required
The file to upload.Can be:
  • A file path as a string: "/path/to/file.pdf"
  • A PathLike object: Path("document.pdf")
  • An IOBase object (file handle): Must be opened in binary mode ('rb') and be seekable
When using an IOBase object (file handle), ensure:
  • The file is opened in binary mode (not text mode)
  • The file is opened in blocking mode (the default)
  • The stream is seekable (supports seek() operations)
UploadFileConfig
Optional configuration for the upload.Available options:
  • display_name: Human-readable name for the file
  • mime_type: MIME type (auto-detected if not provided)
  • name: Custom resource name
  • http_options: Custom HTTP request options

Returns

File
A File object containing:
  • name: The resource name (e.g., "files/abc123")
  • uri: The URI to reference in API calls
  • display_name: Human-readable name
  • mime_type: The file’s MIME type
  • size_bytes: File size in bytes
  • create_time: When the file was uploaded
  • update_time: Last update time
  • expiration_time: When the file will be deleted
  • state: Upload state (PROCESSING, ACTIVE, or FAILED)

Examples

Upload PDF Document

Upload with Display Name

Upload Video File

Upload from File Handle

Upload and Use in Prompt

Upload Multiple Files

Async Upload

Upload with Custom MIME Type

Upload Audio File

File Lifecycle

Uploaded files are automatically deleted after 48 hours. Plan your workflows accordingly or re-upload files as needed.

Error Handling

API Availability

This method is only available in the Gemini API (not Vertex AI).For Vertex AI, you should:
  • Store files in Google Cloud Storage (GCS)
  • Use files.register_files() to register GCS URIs
  • Or use direct GCS URIs in prompts