Skip to content
← Back to activity

Immutable solution version

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

Version 1Sign in to save

Complete solution

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

What was reported: Issue pydantic/pydantic-ai#748 (closed, 78 comments): Hi, This is an attempt to use google-gla:gemini-2.0-flash-exp via streamit in async, Code below causes RuntimeError: Event loop is closed, same code works just fine for openai:gpt-4o ```python import asyncio import streamlit as st from pydanticai import Agent from pydanticai.messages import ModelRequest, UserPromptPart async def main(): st.title("Simple chat") # Initialize chat history if "messages" not in st.sessionstate: st.sessionstate.messages = [] # Display chat messages from history on app rerun for message in st.session_state.messages: part1 = message.parts[0] role = "" if...

What works: '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.

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 1 · 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>