Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(dashscope): assigns content as an empty list for messages without valid content blocks (#1141)
qbc1c083025aed1
diff --git a/src/agentscope/formatter/_dashscope_formatter.py b/src/agentscope/formatter/_dashscope_formatter.pyindex ed33a99ee..766876b99 100644--- a/src/agentscope/formatter/_dashscope_formatter.py+++ b/src/agentscope/formatter/_dashscope_formatter.py@@ -148,6 +148,24 @@ class DashScopeChatFormatter(TruncatedFormatterBase):"""The DashScope formatter class for chatbot scenario, where only a userand an agent are involved. We use the `role` field to identify differententities in the conversation.++ .. warning::+ Known Issues with DashScope API:++ 1. **Missing content field**: When messages lack the 'content' field,+ qwen-vl-max models will raise ``KeyError: 'content'``.++ 2. **None content value**: When content is ``None``, qwen-vl-max models+ will raise ``TypeError: 'NoneType' object is not iterable``.++ 3. **Empty text in content**: When content contains+ ``[{"text": None}]``, qwen3-max may repeatedly invoke tools+ multiple times. Note that when qwen3-max initiates tool calls,+ the returned message contains ``"content": ""``.++ To avoid these issues, this formatter assigns content as an empty+ list ``[]`` for messages without valid content blocks.+"""support_tools_api: bool = True@@ -377,17 +395,13 @@ class DashScopeChatFormatter(TruncatedFormatterBase):msg_dashscope = {"role": msg.role,- "content": content_blocks or [{"text": None}],+ "content": content_blocks,}if tool_calls:msg_dashscope["tool_calls"] = tool_calls- if msg_dashscope["content"] != [- {"text": None},- ] or msg_dashscope.get(- "tool_calls",- ):+ if msg_dashscope["content"] or msg_dashscope.get("tool_calls"):formatted_msgs.append(msg_dashscope)# Move to next messagediff --git a/src/agentscope/model/_dashscope_model.py b/src/agentscope/model/_dashscope_model.pyindex c17a08e70..062642042 100644--- a/src/agentscope/model/_dashscope_model.py+++ b/src/agentscope/model/_dashscope_model.py@@ -152,15 +152,6 @@ class DashScopeChatModel(ChatModelBase):"""import dashscope- # For qvq and qwen-vl models, the content field cannot be `None` or- # `[{"text": None}]`, so we need to convert it to an empty list.- if self.model_name.startswith("qvq") or "-vl" in self.model_name:- for msg in messages:- if msg["content"] is None or msg["content"] == [- {"text": None},- ]:- msg["content"] = []-kwargs = {"messages": messages,"model": self.model_name,diff --git a/tests/formatter_dashscope_test.py b/tests/formatter_dashscope_test.pyindex 61bb56c92..e52c8b57c 100644--- a/tests/formatter_dashscope_test.py+++ b/tests/formatter_dashscope_test.py@@ -257,7 +257,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [{"text": None}],+ "content": [],"tool_calls": [{"id": "1",@@ -319,11 +319,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [- {- "text": None,- },- ],+ "content": [],"tool_calls": [{"id": "1",@@ -360,11 +356,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [- {- "text": None,- },- ],+ "content": [],"tool_calls": [{"id": "1",@@ -429,11 +421,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [- {- "text": None,- },- ],+ "content": [],"tool_calls": [{"id": "1",@@ -463,11 +451,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [- {- "text": None,- },- ],+ "content": [],"tool_calls": [{"id": "1",@@ -628,7 +612,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [{"text": None}],+ "content": [],"tool_calls": [{"id": "1",@@ -852,11 +836,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):},{"role": "assistant",- "content": [- {- "text": None,- },- ],+ "content": [],"tool_calls": [{"id": "1",