Skip to content

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

fix(embedding): honor embedding_cache on the Gemini multimodal path (#3529)

Kiko0301c74c56cc5d3c
diff --git a/src/agentscope/embedding/_gemini/_model.py b/src/agentscope/embedding/_gemini/_model.py
index 53ada87b1..29dfed5ea 100644
--- a/src/agentscope/embedding/_gemini/_model.py
+++ b/src/agentscope/embedding/_gemini/_model.py
@@ -424,6 +424,35 @@ class GeminiEmbeddingModel(EmbeddingModelBase[str | TextBlock | DataBlock]):
}
config = types.EmbedContentConfig(**config_kwargs)
+ # Serialize content only for stable cache keys, excluding block
+ # IDs and timestamps.
+ cache_key = {
+ "model": self.model,
+ "contents": [
+ item
+ if isinstance(item, str)
+ else item.model_dump(mode="json", include={"source"})
+ for item in inputs
+ ],
+ # Transport options do not affect the embedding values.
+ **config.model_dump(
+ mode="json",
+ exclude_none=True,
+ exclude={"http_options"},
+ ),
+ }
+
+ if self.embedding_cache:
+ cached = await self.embedding_cache.retrieve(
+ identifier=cache_key,
+ )
+ if cached:
+ return EmbeddingResponse(
+ embeddings=cached,
+ usage=EmbeddingUsage(tokens=0, time=0),
+ source="cache",
+ )
+
start_time = datetime.now()
response = await self.client.aio.models.embed_content(
model=self.model,
@@ -434,6 +463,12 @@ class GeminiEmbeddingModel(EmbeddingModelBase[str | TextBlock | DataBlock]):
embeddings = [item.values for item in response.embeddings]
+ if self.embedding_cache:
+ await self.embedding_cache.store(
+ identifier=cache_key,
+ embeddings=embeddings,
+ )
+
return EmbeddingResponse(
embeddings=embeddings,
usage=EmbeddingUsage(time=time),
diff --git a/tests/embedding_gemini_test.py b/tests/embedding_gemini_test.py
index 7d842044a..f827947ce 100644
--- a/tests/embedding_gemini_test.py
+++ b/tests/embedding_gemini_test.py
@@ -7,6 +7,7 @@ from typing import Any
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, patch
+from google.genai import types
from utils import AnyValue
from agentscope.embedding import (
@@ -332,6 +333,102 @@ class GeminiMultimodalCallTest(IsolatedAsyncioTestCase):
},
)
+ async def test_multimodal_uses_embedding_cache(self) -> None:
+ """Multimodal results are cached and reused on repeat calls."""
+ model = self._make_multimodal_model()
+ with TemporaryDirectory() as directory:
+ model.embedding_cache = FileEmbeddingCache(cache_dir=directory)
+ model.client = MagicMock()
+ model.client.aio.models.embed_content = AsyncMock(
+ return_value=MagicMock(
+ embeddings=[MagicMock(values=[0.1, 0.2])],
+ ),
+ )
+
+ first = await model([_img()])
+ second = await model([_img()])
+
+ self.assertEqual(first.source, "api")
+ self.assertEqual(
+ asdict(second),
+ {
+ "embeddings": [[0.1, 0.2]],
+ "id": A,
+ "created_at": A,
+ "type": "embedding",
+ "usage": {"tokens": 0, "time": 0, "type": "embedding"},
+ "source": "cache",
+ },
+ )
+ self.assertEqual(
+ model.client.aio.models.embed_content.await_count,
+ 1,
+ )
+
+ async def test_multimodal_cache_ignores_http_options(self) -> None:
+ """Cache by dimensions while forwarding HTTP options to the SDK."""
+ model = self._make_multimodal_model()
+ model.dimensions = 2
+ short = [0.1, 0.2]
+ long = [0.1, 0.2, 0.3, 0.4]
+ model.client = MagicMock()
+ sdk = AsyncMock(
+ side_effect=[
+ MagicMock(embeddings=[MagicMock(values=short)]),
+ MagicMock(embeddings=[MagicMock(values=long)]),
+ ],
+ )
+ model.client.aio.models.embed_content = sdk
+ cases = (
+ (types.HttpOptions(timeout=10000), 2, "api", short),
+ (types.HttpOptions(timeout=20000), 2, "cache", short),
+ ({"timeout": 30000}, 2, "cache", short),
+ (types.HttpOptions(timeout=40000), 4, "api", long),
+ ({"timeout": 50000}, 4, "cache", long),
+ )
+ with TemporaryDirectory() as directory:
+ model.embedding_cache = FileEmbeddingCache(cache_dir=directory)
+ for index, (options, dimensions, source, vector) in enumerate(
+ cases,
+ ):
+ with self.subTest(case=index):
+ result = await model(
+ [_img()],
+ http_options=options,
+ output_dimensionality=dimensions,
+ )
+ self.assertEqual(
+ asdict(result),
+ {
+ "embeddings": [vector],
+ "id": A,
+ "created_at": A,
+ "type": "embedding",
+ "usage": {
+ "tokens": None if source == "api" else 0,
+ "time": A if source == "api" else 0,
+ "type": "embedding",
+ },
+ "source": source,
+ },
+ )
+ self.assertEqual(
+ [
+ call.kwargs["config"].model_dump(exclude_none=True)
+ for call in sdk.await_args_list
+ ],
+ [
+ {
+ "http_options": {"timeout": 10000},
+ "output_dimensionality": 2,
+ },
+ {
+ "http_options": {"timeout": 40000},
+ "output_dimensionality": 4,
+ },
+ ],
+ )
+
async def test_multimodal_batching_by_image_limit(self) -> None:
"""8 images with max_images=6 produces 2 batches (6+2)."""
model = self._make_multimodal_model()