Skip to content

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

fix(model): await provider stream cleanup on public aclose (#3216)

mengxiaopeng2feb44390b22
diff --git a/src/agentscope/model/_base.py b/src/agentscope/model/_base.py
index 87092d6e3..d4782b2a5 100644
--- a/src/agentscope/model/_base.py
+++ b/src/agentscope/model/_base.py
@@ -283,6 +283,14 @@ class ChatModelBase:
except asyncio.CancelledError:
acc_res.finished_reason = FinishedReason.INTERRUPTED
yield_acc_res = True
+ finally:
+ # Closing this wrapper does not close the delegated iterator.
+ # Wait for provider cleanup before ``aclose`` returns.
+ aclose = getattr(res, "aclose", None)
+ if aclose is not None:
+ maybe_awaitable = aclose()
+ if inspect.isawaitable(maybe_awaitable):
+ await maybe_awaitable
if yield_acc_res:
yield acc_res.build()
diff --git a/tests/model_base_test.py b/tests/model_base_test.py
index 83b712c94..cf7780588 100644
--- a/tests/model_base_test.py
+++ b/tests/model_base_test.py
@@ -4,7 +4,7 @@ retry / accumulation / interrupt wrapper around ``_call_api``."""
import asyncio
import base64
import struct
-from typing import Any
+from typing import Any, AsyncGenerator
from unittest.async_case import IsolatedAsyncioTestCase
from utils import AnyString, MockModel
@@ -1353,3 +1353,93 @@ class StructuredOutputStrategyTest(IsolatedAsyncioTestCase):
("auto", {}),
],
)
+
+
+class _TrackedCleanupModel(MockModel):
+ """Model with observable asynchronous stream cleanup."""
+
+ def __init__(self) -> None:
+ """Initialize cleanup events."""
+ super().__init__(model="mock-model", stream=True)
+ self.cleanup_started = asyncio.Event()
+ self.cleanup_finished = asyncio.Event()
+ self.release_cleanup = asyncio.Event()
+
+ async def _call_api(
+ self,
+ *args: Any,
+ **kwargs: Any,
+ ) -> AsyncGenerator[ChatResponse, None]:
+ """Return a stream with gated cleanup."""
+ del args, kwargs
+ release_cleanup = self.release_cleanup
+ cleanup_started = self.cleanup_started
+ cleanup_finished = self.cleanup_finished
+
+ async def _inner() -> AsyncGenerator[ChatResponse, None]:
+ try:
+ yield ChatResponse(
+ content=[TextBlock(text="hello", id="t1")],
+ is_last=False,
+ )
+ yield ChatResponse(
+ content=[TextBlock(text=" world", id="t1")],
+ is_last=False,
+ )
+ finally:
+ cleanup_started.set()
+ await release_cleanup.wait()
+ cleanup_finished.set()
+
+ return _inner()
+
+
+class StreamDelegatedCloseTest(IsolatedAsyncioTestCase):
+ """Test delegated stream cleanup."""
+
+ async def test_aclose_after_first_chunk_closes_delegated_stream(
+ self,
+ ) -> None:
+ """Close the delegated stream after one chunk."""
+ model = _TrackedCleanupModel()
+ model.release_cleanup.set()
+ messages = [UserMsg(name="user", content="hi")]
+ gen = await model(messages=messages)
+ try:
+ first = await anext(gen)
+ self.assertEqual(first.content[0].text, "hello")
+ self.assertFalse(model.cleanup_finished.is_set())
+ await gen.aclose()
+ self.assertTrue(model.cleanup_started.is_set())
+ self.assertTrue(model.cleanup_finished.is_set())
+ finally:
+ model.release_cleanup.set()
+ await gen.aclose()
+
+ async def test_aclose_waits_for_asynchronous_cleanup(self) -> None:
+ """Wait for delegated asynchronous cleanup."""
+ model = _TrackedCleanupModel()
+ messages = [UserMsg(name="user", content="hi")]
+ gen = await model(messages=messages)
+ returned: list[str] = []
+
+ async def _close() -> None:
+ await gen.aclose()
+ returned.append("returned")
+
+ try:
+ first = await anext(gen)
+ self.assertEqual(first.content[0].text, "hello")
+ close_task = asyncio.create_task(_close())
+ await asyncio.wait_for(model.cleanup_started.wait(), timeout=1)
+ await asyncio.sleep(0)
+ self.assertFalse(close_task.done())
+ self.assertFalse(model.cleanup_finished.is_set())
+ self.assertEqual(returned, [])
+ model.release_cleanup.set()
+ await asyncio.wait_for(close_task, timeout=1)
+ self.assertEqual(returned, ["returned"])
+ self.assertTrue(model.cleanup_finished.is_set())
+ finally:
+ model.release_cleanup.set()
+ await gen.aclose()
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index b6788e68d..edae07538 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -5,12 +5,14 @@
Tests cover both non-streaming and streaming modes.
OpenAI Responses API uses event-based streaming with response.completed.
"""
+import asyncio
from typing import Any, cast
import unittest
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock
-from openai import APIError
+import httpx
+from openai import APIError, AsyncOpenAI
from openai.types.responses import ResponseOutputMessage
from pydantic import BaseModel
@@ -1612,3 +1614,64 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
fmt_tools, fmt_choice = self.model._format_tools(_FT_TOOLS, None)
self.assertEqual(fmt_tools, _FT_TOOLS_RESPONSE)
self.assertIsNone(fmt_choice)
+
+
+class _TrackedBody(httpx.AsyncByteStream):
+ """HTTP body with observable cleanup."""
+
+ def __init__(self) -> None:
+ """Initialize the body state."""
+ self.closed = False
+ self.stop = asyncio.Event()
+
+ async def __aiter__(self) -> Any:
+ """Yield one SSE event and wait."""
+ yield (
+ b'data: {"type":"response.output_text.delta",'
+ b'"sequence_number":0,"item_id":"msg_1","output_index":0,'
+ b'"content_index":0,"delta":"hello"}\n\n'
+ )
+ await self.stop.wait()
+
+ async def aclose(self) -> None:
+ """Close and unblock the body."""
+ self.closed = True
+ self.stop.set()
+
+
+class TestPublicModelStreamClose(IsolatedAsyncioTestCase):
+ """Test public stream cleanup."""
+
+ async def test_aclose_closes_offline_httpx_response(self) -> None:
+ """Test cleanup through the OpenAI SDK and HTTPX transport."""
+ body = _TrackedBody()
+
+ def _handler(_request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ stream=body,
+ )
+
+ transport = httpx.MockTransport(_handler)
+ client = AsyncOpenAI(
+ api_key="offline-test",
+ http_client=httpx.AsyncClient(transport=transport),
+ )
+ model = _make_model(stream=True)
+ original_client = model.client
+ model.client = client
+ stream = None
+ try:
+ stream = await model([])
+ first = await anext(stream)
+ self.assertEqual(first.content[0].text, "hello")
+ self.assertFalse(body.closed)
+ await stream.aclose()
+ self.assertTrue(body.closed)
+ finally:
+ if stream is not None:
+ await stream.aclose()
+ await body.aclose()
+ await client.close()
+ await original_client.close()