File Upload Not working

Hi,

I am trying to upload a file to the API, and I keep getting this error.

Code: from google import genai

client = genai.Client()
myfile = client.files.upload(file= “poem.txt”)
print(f"{myfile=}")

result = client.models.generate_content(
model=“gemini-2.0-flash”,
contents=[myfile, “\n\n”, “Can you add a few more lines to this poem?”],
)
print(f"{result.text=}")

Error: /Users/sdhanda/TESTZ/mynewenv/bin/python /Users/sdhanda/TESTZ/test.py
Traceback (most recent call last):
File “/Users/sdhanda/TESTZ/test.py”, line 4, in
myfile = client.files.upload(file= “poem.txt”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/sdhanda/TESTZ/mynewenv/lib/python3.12/site-packages/google/genai/files.py”, line 613, in upload
raise ValueError(
ValueError: This method is only supported in the Gemini Developer client.

Any ideas?

Hello @Sdhanda, welcome to the community.

can you confirm if you are giving in the API key while creating the client ?

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

I got the result just fine when i tried.

yes I am. I am still getting: Traceback (most recent call last):
File “/Users/sdhanda/TESTZ/test.py”, line 5, in
myfile = client.files.upload(file= “poem.txt”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/Users/sdhanda/TESTZ/mynewenv/lib/python3.12/site-packages/google/genai/files.py”, line 613, in upload
raise ValueError(
ValueError: This method is only supported in the Gemini Developer client.
(mynewenv) sdhanda@HS_Daily_sdhanda TESTZ %

can you please make sure the filename is right and the quotes around
i see you are using " and ” differently at different parts of the code,
can you see if that is an issue that can fix this ?

If you are using environment variables, make sure to set export GOOGLE_GENAI_USE_VERTEXAI=false. Alternatively, when initializing the client, you can disable vertex AI by specifying vertexai=False like this: client = genai.Client(api_key=GOOGLE_API_KEY, vertexai=False), since the file API is not supported with vertex AI.