Note

The File API lets you store up to 50MB of PDF files. Files are stored for 48 hours. You can access them in that period with your API key, but you can’t download them from the API. The File API is available at no cost in all regions where the Gemini API is available.

Call media.upload to upload a file using the File API. The following code uploads a document file and then uses the file in a call to models.generateContent.

You can verify the API successfully stored the uploaded file and get its metadata by calling files.get. Only the name (and by extension, the uri) are unique.

from google import genai
import pathlib
 
client = genai.Client()
 
fpath = pathlib.Path('example.txt')
fpath.write_text('hello')
 
file = client.files.upload(file='example.txt')
 
file_info = client.files.get(name=file.name)
print(file_info.model_dump_json(indent=4))