Problem with handling a WS connection break between the server and the LLM
Use runner.run_live with gemini model
Sometimes there is a WS connection break between the server and the LLM.
The problem is that connection closure processing does not involve closing the LiveRequestQueueue queue.
That is, the queue continues to exist and receive messages, but the consumer is no longer there. Looking through the code I realized that at the moment there is no way to determine whether the connection is broken or not.
It would be nice to add a 'closed' flag to LiveRequestQueue and set it when the connection is closed. And add a custom LiveRequestQueueClosed error when invoke get or send (send_content ...).
Such a solution will allow to implement the check, and recreate the runner instance when the connection is lost.
I saw a note that shotdown is planned to be added. But in fact the 'closed' flag solves the problem completely, because you can recreate the runner with the same queue or create a new one. And we dont depend on python version.
I tried to think of a crutch. But so far I have not been able to get to the WS instance to implement the check. Please advise me on a temporary solution.
Reproduse: Run runner Turn off the internet Wait connection close Send new message
I found a temporary solution. Override methods to get to the websocket. Maybe someone will need it too.
import contextlib
from google.adk.agents import Agent
from google.adk.models import Gemini
from google.adk.models.gemini_llm_connection import GeminiLlmConnection
class CustomAgent(Agent):
_llm_con: GeminiLlmConnection | None = None
_llm: Gemini | None = None
_original_connect = None
@property
def _llm_flow(self):
flow = super()._llm_flow
original_get_llm = flow._BaseLlmFlow__get_llm
@contextlib.asynccontextmanager
async def custom_connect(*args, **kwargs):
async with self._original_connect(*args, **kwargs) as con:
self._llm_con = con
yield self._llm_con
def custom_get_llm(*args, **kwargs):
self._llm = original_get_llm(*args, **kwargs)
self._original_connect = self._llm.connect
self._llm.__dict__['connect'] = custom_connect
return self._llm
flow._BaseLlmFlow__get_llm = custom_get_llm
return flow
root_agent = CustomAgent(
name=ROOT_AGENT_NAME .... # init your agent with custom class
And then you can make check
from websockets import ClientConnection, State
....
async def alive(self):
ws: ClientConnection = self.runner.agent._llm_con._gemini_session._ws
return ws.state == State.OPEN
Hi @NikitaZemtsov and thanks to everyone who participated in this thread.
We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.
If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.
Thanks for your contribution and understanding!