Skip to content

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

fix(embedding): deep-copy DashScope multimodal cache identifier (#3533)

Kiko03012929776bb666
diff --git a/src/agentscope/embedding/_dashscope/_model.py b/src/agentscope/embedding/_dashscope/_model.py
index 5d4d49e74..b80103175 100644
--- a/src/agentscope/embedding/_dashscope/_model.py
+++ b/src/agentscope/embedding/_dashscope/_model.py
@@ -16,6 +16,7 @@ Text payloads may be passed either as bare ``str`` or as
from __future__ import annotations
import asyncio
+import copy
from datetime import datetime
from dataclasses import dataclass
from typing import Any
@@ -435,11 +436,10 @@ class DashScopeEmbeddingModel(EmbeddingModelBase[str | TextBlock | DataBlock]):
**kwargs,
}
- # Exclude api_key from cache identifier to avoid persisting secrets
- # and to keep cache valid across key rotations.
- cache_identifier = {
- k: v for k, v in api_kwargs.items() if k != "api_key"
- }
+ # Exclude api_key and isolate the cache identifier from SDK mutations.
+ cache_identifier = copy.deepcopy(
+ {k: v for k, v in api_kwargs.items() if k != "api_key"},
+ )
if self.embedding_cache:
cached = await self.embedding_cache.retrieve(
diff --git a/tests/embedding_dashscope_test.py b/tests/embedding_dashscope_test.py
index 9aa5adc9c..2b3862ab8 100644
--- a/tests/embedding_dashscope_test.py
+++ b/tests/embedding_dashscope_test.py
@@ -330,6 +330,35 @@ class DashScopeMultimodalCallTest(IsolatedAsyncioTestCase):
},
)
+ @patch("dashscope.MultiModalEmbedding.call")
+ async def test_file_cache_survives_sdk_input_mutation(
+ self,
+ sdk: Any,
+ ) -> None:
+ """The store identifier matches the retrieve identifier even when
+ the SDK rewrites the input in place (upload preprocessing)."""
+
+ def mutating_call(**kwargs: Any) -> SimpleNamespace:
+ if kwargs["input"] and isinstance(kwargs["input"][0], dict):
+ kwargs["input"][0]["image"] = "oss://uploaded/img.png"
+ return _multimodal_resp(**kwargs)
+
+ with tempfile.TemporaryDirectory() as directory:
+ model = _multimodal_model(
+ 2560,
+ "qwen3-vl-embedding",
+ cache=FileEmbeddingCache(cache_dir=directory),
+ )
+ sdk.side_effect = mutating_call
+ first = await model([_img()])
+
+ sdk.side_effect = None
+ sdk.return_value = _multimodal_resp(input=[{"image": "x"}])
+ second = await model([_img()])
+
+ self.assertEqual(first.source, "api")
+ self.assertEqual(second.source, "cache")
+
@patch("dashscope.MultiModalEmbedding.call", side_effect=_multimodal_resp)
async def test_file_cache_separates_dimensions(self, sdk: Any) -> None:
"""Different contracts must not read each other's stored vectors."""