- Notifications
You must be signed in to change notification settings - Fork1k
Open
Description
Hi,
This is an attempt to usegoogle-gla:gemini-2.0-flash-exp
via streamit in async,
Code below causesRuntimeError: Event loop is closed
, same code works just fine foropenai:gpt-4o
importasyncioimportstreamlitasstfrompydantic_aiimportAgentfrompydantic_ai.messagesimportModelRequest,UserPromptPartasyncdefmain():st.title("Simple chat")# Initialize chat historyif"messages"notinst.session_state:st.session_state.messages= []# Display chat messages from history on app rerunformessageinst.session_state.messages:part1=message.parts[0]role=""ifisinstance(part1,UserPromptPart):role="user"withst.chat_message("human"ifrole=="user"else"ai"):st.markdown(message.parts[0].content)# Accept user inputifprompt:=st.chat_input("Say something..."):# Display user message in chat message containerst.chat_message("user").markdown(prompt)# Add user message to chat historyst.session_state.messages.append(ModelRequest(parts=[UserPromptPart(content=prompt)]) )# Display assistant response in chat message containerwithst.chat_message("assistant"):response_container=st.empty()# Placeholder for streaming responseagent=Agent("google-gla:gemini-2.0-flash-exp",# "openai:gpt-4o", # works!# https://ai.pydantic.dev/results/#structured-result-validationresult_type=str,# type: ignoresystem_prompt="you are a nice bot", )result=awaitagent.run(prompt)response_container.markdown(result.data)if__name__=="__main__":asyncio.run(main())