Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't send images in a chat session, but audio and video files work. #48

Open
War004 opened this issue Dec 24, 2024 · 2 comments
Open
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@War004
Copy link

War004 commented Dec 24, 2024

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

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))

@War004 War004 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 24, 2024
@Giom-V
Copy link

Giom-V commented Jan 8, 2025

Hello @War004, I cannot see any major difference with what we recommend to do in the Cookbook getting starting notebook, so could you share the image you are using, maybe there's an issue with it?

@War004
Copy link
Author

War004 commented Jan 9, 2025

  • The issue was arising with any image I used; I even checked the sha 256 hash generated through in the files Api and in my local machine.
  • It's unstable right now, will update after some hours. But right now, the issue seems to be fixed when using the files Api, **as of now I still get the same error when I directly send the images.**
  • Removing the code execution in the tools eliminated all the issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants