Google-genai files.upload not working

Hi, I installed the python genai version from pypi - pip install google-genai.
I’m running the following code :
from google import genai
from google.genai import types
client = genai.Client(
vertexai=True, project=, location=‘us-central1’,
)
file = client.files.upload(path=‘a11.txt’)
response = client.models.generate_content(
model=‘gemini-2.0-flash-001’,
contents=[‘Could you summarize this file?’, file]
)
print(response.text)

I keep getting the error :
file = client.files.upload(path=‘a11.txt’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Files.upload() got an unexpected keyword argument ‘path’

What am I doing wrong?
Thanks,
Krishna

Hi,

Welcome to the forum.

You’re using Gemini on Vertex AI. AFAIK, the File API is bound to AI Studio only.
For Vertex AI you’d use Cloud Storage directly and pass in the “gs://” URLs.

Cheers

Hi @Kumar_Ramanathan , Welcome to the forum.

Can you try changing the argument path to file and check if it works?

from google import genai
from google.genai import types

client = genai.Client(
vertexai=True, project=<project_id>, location=‘us-central1’
)

file = client.files.upload(path=‘gs://kumar-my-new-bucket/test/file.txt’)

response = client.models.generate_content(
model=‘gemini-2.0-flash-001’,
contents=[‘Could you summarize this file?’, ‘gs://kumar-my-new-bucket/test/file.txt’]
)
print(response.text)

Thanks for the reponse - i get the same error - unexpected argument “path”.

If I comment out the upload line, the LLM complains that it can’t access GCS paths., like so :
"Okay, I can’t directly access files in Google Cloud Storage (GCS) buckets, including the one you specified. Therefore, I can’t summarize the contents of gs://kumar-my-new-bucket/test/file.txt.

To get a summary, you’ll need to do one of the following:

  1. Download the file and summarize it yourself: You can use the Google Cloud SDK (gsutil) or other GCS tools to download the file to your local machine and then use a text editor or programming language to read and summarize it.

  2. Provide the content of the file to me: Copy and paste the text from file.txt into our chat, and I will summarize it for you.

  3. Use a Google Cloud Function or other cloud-based service: You could create a Cloud Function that is triggered when a file is uploaded to that GCS bucket. This function could then read the file, summarize it, and store the summary in a database or another GCS bucket.

Let me know if you want me to help you with any of these steps! For example, if you provide the content of the file, I can give you a summary right away.
"

ValueError: Vertex AI does not support creating files. You can upload files to GCS files instead.
I get this when I change “path” to “file”
Thank you.

Seems like Vertex AI does not support the Files API. You need to use GCS files instead. However, when you initialize a client with your API key, the Files API is supported.

How do I use gcs files? I tried sending in the gs:// in the files.upload call , as well as in the generate_content call. No luck,
Krishna

client = genai.Client(api_key=os.getenv(‘GEMINI_API_KEY’))

file = client.files.upload(file=“./a11.txt”)

response = client.models.generate_content(
model=‘gemini-2.0-flash-001’,
contents=[‘Could you summarize this file?’, file],
)
print(response.text)

This works - I need to use the “file” in the upload call.
The documentation at GitHub - googleapis/python-genai: Google Gen AI Python SDK provides an interface for developers to integrate Google's generative models into their Python applications. needs to be updated, this wasted a good 3 hours for me.
I would love vertex to support uploads, gcs, or not - I’d prefer a secure upload of files.
Thanks a lot.

1 Like

So, when you use Vertex AI, you can directly pass the file URL.

For example:

response = client.models.generate_content(
    model='gemini-2.0-flash',
    contents=[
    'What is this a picture of?',
    types.Part.from_uri(
        file_uri='gs://generativeai-downloads/images/scones.jpg',
        mime_type='image/jpeg',
    ),
]

)
print(response.text)

Hi Kumar,

Forget about the files.upload() completely. That’s for AI Studio only.
In Vertex AI you can reference the GCS URL directly.

Cheers

This worked !!! Thank you !

1 Like

@jkirstaetter and @GUNAND_MAYANGLAMBAM - thanks so much !

1 Like