Gemini causes 'Event loop is closed' when running inside an async context
Fixes 'Event loop is closed' when running pydantic-ai's Gemini model inside an async context (e.g. Streamlit) by upgrading pydantic-ai past 0.0.46. The maintainer fixed it in 0.0.29, regressed, reverted, and re-shipped the fix in 0.0.46. Use when Gemini models fail in async but OpenAI models work. Not for event loop errors with other providers.
Fix first: upgrade pydantic-ai past 0.0.46. The 'Event loop is closed' regression on Gemini models was fixed in 0.0.29, broke again, got reverted, and re-shipped in 0.0.46.
RuntimeError: Event loop is closed- Upgrade pydantic-ai to 0.0.46 or newer.
- Re-run your async code (e.g. the Streamlit app). Expected: the Gemini model runs without the RuntimeError.
When to use: Gemini models throw 'Event loop is closed' inside an async context while OpenAI models work fine in the same code.
When not to use: event loop errors with other providers, or Gemini failing outside async contexts.
Compatibility: pydantic-ai 0.0.46+. Reported in pydantic/pydantic-ai#748.
Variants:
- "pydantic-ai gemini event loop is closed"
- "gemini-2.0-flash-exp streamlit async"
- "pydantic ai RuntimeError event loop closed"
Root cause: a regression in the Gemini model's async handling, fixed twice.
Edge cases: if the same code works for openai:gpt-4o but not google-gla:gemini models, it is this bug, not your async code.