Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(embedding): preserve configured qwen3 multimodal dimensions (#3350)
HootRock1b810b362750
diff --git a/src/agentscope/embedding/_dashscope/_model.py b/src/agentscope/embedding/_dashscope/_model.pyindex 5da7aad7f..5d4d49e74 100644--- a/src/agentscope/embedding/_dashscope/_model.py+++ b/src/agentscope/embedding/_dashscope/_model.py@@ -431,6 +431,7 @@ class DashScopeEmbeddingModel(EmbeddingModelBase[str | TextBlock | DataBlock]):"input": formatted,"model": self.model,"api_key": self.api_key,+ "dimension": self.dimensions,**kwargs,}diff --git a/tests/embedding_dashscope_test.py b/tests/embedding_dashscope_test.pyindex 1f660e3db..9aa5adc9c 100644--- a/tests/embedding_dashscope_test.py+++ b/tests/embedding_dashscope_test.py@@ -1,11 +1,13 @@# -*- coding: utf-8 -*-# pylint: disable=protected-access,unused-argument"""Unit tests for DashScopeEmbeddingModel."""+import tempfilefrom dataclasses import asdictfrom threading import get_ident+from types import SimpleNamespacefrom typing import Anyfrom unittest import IsolatedAsyncioTestCase-from unittest.mock import AsyncMock, MagicMock, patch+from unittest.mock import AsyncMock, MagicMock, call, patchfrom utils import AnyValue@@ -14,6 +16,7 @@ from agentscope.embedding import (DashScopeEmbeddingModel,EmbeddingResponse,EmbeddingUsage,+ FileEmbeddingCache,)from agentscope.message import DataBlock, Base64Source, URLSource@@ -60,6 +63,35 @@ def _mock_resp(embeddings: list[list[float]]) -> EmbeddingResponse:)+def _multimodal_resp(**kwargs: Any) -> SimpleNamespace:+ """Return deterministic vectors without calling a provider."""+ dimension = kwargs.get("dimension", 2560)+ return SimpleNamespace(+ status_code=200,+ output={+ "embeddings": [+ {"embedding": [0.25] * dimension} for _ in kwargs["input"]+ ],+ },+ usage={"input_tokens": 1, "image_tokens": 0},+ )+++def _multimodal_model(+ dimensions: int,+ name: str,+ cache: FileEmbeddingCache | None = None,+) -> DashScopeEmbeddingModel:+ """Build a public model wrapper with an offline credential."""+ return DashScopeEmbeddingModel(+ credential=DashScopeCredential(api_key="offline-placeholder"),+ model=name,+ dimensions=dimensions,+ embedding_cache=cache,+ max_retries=0,+ )++class DashScopeListModelsTest(IsolatedAsyncioTestCase):"""Test list_models for DashScope."""@@ -206,7 +238,7 @@ class DashScopeTextCallTest(IsolatedAsyncioTestCase):class DashScopeMultimodalCallTest(IsolatedAsyncioTestCase):- """Test DashScope multimodal embedding via mocked _call_multimodal."""+ """Test multimodal batching, SDK requests, and embedding caches."""async def test_multimodal_text_and_image(self) -> None:"""Multimodal call with text + image."""@@ -274,3 +306,110 @@ class DashScopeMultimodalCallTest(IsolatedAsyncioTestCase):bad = DataBlock(source=Base64Source(data="x", media_type="video/mp4"))with self.assertRaises(ValueError):DashScopeEmbeddingModel._format_data_block(bad)++ def _assert_response(+ self,+ response: EmbeddingResponse,+ dimension: int,+ source: str,+ ) -> None:+ """Compare the complete response, matching only dynamic fields."""+ self.assertEqual(+ asdict(response),+ {+ "embeddings": [[0.25] * dimension],+ "id": AnyValue(),+ "created_at": AnyValue(),+ "type": "embedding",+ "usage": {+ "tokens": 0 if source == "cache" else 1,+ "time": 0 if source == "cache" else AnyValue(),+ "type": "embedding",+ },+ "source": source,+ },+ )++ @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."""+ name = "tongyi-embedding-vision-plus"+ with tempfile.TemporaryDirectory() as directory:+ cache = FileEmbeddingCache(cache_dir=directory)+ small = _multimodal_model(512, name=name, cache=cache)+ large = _multimodal_model(1024, name=name, cache=cache)+ first = await small(["same input"])+ second = await large(["same input"])+ repeated = await small(["same input"])+ self.assertEqual(+ sdk.call_args_list,+ [+ call(+ input=[{"text": "same input"}],+ model=name,+ api_key="offline-placeholder",+ dimension=dimension,+ )+ for dimension in (512, 1024)+ ],+ )+ self._assert_response(first, 512, "api")+ self._assert_response(second, 1024, "api")+ self._assert_response(repeated, 512, "cache")++ @patch("dashscope.MultiModalEmbedding.call", side_effect=_multimodal_resp)+ async def test_explicit_dimension_override(self, sdk: Any) -> None:+ """An explicit override determines the request and cache identity."""+ name = "tongyi-embedding-vision-plus"+ with tempfile.TemporaryDirectory() as directory:+ cache = FileEmbeddingCache(cache_dir=directory)+ first = await _multimodal_model(1024, name=name, cache=cache)(+ ["same input"],+ dimension=512,+ output_type="dense",+ )+ second = await _multimodal_model(512, name=name, cache=cache)(+ ["same input"],+ output_type="dense",+ )+ model = _multimodal_model(1024, name=name, cache=cache)+ reordered = await model(+ ["same input"],+ output_type="dense",+ dimension=512,+ )+ self.assertEqual(+ sdk.call_args_list,+ [+ call(+ input=[{"text": "same input"}],+ model=name,+ api_key="offline-placeholder",+ dimension=512,+ output_type="dense",+ ),+ ],+ )+ self._assert_response(first, 512, "api")+ self._assert_response(second, 512, "cache")+ self._assert_response(reordered, 512, "cache")++ @patch("dashscope.MultiModalEmbedding.call", side_effect=_multimodal_resp)+ async def test_multimodal_dimensions(self, sdk: Any) -> None:+ """Qwen3 and other multimodal models forward dimensions."""+ for name in ("qwen3-vl-embedding", "tongyi-embedding-vision-plus"):+ with self.subTest(model=name):+ sdk.reset_mock()+ response = await _multimodal_model(512, name=name)(["hello"])+ self.assertEqual(+ sdk.call_args_list,+ [+ call(+ input=[{"text": "hello"}],+ model=name,+ api_key="offline-placeholder",+ dimension=512,+ ),+ ],+ )+ self._assert_response(response, 512, "api")