High severity intermediate · Fix: 2-5 min

RuntimeError

RuntimeError: no running event loop

What this error means
Gemini's generate_content_async() requires an active asyncio event loop, but was called from synchronous code or without proper async context.

Stack trace

traceback
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
QUICK FIX
Wrap your async call in asyncio.run(): `response = asyncio.run(model.generate_content_async(prompt))` or convert your function to async with `async def` and use `await`.

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

1

Calling generate_content_async() directly in a synchronous (non-async) function without asyncio.run()

✓ Fix

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`.

2

Attempting to await generate_content_async() outside of an async function or async context manager

✓ Fix

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.

3

Calling async Gemini methods from a synchronous web framework (Flask, FastAPI without async), or from a script's main block

✓ Fix

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.

4

Using generate_content_async() in a thread pool without a proper event loop in that thread

✓ Fix

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

Broken - triggers the error
python
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?')
Fixed - works correctly
python
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}")
asyncio.run() creates a fresh event loop, runs the coroutine inside it, and cleans up—allowing generate_content_async() to execute properly in synchronous code.

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.

Python 3.7+ · google-generativeai >=0.3.0 · tested on 0.7.0
Verified 2026-04 · gemini-2.0-flash, gemini-1.5-pro
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.