generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Async chat using rest transport results in Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>

Open holymode opened this issue 1 year ago • 9 comments

Description of the bug:

When using the rest transport with async chat method it will result in Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>.

Using sync function however works fine.

Changing to default transport also works fine with async method.

Traceback:

Traceback (most recent call last):
  File "C:\Users\tobia\Desktop\py\g3rb-bot\test.py", line 258, in <module>
    test = asyncio.run(generate_response_with_text("Hello"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\test.py", line 256, in generate_response_with_text
    return await chat.send_message_async(message_text)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\generativeai\generative_models.py", line 410, in send_message_async
    response = await self.model.generate_content_async(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\generativeai\generative_models.py", line 275, in generate_content_async
    response = await self._async_client.generate_content(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\async_client.py", line 354, in generate_content
    response = await rpc(
               ^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\api_core\retry\retry_unary_async.py", line 231, in retry_wrapped_func
    return await retry_target(
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\api_core\retry\retry_unary_async.py", line 161, in retry_target
    _retry_error_helper(
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\api_core\retry\retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\api_core\retry\retry_unary_async.py", line 155, in retry_target
    return await target()
           ^^^^^^^^^^^^^^
  File "C:\Users\tobia\Desktop\py\g3rb-bot\venv\Lib\site-packages\google\api_core\grpc_helpers_async.py", line 187, in error_remapped_callable
    raise TypeError("Unexpected type of call %s" % type(call))
TypeError: Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>

Actual vs expected behavior:

No response

Any other information you'd like to share?

Using the latest version of the lib.

holymode avatar Feb 09 '24 09:02 holymode

+1

LukeSamkharadze avatar Feb 29 '24 00:02 LukeSamkharadze

@MarkDaoust

LukeSamkharadze avatar Mar 01 '24 01:03 LukeSamkharadze

@holymode @LukeSamkharadze this should be fixed by #229, try to updated to version 0.4.1

Andy963 avatar Mar 14 '24 11:03 Andy963

Still getting same error if using transport="rest" and generate_content_async TypeError: Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>

google-generativeai==0.4.1

P1nk-L0rD avatar Mar 31 '24 11:03 P1nk-L0rD

Still getting same error if using transport="rest" and generate_content_async TypeError: Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>

google-generativeai==0.4.1 @P1nk-L0rD Please provide a minimal reproducible example ,so that we can better understand and address your issue.

Andy963 avatar Apr 01 '24 08:04 Andy963

Please provide a minimal reproducible example ,so that we can better understand and address your issue. @Andy963

Code:

import asyncio
import google.generativeai as genai


async def ask_gemini(question: str):
    genai.configure(api_key=api_key, transport="rest")
    model = genai.GenerativeModel('gemini-pro')
    response = await model.generate_content_async(question)
    return response.text


print(asyncio.run(ask_gemini("What is your name?")))

Exception:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\Gemini_bot\check_gemini.py", line 14, in <module>
    print(asyncio.run(ask_gemini("What is your name?")))
  File "C:\Program Files\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\user\PycharmProjects\Gemini_bot\check_gemini.py", line 10, in ask_gemini
    response = await model.generate_content_async(question)
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\generativeai\generative_models.py", line 269, in generate_content_async
    response = await self._async_client.generate_content(
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\ai\generativelanguage_v1beta\services\generative_service\async_client.py", line 354, in generate_content
    response = await rpc(
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\api_core\retry\retry_unary_async.py", line 230, in retry_wrapped_func
    return await retry_target(
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\api_core\retry\retry_unary_async.py", line 160, in retry_target
    _retry_error_helper(
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\api_core\retry\retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\api_core\retry\retry_unary_async.py", line 155, in retry_target
    return await target()
  File "C:\Users\user\PycharmProjects\Gemini_bot\venv\lib\site-packages\google\api_core\grpc_helpers_async.py", line 187, in error_remapped_callable
    raise TypeError("Unexpected type of call %s" % type(call))
TypeError: Unexpected type of call <class 'google.ai.generativelanguage_v1beta.types.generative_service.GenerateContentResponse'>

P1nk-L0rD avatar Apr 01 '24 15:04 P1nk-L0rD

@P1nk-L0rD yes,there are some problem with the rest transport, you can try "grpc_asyncio" in this condition

Andy963 avatar Apr 02 '24 04:04 Andy963

Any updates on this?

LukeSamkharadze avatar May 08 '24 22:05 LukeSamkharadze

This error is still unsolved for generativeai == 0.7.0 (latest to this date), which prevents for recording cassettes for async test, any update or workaround?

adrienbanse avatar Jun 21 '24 16:06 adrienbanse

This is https://github.com/googleapis/gapic-generator-python/issues/1962

markmcd avatar Sep 11 '24 00:09 markmcd

We'll track this in #499

markmcd avatar Sep 11 '24 00:09 markmcd