Skip to content

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

fix(embedding): join sibling batches after failure (#3148)

creed906d4ef743fb
diff --git a/src/agentscope/embedding/_embedding_base.py b/src/agentscope/embedding/_embedding_base.py
index a103a795c..f5db83843 100644
--- a/src/agentscope/embedding/_embedding_base.py
+++ b/src/agentscope/embedding/_embedding_base.py
@@ -253,9 +253,38 @@ class EmbeddingModelBase(Generic[InputT]):
)
# Dispatch all batches concurrently, each with retry.
- results: list[EmbeddingResponse] = await asyncio.gather(
- *(self._call_with_retry(batch, **kwargs) for batch in batches),
- )
+ tasks = [
+ asyncio.create_task(self._call_with_retry(batch, **kwargs))
+ for batch in batches
+ ]
+ batches_result = asyncio.gather(*tasks)
+ try:
+ results: list[EmbeddingResponse] = await asyncio.shield(
+ batches_result,
+ )
+ except BaseException as error:
+ # gather propagates a batch error without stopping its siblings.
+ # Finish owned coroutine cleanup before returning to the caller.
+ for task in tasks:
+ if not task.done() and not task.cancelling():
+ task.cancel()
+ # Retrieve the original gather's exception on caller cancellation.
+ cleanup = asyncio.gather(
+ batches_result,
+ *tasks,
+ return_exceptions=True,
+ )
+ cancellation: asyncio.CancelledError | None = None
+ while True:
+ try:
+ await asyncio.shield(cleanup)
+ break
+ except asyncio.CancelledError as exc:
+ # Finish cleanup before propagating caller cancellation.
+ cancellation = exc
+ if cancellation is not None:
+ raise cancellation from error
+ raise
return self._merge_responses(results)
diff --git a/tests/embedding_base_test.py b/tests/embedding_base_test.py
index 8de90fde6..8babba7fd 100644
--- a/tests/embedding_base_test.py
+++ b/tests/embedding_base_test.py
@@ -1,12 +1,15 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
"""Unit tests for EmbeddingModelBase."""
+import asyncio
from dataclasses import asdict
-from typing import Literal
-from unittest import TestCase
+from typing import Any, Literal
+from unittest import IsolatedAsyncioTestCase, TestCase
+from unittest.mock import AsyncMock, patch
from utils import AnyString
+from agentscope.credential import OpenAICredential
from agentscope.embedding import (
EmbeddingModelBase,
EmbeddingResponse,
@@ -119,3 +122,261 @@ class EmbeddingModelBaseTest(TestCase):
),
),
)
+
+
+class _RetryableError(RuntimeError):
+ """A provider error eligible for retry."""
+
+
+class _LocalEmbedding(EmbeddingModelBase[str]):
+ """Exercise the base implementation without a provider connection."""
+
+ def __init__(self, max_retries: int = 0) -> None:
+ super().__init__(
+ credential=OpenAICredential(api_key="unused-test-key"),
+ model="local",
+ dimensions=1,
+ parameters=None,
+ context_size=100,
+ batch_size=1,
+ max_retries=max_retries,
+ retry_delay=0,
+ )
+
+ @classmethod
+ def _get_retryable_exceptions(cls) -> tuple[type[Exception], ...]:
+ return (_RetryableError,)
+
+ async def _call_api(
+ self,
+ inputs: list[str],
+ **_kwargs: Any,
+ ) -> EmbeddingResponse:
+ raise NotImplementedError
+
+
+class EmbeddingBatchLifecycleTest(IsolatedAsyncioTestCase):
+ """A failed call must finish its cooperative batch tasks."""
+
+ async def test_batch_failure_waits_for_sibling_cleanup(self) -> None:
+ """Join async cleanup and preserve the original provider error."""
+ for retryable in (False, True):
+ for inputs in (["fail", "slow"], ["slow", "fail"]):
+ with self.subTest(retryable=retryable, inputs=inputs):
+ model = _LocalEmbedding(max_retries=1 if retryable else 0)
+ started = asyncio.Event()
+ cleaning = asyncio.Event()
+ release = asyncio.Event()
+ finished = asyncio.Event()
+ error = (
+ _RetryableError("failed")
+ if retryable
+ else ValueError("failed")
+ )
+ attempts = 0
+
+ async def call_api(
+ batch: list[str],
+ *,
+ call_started: asyncio.Event = started,
+ call_error: Exception = error,
+ call_cleaning: asyncio.Event = cleaning,
+ call_release: asyncio.Event = release,
+ call_finished: asyncio.Event = finished,
+ **_kwargs: Any,
+ ) -> None:
+ nonlocal attempts
+ if batch == ["fail"]:
+ await call_started.wait()
+ attempts += 1
+ raise call_error
+ call_started.set()
+ try:
+ await asyncio.Event().wait()
+ finally:
+ call_cleaning.set()
+ await call_release.wait()
+ call_finished.set()
+
+ with patch.object(
+ model,
+ "_call_api",
+ side_effect=call_api,
+ ):
+ call = asyncio.create_task(model(inputs))
+ try:
+ await asyncio.wait_for(cleaning.wait(), 1)
+ self.assertFalse(call.done())
+ release.set()
+ with self.assertRaises(type(error)) as caught:
+ await asyncio.wait_for(call, 1)
+ self.assertIs(caught.exception, error)
+ self.assertTrue(finished.is_set())
+ self.assertEqual(attempts, 2 if retryable else 1)
+ finally:
+ release.set()
+ call.cancel()
+ await asyncio.gather(call, return_exceptions=True)
+
+ async def test_parent_cancellation_joins_batches(self) -> None:
+ """Repeated cancellation waits for all batch cleanup to finish."""
+ model = _LocalEmbedding()
+ started = [asyncio.Event(), asyncio.Event()]
+ finished = []
+ cleaning = asyncio.Event()
+ release = asyncio.Event()
+
+ async def call_api(batch: list[str], **_kwargs: Any) -> None:
+ index = int(batch[0])
+ started[index].set()
+ try:
+ await asyncio.Event().wait()
+ finally:
+ if index == 1:
+ cleaning.set()
+ await release.wait()
+ finished.append(index)
+
+ with patch.object(model, "_call_api", side_effect=call_api):
+ call = asyncio.create_task(model(["0", "1"]))
+ try:
+ await asyncio.wait_for(
+ asyncio.gather(*(event.wait() for event in started)),
+ 1,
+ )
+ call.cancel()
+ await asyncio.wait_for(cleaning.wait(), 1)
+ await asyncio.sleep(0)
+ self.assertFalse(call.done())
+ for _ in range(2):
+ call.cancel()
+ await asyncio.sleep(0)
+ self.assertFalse(call.done())
+ release.set()
+ with self.assertRaises(asyncio.CancelledError):
+ await asyncio.wait_for(call, 1)
+ self.assertCountEqual(finished, [0, 1])
+ finally:
+ release.set()
+ call.cancel()
+ await asyncio.gather(call, return_exceptions=True)
+
+ async def test_cancelled_batch_joins_sibling(self) -> None:
+ """A child cancellation also cleans up the other batch."""
+ model = _LocalEmbedding()
+ started = asyncio.Event()
+ finished = asyncio.Event()
+
+ async def call_api(batch: list[str], **_kwargs: Any) -> None:
+ if batch == ["cancel"]:
+ await started.wait()
+ raise asyncio.CancelledError
+ started.set()
+ try:
+ await asyncio.Event().wait()
+ finally:
+ await asyncio.sleep(0)
+ finished.set()
+
+ with patch.object(model, "_call_api", side_effect=call_api):
+ with self.assertRaises(asyncio.CancelledError):
+ await asyncio.wait_for(model(["cancel", "slow"]), 1)
+ self.assertTrue(finished.is_set())
+
+ async def test_success_preserves_input_order(self) -> None:
+ """Merge in input order even when batches finish in reverse order."""
+ model = _LocalEmbedding()
+ second_finished = asyncio.Event()
+
+ async def call_api(
+ batch: list[str],
+ **_kwargs: Any,
+ ) -> EmbeddingResponse:
+ if batch == ["first"]:
+ await second_finished.wait()
+ return EmbeddingResponse(embeddings=[[1.0]])
+ second_finished.set()
+ return EmbeddingResponse(embeddings=[[2.0]])
+
+ with patch.object(model, "_call_api", side_effect=call_api):
+ response = await asyncio.wait_for(model(["first", "second"]), 1)
+ self.assertEqual(
+ asdict(response),
+ asdict(
+ EmbeddingResponse(
+ embeddings=[[1.0], [2.0]],
+ id=AnyString(),
+ created_at=AnyString(),
+ usage=EmbeddingUsage(tokens=0, time=0),
+ ),
+ ),
+ )
+
+ async def test_empty_input_does_not_call_provider(self) -> None:
+ """Empty input creates no batch work."""
+ model = _LocalEmbedding()
+ with patch.object(model, "_call_api", new_callable=AsyncMock) as call:
+ response = await model([])
+ self.assertEqual(
+ asdict(response),
+ asdict(
+ EmbeddingResponse(
+ embeddings=[],
+ id=AnyString(),
+ created_at=AnyString(),
+ usage=EmbeddingUsage(tokens=0, time=0),
+ ),
+ ),
+ )
+ call.assert_not_awaited()
+
+ async def test_parent_cancellation_during_failure_cleanup(self) -> None:
+ """Finish sibling cleanup despite repeated caller cancellation."""
+ for cancel_count in (1, 3):
+ with self.subTest(cancel_count=cancel_count):
+ model = _LocalEmbedding()
+ started = asyncio.Event()
+ cleaning = asyncio.Event()
+ release = asyncio.Event()
+ finished = asyncio.Event()
+
+ async def call_api(
+ batch: list[str],
+ *,
+ call_started: asyncio.Event = started,
+ call_cleaning: asyncio.Event = cleaning,
+ call_release: asyncio.Event = release,
+ call_finished: asyncio.Event = finished,
+ **_kwargs: Any,
+ ) -> None:
+ if batch == ["fail"]:
+ await call_started.wait()
+ raise ValueError("failed")
+ call_started.set()
+ try:
+ await asyncio.Event().wait()
+ finally:
+ call_cleaning.set()
+ await call_release.wait()
+ call_finished.set()
+
+ with patch.object(model, "_call_api", side_effect=call_api):
+ call = asyncio.create_task(model(["fail", "slow"]))
+ try:
+ await asyncio.wait_for(cleaning.wait(), 1)
+ for _ in range(cancel_count):
+ call.cancel()
+ await asyncio.sleep(0)
+ self.assertFalse(call.done())
+ self.assertFalse(finished.is_set())
+ release.set()
+ with self.assertRaises(asyncio.CancelledError):
+ await asyncio.wait_for(call, 1)
+ self.assertEqual(
+ (cleaning.is_set(), finished.is_set()),
+ (True, True),
+ )
+ finally:
+ release.set()
+ call.cancel()
+ await asyncio.gather(call, return_exceptions=True)
diff --git a/tests/embedding_openai_test.py b/tests/embedding_openai_test.py
index f84b9e0f2..88a2abb64 100644
--- a/tests/embedding_openai_test.py
+++ b/tests/embedding_openai_test.py
@@ -114,23 +114,26 @@ class OpenAIEmbeddingCallTest(IsolatedAsyncioTestCase):
self,
mock_client_cls: Any,
) -> None:
- """Unchanged requests must still hit cache keys from older versions."""
+ """Requests with extra arguments hit the endpoint-specific cache."""
embeddings = [[0.1, 0.2]]
mock_client = mock_client_cls.return_value
+ mock_client.base_url = "https://api.openai.com/v1/"
mock_client.embeddings.create = AsyncMock(
return_value=_make_response(embeddings, 1),
)
with TemporaryDirectory() as cache_dir:
cache = FileEmbeddingCache(cache_dir=cache_dir)
- # Older requests appended dimensions after per-call arguments.
await cache.store(
embeddings=embeddings,
identifier={
- "input": ["hello"],
- "model": "text-embedding-3-small",
- "encoding_format": "float",
- "user": "test-user",
- "dimensions": 2,
+ "base_url": "https://api.openai.com/v1/",
+ "api_kwargs": {
+ "input": ["hello"],
+ "model": "text-embedding-3-small",
+ "encoding_format": "float",
+ "user": "test-user",
+ "dimensions": 2,
+ },
},
)
model = OpenAIEmbeddingModel(