Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Assignees
Kludex
Labels
more infoMore information requiredquestionFurther information is requested
@asaf

Description

@asaf

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())

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


    [8]ページ先頭

    ©2009-2025 Movatter.jp