[Forwarded from GitHub] Sending Image file into a chat session return 400 INVALID_ARGUMENT. But if a video or an audio file is sent it works

When I try to send an image with the text prompt into the chat session it gives me this error consistently.

File "...../test.py", line 110, in main
    response = await chat.send_message(["What do you think of this?",media_file])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/chats.py", line 188, in send_message
    response = await self._modules.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/models.py", line 5293, in generate_content
    response = await self._generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/models.py", line 4565, in _generate_content
    response_dict = await self.api_client.async_request(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/_api_client.py", line 358, in async_request
    result = await self._async_request(http_request=http_request, stream=False)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/_api_client.py", line 304, in _async_request
    return await asyncio.to_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/threads.py", line 25, in to_thread
    return await loop.run_in_executor(None, func_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/_api_client.py", line 263, in _request
    return self._request_unauthorized(http_request, stream)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...../apikeys/lib/python3.11/site-packages/google/genai/_api_client.py", line 285, in _request_unauthorized
    errors.APIError.raise_for_response(response)
  File "...../apikeys/lib/python3.11/site-packages/google/genai/errors.py", line 100, in raise_for_response
    raise ClientError(status_code, response)
google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}

Environment details

  • Programming language: Python
  • OS: Linux (Debian 6.1.119-1, kernel 6.1.0-28-cloud-amd64)
  • Language runtime version: Python 3.11.2
  • Package version: google-genai 0.3.0

Steps to reproduce

  1. Initialize a chat session (Async or normal)
  2. Send an image file in the chat session. (Files or Local upload)

Additional details

  • The error comes in both async or normal chat session.
  • Whether the method for attaching the image is through local upload or using the files. It gives the same error.
  • If I send a video file or an audio file using local upload or files, it doesn’t give me an error and request is completed successfully.

Code used:

#Configuration Block
from google import genai
from google.genai import types
from google.genai import files

GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
client = genai.Client(api_key=GOOGLE_API_KEY)
config = types.GenerateContentConfig(
    system_instruction="Act like a useful assistant",
    temperature=1,
    top_p=0.95,
    top_k=40,
    candidate_count=1,
    seed=-1,
    max_output_tokens=8192,
    safety_settings=[
        types.SafetySetting(category='HARM_CATEGORY_HATE_SPEECH', threshold='BLOCK_NONE'),
        types.SafetySetting(category='HARM_CATEGORY_HARASSMENT', threshold='BLOCK_NONE'),
        types.SafetySetting(category='HARM_CATEGORY_SEXUALLY_EXPLICIT', threshold='BLOCK_NONE'),
        types.SafetySetting(category='HARM_CATEGORY_DANGEROUS_CONTENT', threshold='BLOCK_NONE')
    ],
    tools=[
        types.Tool(code_execution={}),
    ]
)

Using Files to send the image with the text prompt in the chat session

async def main():
    chat = client.aio.chats.create(
    model="models/gemini-2.0-flash-exp",
    config=config,
    history=[]
    )
    path = "...../uuu.jpg"
    file = client.files.upload(path=path)
    file_uri = file.uri  # Get the URI from the file object
    mime_type = file.mime_type  # Get the mime_type from the file object
    media_file = types.Part.from_uri(file_uri=file_uri, mime_type=mime_type)
    print("--------------")

    await asyncio.sleep(10) #Waiting manually to make sure file is active before processing
    response = await chat.send_message(["What do you think of this?",media_file])
    
    print(media_file.file_data.file_uri) #prints the file_uri
    print(await extract_response_text(response)) #custom function for printing out the final response
    #output is a 400 error

Using local upload to send the image with the text prompt in the chat session

async def main():
    chat = client.aio.chats.create(
    model="models/gemini-2.0-flash-exp",
    config=config,
    history=[]
    )
    path = "...../uuu.jpg"

    with open(path, 'rb') as f:
        image_bytes = f.read()

    format="image/jpeg"
    media_file = types.Part.from_bytes(
                    data=image_bytes,
                    mime_type=format
                )
    print("--------------")
    #await asyncio.sleep(10)
    
    response = await chat.send_message(["What do you think of this?",media_file])


    #print(media_file.file_data.file_uri)
    print(await extract_response_text(response))
    #output is a 400 error

When sending the image through the “genrate_content” or a single session it works

async def main():
    path = "...../uuu.jpg"
    file = client.files.upload(path=path)
    file_uri = file.uri  # Get the URI from the file object
    mime_type = file.mime_type  # Get the mime_type from the file object
    media_file = types.Part.from_uri(file_uri=file_uri, mime_type=mime_type)

    response = client.models.generate_content(
        model='gemini-2.0-flash-exp',
        contents=['What you say about the Image?',media_file],
        config=types.GenerateContentConfig(
            system_instruction='Descrive the image in a short sentence',
            temperature= 0.3,
        ),
    )
    print(file_uri)
    print(await extract_response_text(response))
    #This works without any error

Or, using a video file during a chat session it works.

async def main():
    path = "...../ert.mp4"
    file = client.files.upload(path=path)
    file_uri = file.uri  # Get the URI from the file object
    mime_type = "video/mp4"
    media_file = types.Part.from_uri(file_uri=file_uri, mime_type=mime_type)
    chat = client.aio.chats.create(
    model="models/gemini-2.0-flash-exp",
    config=config,
    history=[]
    )
    print("--------------")

    await asyncio.sleep(10) #Waiting manually to make sure file is active before processing
    response = await chat.send_message(["What do you think of this?",media_file])
    
    print(await extract_response_text(response)) #custom function for printing out the final response
    #No errors

Hey @Ritesh, there seems to be an issue with sending the code_execution tool along with the image. Could you try removing the code_execution tool and see if it works?

Thanks.

Removing code_execution does works, I get no error. And currently when I use files Api to send the image with code execution on i get no error. But still when I directly upload it to the chat session, I still get the same error when code execution is on.

Is turning off code_execution a temporary solution?

Could you try using the code_execution tool now?
The issue has been fixed.

1 Like

Yes, it is now even working with code_execution. There is no issue even on direct upload or files Api.

1 Like

Hey it seems like the bug is back, the trigger is same as before, sending image file into the chat session.
Directly uploading to chat or using files Api result in 400 error.

ClientError                               Traceback (most recent call last)
<ipython-input-61-ad5a03c9c4d7> in <cell line: 0>()
      2 print(f"{current_time}\nSecond run for direct")
      3 if __name__ == "__main__":
----> 4     asyncio.run(main())

17 frames
/usr/local/lib/python3.11/dist-packages/google/genai/errors.py in raise_for_response(cls, response)
     98     status_code = response.status_code
     99     if 400 <= status_code < 500:
--> 100       raise ClientError(status_code, response)
    101     elif 500 <= status_code < 600:
    102       raise ServerError(status_code, response)

ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}
  • Additionally, while using local upload, it is not able to process the video file and the images. Audio files are working.
  • In files api just image files are not processed just as before.

Here is a notebook that I am using for testing

As I said in github, I got this code to work so there might be something I can’t see in your colab:

!wget "https://storage.googleapis.com/generativeai-downloads/data/jetpack.png" -O jetpack.png -q
image = Image.open('jetpack.png')
image_upload = client.files.upload(path='jetpack.png')

response = chat.send_message([
    "What is this?",
    types.Part.from_uri(
      file_uri=image_upload.uri,
      mime_type=image_upload.mime_type),
    ])
1 Like