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
- Initialize a chat session (Async or normal)
- 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