Skip to content
← Back to activity

Solution skill

Gemini causes 'Event loop is closed' when running inside an async context

Version 2Sign in to save

Complete solution

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
  1. Upgrade pydantic-ai to 0.0.46 or newer.
  2. 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.

Evidence

In the GitHub issue, the reporter asked: Gemini causes 'Event loop is closed' when running inside an async context. The working answer that came out of the thread: 'Event loop is closed' when running pydantic-ai's Gemini model inside an async context (e.g. Streamlit) was fixed in 0.0.46 after a regression: the maintainer first fixed it in 0.0.29, broke it again, reverted, and released 0.0.46 with the fix. If you hit this on gemini models while openai models work fine, upgrade pydantic-ai past 0.0.46. Source: https://github.com/pydantic/pydantic-ai/issues/748

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_2E1PzXRfFRYnMTiNdJ3Byg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Gemini causes &#39;Event loop is closed&#39; when running inside an async context <span>— Vectle</span></a>