Skip to content

Build and run agents you can see, understand and trust.

fix(realtime): drop the previous session's terminal events on reconnect (#2588)

Lukas Buck811f043b7fdc
diff --git a/src/agentscope/realtime/_dashscope/_model.py b/src/agentscope/realtime/_dashscope/_model.py
index dfc561ac2..b342e08a9 100644
--- a/src/agentscope/realtime/_dashscope/_model.py
+++ b/src/agentscope/realtime/_dashscope/_model.py
@@ -92,6 +92,11 @@ class DashScopeRealtimeModel(RealtimeModelBase):
"""Open the WebSocket and send the session config."""
import websockets
+ # Stop the previous session and drop its terminal events.
+ await self.close()
+ while not self._queue.empty():
+ self._queue.get_nowait()
+
if kwargs.get("turn_detection_disabled"):
self.parameters = self.parameters.model_copy(
update={"turn_detection": "none"},
diff --git a/src/agentscope/realtime/_gemini/_model.py b/src/agentscope/realtime/_gemini/_model.py
index b21487f53..ceb4a7bf1 100644
--- a/src/agentscope/realtime/_gemini/_model.py
+++ b/src/agentscope/realtime/_gemini/_model.py
@@ -120,6 +120,11 @@ class GeminiRealtimeModel(RealtimeModelBase):
"""Open the WebSocket and send the setup message."""
import websockets
+ # Stop the previous session and drop its terminal events.
+ await self.close()
+ while not self._queue.empty():
+ self._queue.get_nowait()
+
if kwargs.get("turn_detection_disabled"):
self.parameters = self.parameters.model_copy(
update={"turn_detection": "none"},
@@ -146,14 +151,15 @@ class GeminiRealtimeModel(RealtimeModelBase):
raise ModelDisconnectedError("Session closed during setup.")
async def close(self) -> None:
- """Stop reading and close the WebSocket."""
+ """Close the WebSocket and stop reading."""
+ # Close the socket first: the reader's finally resets ``self._ws``.
+ if self._ws is not None:
+ await self._ws.close()
+ self._ws = None
if self._reader is not None:
self._reader.cancel()
await asyncio.gather(self._reader, return_exceptions=True)
self._reader = None
- if self._ws is not None:
- await self._ws.close()
- self._ws = None
async def events(self) -> AsyncIterator[me.ModelEvent]:
"""Yield events until the session ends."""
diff --git a/src/agentscope/realtime/_openai/_model.py b/src/agentscope/realtime/_openai/_model.py
index 30cf6e2cc..8b88f8239 100644
--- a/src/agentscope/realtime/_openai/_model.py
+++ b/src/agentscope/realtime/_openai/_model.py
@@ -108,6 +108,11 @@ class OpenAIRealtimeModel(RealtimeModelBase):
"""Open the WebSocket and send the session config."""
import websockets
+ # Stop the previous session and drop its terminal events.
+ await self.close()
+ while not self._queue.empty():
+ self._queue.get_nowait()
+
if kwargs.get("turn_detection_disabled"):
self.parameters = self.parameters.model_copy(
update={"turn_detection": "none"},
diff --git a/src/agentscope/realtime/_xai/_model.py b/src/agentscope/realtime/_xai/_model.py
index 280512784..3e1e889eb 100644
--- a/src/agentscope/realtime/_xai/_model.py
+++ b/src/agentscope/realtime/_xai/_model.py
@@ -100,6 +100,11 @@ class XAIRealtimeModel(RealtimeModelBase):
"""Open the WebSocket and send the session config."""
import websockets
+ # Stop the previous session and drop its terminal events.
+ await self.close()
+ while not self._queue.empty():
+ self._queue.get_nowait()
+
if kwargs.get("turn_detection_disabled"):
self.parameters = self.parameters.model_copy(
update={"turn_detection": "none"},
diff --git a/tests/realtime_reconnect_test.py b/tests/realtime_reconnect_test.py
new file mode 100644
index 000000000..bb21a2102
--- /dev/null
+++ b/tests/realtime_reconnect_test.py
@@ -0,0 +1,148 @@
+# -*- coding: utf-8 -*-
+"""Reconnecting a realtime model must give the new session a clean event
+stream (issue #2587)."""
+# pylint: disable=protected-access
+import asyncio
+import json
+from typing import Any
+from unittest.async_case import IsolatedAsyncioTestCase
+from unittest.mock import AsyncMock, patch
+
+from agentscope.credential import (
+ DashScopeCredential,
+ GeminiCredential,
+ OpenAICredential,
+ XAICredential,
+)
+from agentscope.realtime import (
+ DashScopeRealtimeModel,
+ GeminiRealtimeModel,
+ OpenAIRealtimeModel,
+ RealtimeModelBase,
+ XAIRealtimeModel,
+)
+from agentscope.realtime import _events as me
+
+
+class IdleSocket:
+ """A WebSocket that replays a few frames, then stays open until closed."""
+
+ def __init__(self, frames: list[dict]) -> None:
+ self._frames = [json.dumps(_) for _ in frames]
+ self._closed = asyncio.Event()
+ self.sent: list[Any] = []
+
+ async def send(self, payload: str) -> None:
+ """Record the payload and yield like a real write, so the reader
+ task gets to start."""
+ self.sent.append(json.loads(payload))
+ await asyncio.sleep(0)
+
+ async def close(self) -> None:
+ """Release the reader."""
+ self._closed.set()
+
+ def __aiter__(self) -> "IdleSocket":
+ return self
+
+ async def __anext__(self) -> str:
+ if self._frames:
+ return self._frames.pop(0)
+ await self._closed.wait()
+ raise StopAsyncIteration
+
+
+class ReconnectTest(IsolatedAsyncioTestCase):
+ """The previous session must not end or break the new one."""
+
+ def setUp(self) -> None:
+ """Build one model per provider; Gemini's connect() waits for
+ setupComplete, so its socket replays that frame."""
+ self.models: list[tuple[RealtimeModelBase, list[dict]]] = [
+ (
+ OpenAIRealtimeModel(
+ "gpt-realtime-1.5",
+ OpenAICredential(api_key="sk-x"),
+ ),
+ [],
+ ),
+ (
+ DashScopeRealtimeModel(
+ "qwen-omni-turbo-realtime",
+ DashScopeCredential(api_key="sk-x"),
+ ),
+ [],
+ ),
+ (
+ XAIRealtimeModel(
+ "grok-voice-latest",
+ XAICredential(api_key="sk-x"),
+ ),
+ [],
+ ),
+ (
+ GeminiRealtimeModel(
+ "gemini-2.5-flash-native-audio-preview-12-2025",
+ GeminiCredential(api_key="key-x"),
+ ),
+ [{"setupComplete": {}}],
+ ),
+ ]
+
+ async def _connect(self, model: Any, frames: list[dict]) -> IdleSocket:
+ """Connect the model to a fresh idle socket."""
+ socket = IdleSocket(frames)
+ with patch("websockets.connect", new=AsyncMock(return_value=socket)):
+ await model.connect("Be brief.")
+ return socket
+
+ async def _read_new_session(self, model: Any) -> list[me.ModelEvent]:
+ """Feed one event of the new session, end it, and read the stream."""
+ model._queue.put_nowait(me.ResponseCreatedEvent(item_id="item_new"))
+ model._queue.put_nowait(None)
+ got = [event async for event in model.events()]
+ await model.close()
+ return got
+
+ async def test_reconnect_after_close(self) -> None:
+ """connect -> close -> connect: the closed session's terminal events
+ are not replayed to the new stream."""
+ for model, frames in self.models:
+ with self.subTest(model=type(model).__name__):
+ await self._connect(model, frames)
+ await model.close()
+ await self._connect(model, frames)
+ self.assertListEqual(
+ await self._read_new_session(model),
+ [me.ResponseCreatedEvent(item_id="item_new")],
+ )
+
+ async def test_reconnect_without_close(self) -> None:
+ """connect -> connect: the old reader and socket are stopped before
+ the new session starts, so they can't end or break it."""
+ for model, frames in self.models:
+ with self.subTest(model=type(model).__name__):
+ old_socket = await self._connect(model, frames)
+ old_reader = model._reader
+ new_socket = await self._connect(model, frames)
+ self.assertTrue(old_reader.done())
+ self.assertTrue(old_socket._closed.is_set())
+ self.assertIs(model._ws, new_socket)
+ self.assertListEqual(
+ await self._read_new_session(model),
+ [me.ResponseCreatedEvent(item_id="item_new")],
+ )
+
+ async def test_events_after_close_without_reconnect_still_end(
+ self,
+ ) -> None:
+ """Draining happens only on connect(); a closed session still
+ terminates its consumer with the terminal event."""
+ for model, frames in self.models:
+ with self.subTest(model=type(model).__name__):
+ await self._connect(model, frames)
+ await model.close()
+ self.assertListEqual(
+ [event async for event in model.events()],
+ [me.SessionEndedEvent(reason="closed")],
+ )