Skip to content

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

Open
@asaf

Description

@asaf

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

import asyncio

import streamlit as st
from pydantic_ai import Agent
from pydantic_ai.messages import ModelRequest, UserPromptPart


async def main():
    st.title("Simple chat")

    # Initialize chat history
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # Display chat messages from history on app rerun
    for message in st.session_state.messages:
        part1 = message.parts[0]
        role = ""
        if isinstance(part1, UserPromptPart):
            role = "user"
        with st.chat_message("human" if role == "user" else "ai"):
            st.markdown(message.parts[0].content)

    # Accept user input
    if prompt := st.chat_input("Say something..."):
        # Display user message in chat message container
        st.chat_message("user").markdown(prompt)

        # Add user message to chat history
        st.session_state.messages.append(
            ModelRequest(parts=[UserPromptPart(content=prompt)])
        )

        # Display assistant response in chat message container
        with st.chat_message("assistant"):
            response_container = st.empty()  # Placeholder for streaming response
            agent = Agent(
                "google-gla:gemini-2.0-flash-exp",
                # "openai:gpt-4o", # works!
                # https://ai.pydantic.dev/results/#structured-result-validation
                result_type=str,  # type: ignore
                system_prompt="you are a nice bot",
            )
            result = await agent.run(prompt)
            response_container.markdown(result.data)


if __name__ == "__main__":
    asyncio.run(main())

Metadata

Metadata

Assignees

Labels

more infoMore information requiredquestionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions