RuntimeError
RuntimeError: no running event loop
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = genai.GenerativeModel('gemini-2.0-flash').generate_content_async(prompt)
File "/usr/local/lib/python3.11/site-packages/google/generativeai/generative_model.py", line 187, in generate_content_async
return await self._async_client.generate_content(request)
File "/usr/local/lib/python3.11/site-packages/google/generativeai/client.py", line 456, in generate_content
async with self._transport.stream() as stream:
RuntimeError: no running event loop Why it happens
The google-generativeai SDK's generate_content_async() is a coroutine that requires an active asyncio event loop to execute. When you call it from a synchronous function or without wrapping it in asyncio.run() or being inside an async context, Python has no event loop to schedule the coroutine on. This is a fundamental asyncio requirement: async code cannot run without an event loop managing it.
Detection
Look for generate_content_async() calls in non-async functions, or calls outside of asyncio.run() blocks. If you see 'no running event loop' in the traceback when calling async Gemini methods, you're mixing async and sync code improperly. Check that all calls to async methods are inside async functions and executed within an event loop.
Causes & fixes
Calling generate_content_async() directly in a synchronous (non-async) function without asyncio.run()
Wrap the async call in asyncio.run(): `response = asyncio.run(model.generate_content_async(prompt))` at the top level, or refactor your function to be async with `async def` and use `await`.
Attempting to await generate_content_async() outside of an async function or async context manager
Define your calling function as `async def` and use `await model.generate_content_async(prompt)` inside it. The entire call chain from event loop creation down must be async.
Calling async Gemini methods from a synchronous web framework (Flask, FastAPI without async), or from a script's main block
Use asyncio.run() at the entry point: `if __name__ == '__main__': asyncio.run(main())` where main() is async. For Flask, use async routes: `@app.route('/'); async def handle_request():`. For FastAPI, routes are async-first.
Using generate_content_async() in a thread pool without a proper event loop in that thread
Create a new event loop in the thread: `loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop); response = loop.run_until_complete(model.generate_content_async(prompt))`
Code: broken vs fixed
import os
import google.generativeai as genai
# BROKEN: Calling async method from sync code without event loop
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
model = genai.GenerativeModel('gemini-2.0-flash')
# This line raises: RuntimeError: no running event loop
response = model.generate_content_async('What is the capital of France?') import os
import asyncio
import google.generativeai as genai
# FIXED: Wrap async call in asyncio.run()
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
model = genai.GenerativeModel('gemini-2.0-flash')
# Now works: asyncio.run() creates and manages the event loop
response = asyncio.run(model.generate_content_async('What is the capital of France?'))
print(f"Response: {response.text}") Workaround
If you cannot refactor to async, use the synchronous generate_content() method instead: `response = model.generate_content(prompt)`. This blocks but avoids event loop issues entirely. For concurrent requests, use a thread pool with asyncio.run() in each thread.
Prevention
Design your application around async from the start if you need concurrency. Use asyncio.run() at your main entry point and keep all Gemini calls async. For web frameworks, use async-native frameworks (FastAPI, aiohttp) or wrap asyncio.run() in your request handlers. Document which Gemini methods are async and which are sync to prevent accidental mixing.