Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Googleapis Python genai Files Upload

From Leeroopedia
Knowledge Sources
Domains File_Management, Multimodal
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for uploading local files to the Google Gen AI file service provided by the google-genai files module.

Description

Files.upload transfers a local file (from a path or IO stream) to the Gen AI service. The method auto-detects MIME type from the file extension if not specified. It returns a File object containing the uri (for use in Part.from_uri), name (resource identifier), mime_type, size_bytes, and state (ACTIVE when processing is complete). For Vertex AI, files are stored in GCS; for the Gemini Developer API, they are stored in Google's file service.

Usage

Use Files.upload to make local media files available for multimodal generation, context caching, or file search. The returned File.uri can be passed to Part.from_uri to create content parts. Always check File.state for video files, as they may require processing time before becoming ACTIVE.

Code Reference

Source Location

Signature

class Files:
    def upload(
        self,
        *,
        file: Union[str, os.PathLike[str], io.IOBase],
        config: Optional[types.UploadFileConfigOrDict] = None,
    ) -> types.File:
        """Uploads a file to the Gen AI service.

        Args:
            file: Local file path or IO stream to upload.
            config: Optional upload configuration (display_name, mime_type).
        """

Import

from google import genai

I/O Contract

Inputs

Name Type Required Description
file Union[str, os.PathLike[str], io.IOBase] Yes Local file path or IO stream
config Optional[UploadFileConfigOrDict] No Upload config with display_name, mime_type overrides

Outputs

Name Type Description
File types.File Uploaded file with .uri, .name, .mime_type, .size_bytes, .state

Usage Examples

Upload and Use in Generation

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_API_KEY")

# Upload a PDF file
uploaded_file = client.files.upload(file="document.pdf")

# Use in generation
response = client.models.generate_content(
    model="gemini-2.0-flash",
    contents=[
        types.Part.from_uri(
            file_uri=uploaded_file.uri,
            mime_type=uploaded_file.mime_type
        ),
        "Summarize this document."
    ]
)
print(response.text)

Upload with Configuration

uploaded_file = client.files.upload(
    file="image.png",
    config=types.UploadFileConfig(
        display_name="My Image",
        mime_type="image/png"
    )
)
print(f"URI: {uploaded_file.uri}")
print(f"State: {uploaded_file.state}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment