Open ochafik opened 1 month ago
Interesting! Thanks for reporting this.
Colab sets genai.configure(transport="rest")
, the problem seems to be async+rest transport.
In standard python this error is reproducible if I set transport="rest"
.
So that seems like an issue with the underlying generated client library.
Description of the bug:
Calling
GenerativeModel.generate_content_async
in Colab results in the following error:show colab cell code
```py !pip install -U -q google-generativeai # installs google-generativeai==0.7.2 # The following two lines don't change anything: # import nest_asyncio # nest_asyncio.apply() import asyncio import google.generativeai as genai from google.colab import userdata GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY') genai.configure(api_key=GOOGLE_API_KEY) model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest") async def main(): print(await model.generate_content_async("Tell me a joke")) await main() ```The ~same code works well outside Colab (tested Python 3.10.12 = same as Colab, 3.11 and 3.12)
show standalone code that works
```py ''' This nearly identical code works w/ Python 3.10.12 (same as Colab), 3.11, 3.12 conda create -n gemini-bug-3.10.12 -y conda activate gemini-bug-3.10.12 pip install -U -q google-generativeai GOOGLE_API_KEY=... python bug.py ''' import asyncio import google.generativeai as genai import os GOOGLE_API_KEY=os.environ['GOOGLE_API_KEY'] genai.configure(api_key=GOOGLE_API_KEY) model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest") async def main(): print(await model.generate_content_async("Tell me a joke")) if __name__ == "__main__": asyncio.run(main()) ```Full repro: https://gist.github.com/ochafik/f7472295d7b3f42824c8458f32bfa58b
Actual vs expected behavior:
Unexpected exception in Colab (works outside Colab)
Any other information you'd like to share?
No response