Skip to content

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

refactor(model): refactor dashscope model to openai compatible (#1617)

qbcec0a6e255912
diff --git a/scripts/model_examples/README.md b/scripts/model_examples/README.md
index b953326e1..e7b0822fc 100644
--- a/scripts/model_examples/README.md
+++ b/scripts/model_examples/README.md
@@ -212,7 +212,7 @@ Each script typically defines two or more async functions:
- `example_simple_call()` – basic text call with streaming
- `example_tool_call()` – two-round conversation with tool/function calling
-- `example_multimodal_call()` – image + text input (in `_multimodal.py` variants)
+- `example_image_url()` – image + text input (in `_multimodal.py` variants)
---
diff --git a/scripts/model_examples/_utils.py b/scripts/model_examples/_utils.py
index 7f8400a59..534d0a6d5 100644
--- a/scripts/model_examples/_utils.py
+++ b/scripts/model_examples/_utils.py
@@ -2,7 +2,7 @@
"""Shared utility helpers for model-examples scripts."""
from typing import AsyncGenerator
-from agentscope.message import TextBlock, ThinkingBlock
+from agentscope.message import TextBlock, ThinkingBlock, DataBlock
from agentscope.model import ChatResponse
@@ -52,6 +52,17 @@ async def stream_and_collect(
print(final_text)
if in_thinking:
print()
+ # Report audio output if present in the final chunk
+ if final is not None:
+ import base64 as _b64
+
+ for block in final.content:
+ if isinstance(block, DataBlock):
+ media = block.source.media_type
+ byte_size = len(_b64.b64decode(block.source.data))
+ print(
+ f"[Audio output: {media}, " f"{byte_size} bytes]",
+ )
print()
assert final is not None
return final
diff --git a/scripts/model_examples/anthropic_multiagent_multimodal.py b/scripts/model_examples/anthropic_multiagent_multimodal.py
index 25f131b2c..a4d79569d 100644
--- a/scripts/model_examples/anthropic_multiagent_multimodal.py
+++ b/scripts/model_examples/anthropic_multiagent_multimodal.py
@@ -21,7 +21,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = AnthropicMultiAgentFormatter()
@@ -82,4 +82,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/anthropic_multimodal.py b/scripts/model_examples/anthropic_multimodal.py
index 9c9920590..9b5bd0db5 100644
--- a/scripts/model_examples/anthropic_multimodal.py
+++ b/scripts/model_examples/anthropic_multimodal.py
@@ -24,7 +24,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call claude-opus-4-5 with an image URL and ask what is in the image."""
model = AnthropicChatModel(
credential=AnthropicCredential(
@@ -71,7 +71,7 @@ def _build_model() -> AnthropicChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call claude-opus-4-5 with a local image using a ``file://`` URL.
The formatter automatically reads the file and converts it to base64.
@@ -104,7 +104,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call claude-opus-4-5 with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -141,6 +141,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/dashscope_call.py b/scripts/model_examples/dashscope_call.py
index 7e4a7ea47..ac77abbda 100644
--- a/scripts/model_examples/dashscope_call.py
+++ b/scripts/model_examples/dashscope_call.py
@@ -27,7 +27,6 @@ async def example_simple_call() -> None:
api_key=os.environ["DASHSCOPE_API_KEY"],
),
model="qwen3.5-plus",
- multimodality=True,
stream=True,
context_size=1_000_000,
parameters=DashScopeChatModel.Parameters(thinking_enable=True),
@@ -77,7 +76,6 @@ async def example_tool_call() -> None:
),
model="qwen3.5-plus",
stream=True,
- multimodality=True,
context_size=1_000_000,
parameters=DashScopeChatModel.Parameters(thinking_enable=True),
)
diff --git a/scripts/model_examples/dashscope_multiagent.py b/scripts/model_examples/dashscope_multiagent.py
index f03e90436..c2d5a19fa 100644
--- a/scripts/model_examples/dashscope_multiagent.py
+++ b/scripts/model_examples/dashscope_multiagent.py
@@ -28,7 +28,6 @@ async def example_multiagent() -> None:
api_key=os.environ["DASHSCOPE_API_KEY"],
),
model="qwen3.5-plus",
- multimodality=True,
stream=True,
context_size=1_000_000,
formatter=formatter,
diff --git a/scripts/model_examples/dashscope_multiagent_multimodal.py b/scripts/model_examples/dashscope_multiagent_multimodal.py
index ceb37e78b..b9189b6a1 100644
--- a/scripts/model_examples/dashscope_multiagent_multimodal.py
+++ b/scripts/model_examples/dashscope_multiagent_multimodal.py
@@ -20,7 +20,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = DashScopeMultiAgentFormatter()
@@ -29,7 +29,6 @@ async def example_multiagent_multimodal() -> None:
api_key=os.environ["DASHSCOPE_API_KEY"],
),
model="qwen3.5-plus",
- multimodality=True,
stream=True,
context_size=1_000_000,
formatter=formatter,
@@ -82,4 +81,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/dashscope_multimodal.py b/scripts/model_examples/dashscope_multimodal.py
index 51f481dc0..973a0de2b 100644
--- a/scripts/model_examples/dashscope_multimodal.py
+++ b/scripts/model_examples/dashscope_multimodal.py
@@ -16,21 +16,32 @@ from agentscope.message import (
from agentscope.model import DashScopeChatModel
from agentscope.credential import DashScopeCredential
-# A publicly accessible test image (a simple cat photo)
+# A publicly accessible test image
TEST_IMAGE_URL = (
"https://help-static-aliyun-doc.aliyuncs.com/file-manage"
"-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
)
+# A publicly accessible test video
+TEST_VIDEO_URL = (
+ "https://help-static-aliyun-doc.aliyuncs.com/file-manage"
+ "-files/zh-CN/20241115/cqqkru/1.mp4"
+)
-async def example_multimodal() -> None:
+# A publicly accessible test audio
+TEST_AUDIO_URL = (
+ "https://help-static-aliyun-doc.aliyuncs.com/file-manage"
+ "-files/zh-CN/20250211/tixcef/cherry.wav"
+)
+
+
+async def example_image_url() -> None:
"""Call qwen3.5-plus with an image URL and ask what is in the image."""
model = DashScopeChatModel(
credential=DashScopeCredential(
api_key=os.environ["DASHSCOPE_API_KEY"],
),
model="qwen3.5-plus",
- multimodality=True,
stream=True,
context_size=1_000_000,
)
@@ -66,13 +77,12 @@ def _build_model() -> DashScopeChatModel:
api_key=os.environ["DASHSCOPE_API_KEY"],
),
model="qwen3.5-plus",
- multimodality=True,
stream=True,
context_size=1_000_000,
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call qwen3.5-plus with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to a base64 data
@@ -106,7 +116,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call qwen3.5-plus with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -142,7 +152,78 @@ async def example_file_url() -> None:
await stream_and_collect(await model(msgs))
+async def example_video() -> None:
+ """Call qwen3.5-plus with a video URL and ask what is in the video."""
+ model = _build_model()
+
+ video_block = DataBlock(
+ source=URLSource(
+ url=TEST_VIDEO_URL,
+ media_type="video/mp4",
+ ),
+ )
+
+ msgs = [
+ Msg(
+ name="user",
+ content=[
+ TextBlock(
+ text="What is happening in this video? "
+ "Describe it briefly.",
+ ),
+ video_block,
+ ],
+ role="user",
+ ),
+ ]
+
+ print("=== Multimodal Call (Video URL) ===")
+ await stream_and_collect(await model(msgs))
+
+
+async def example_audio() -> None:
+ """Call qwen3.5-omni-plus with an audio URL.
+
+ Audio understanding requires an Omni model (qwen3.5-omni-plus or
+ Qwen3-Omni-Flash). Omni models also require stream=True and the
+ ``modalities`` parameter.
+ """
+ model = DashScopeChatModel(
+ credential=DashScopeCredential(
+ api_key=os.environ["DASHSCOPE_API_KEY"],
+ ),
+ model="qwen3.5-omni-plus",
+ stream=True,
+ context_size=1_000_000,
+ )
+
+ audio_block = DataBlock(
+ source=URLSource(
+ url=TEST_AUDIO_URL,
+ media_type="audio/wav",
+ ),
+ )
+
+ msgs = [
+ Msg(
+ name="user",
+ content=[
+ TextBlock(text="What is being said in this audio clip?"),
+ audio_block,
+ ],
+ role="user",
+ ),
+ ]
+
+ print("=== Multimodal Call (Audio URL - Omni) ===")
+ await stream_and_collect(
+ await model(msgs, modalities=["text", "audio"]),
+ )
+
+
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
+ asyncio.run(example_video())
+ asyncio.run(example_audio())
diff --git a/scripts/model_examples/gemini_multiagent_multimodal.py b/scripts/model_examples/gemini_multiagent_multimodal.py
index 9e632e510..4ce040a7e 100644
--- a/scripts/model_examples/gemini_multiagent_multimodal.py
+++ b/scripts/model_examples/gemini_multiagent_multimodal.py
@@ -15,7 +15,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = GeminiMultiAgentFormatter()
@@ -76,4 +76,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/gemini_multimodal.py b/scripts/model_examples/gemini_multimodal.py
index c2e8814bb..ed78c4fe0 100644
--- a/scripts/model_examples/gemini_multimodal.py
+++ b/scripts/model_examples/gemini_multimodal.py
@@ -23,7 +23,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call gemini-2.5-flash with an image URL and ask what is in the image."""
model = GeminiChatModel(
credential=GeminiCredential(
@@ -67,7 +67,7 @@ def _build_model() -> GeminiChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call gemini-2.5-flash with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to base64.
@@ -98,7 +98,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call gemini-2.5-flash with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -130,6 +130,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/kimi_multiagent_multimodal.py b/scripts/model_examples/kimi_multiagent_multimodal.py
index a4858bd46..489660b38 100644
--- a/scripts/model_examples/kimi_multiagent_multimodal.py
+++ b/scripts/model_examples/kimi_multiagent_multimodal.py
@@ -15,7 +15,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = KimiMultiAgentFormatter()
@@ -76,4 +76,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/kimi_multimodal.py b/scripts/model_examples/kimi_multimodal.py
index 8c2b34a63..c0b544fcd 100644
--- a/scripts/model_examples/kimi_multimodal.py
+++ b/scripts/model_examples/kimi_multimodal.py
@@ -23,7 +23,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call kimi-k2.6 with an image URL and ask what is in the image."""
model = KimiChatModel(
credential=KimiCredential(
@@ -67,7 +67,7 @@ def _build_model() -> KimiChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call kimi-k2.6 with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to a base64 data
@@ -99,7 +99,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call kimi-k2.6 with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -131,6 +131,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/ollama_multiagent_multimodal.py b/scripts/model_examples/ollama_multiagent_multimodal.py
index 4e9abd9ed..b55f0aba6 100644
--- a/scripts/model_examples/ollama_multiagent_multimodal.py
+++ b/scripts/model_examples/ollama_multiagent_multimodal.py
@@ -17,7 +17,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group.
Requires `ollama pull llava` to have been run first.
@@ -78,4 +78,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/ollama_multimodal.py b/scripts/model_examples/ollama_multimodal.py
index 461ae3973..33a333139 100644
--- a/scripts/model_examples/ollama_multimodal.py
+++ b/scripts/model_examples/ollama_multimodal.py
@@ -25,7 +25,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call llava:7b with an image URL and ask what is in the image.
Requires `ollama pull llava` to be run first.
@@ -68,7 +68,7 @@ def _build_model() -> OllamaChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call llava:7b with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to base64.
@@ -99,7 +99,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call llava:7b with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -131,6 +131,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/openai_chat_multiagent_multimodal.py b/scripts/model_examples/openai_chat_multiagent_multimodal.py
index a74337710..8f58ea2ca 100644
--- a/scripts/model_examples/openai_chat_multiagent_multimodal.py
+++ b/scripts/model_examples/openai_chat_multiagent_multimodal.py
@@ -16,7 +16,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = OpenAIMultiAgentFormatter()
@@ -77,4 +77,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/openai_chat_multimodal.py b/scripts/model_examples/openai_chat_multimodal.py
index 5dfc3666f..07ec51dc8 100644
--- a/scripts/model_examples/openai_chat_multimodal.py
+++ b/scripts/model_examples/openai_chat_multimodal.py
@@ -23,7 +23,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call gpt-4.1 with an image URL and ask what is in the image."""
model = OpenAIChatModel(
credential=OpenAICredential(
@@ -67,7 +67,7 @@ def _build_model() -> OpenAIChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call gpt-4.1 with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to a base64 data
@@ -99,7 +99,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call gpt-4.1 with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -131,6 +131,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/openai_response_multiagent_multimodal.py b/scripts/model_examples/openai_response_multiagent_multimodal.py
index 914811806..f6b909393 100644
--- a/scripts/model_examples/openai_response_multiagent_multimodal.py
+++ b/scripts/model_examples/openai_response_multiagent_multimodal.py
@@ -16,7 +16,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = OpenAIResponseMultiAgentFormatter()
@@ -77,4 +77,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/openai_response_multimodal.py b/scripts/model_examples/openai_response_multimodal.py
index e9cfe436b..f0a5be805 100644
--- a/scripts/model_examples/openai_response_multimodal.py
+++ b/scripts/model_examples/openai_response_multimodal.py
@@ -24,7 +24,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call gpt-4.1 (Responses API) with an image URL and ask what is in
the image."""
model = OpenAIResponseModel(
@@ -69,7 +69,7 @@ def _build_model() -> OpenAIResponseModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call gpt-4.1 (Responses API) with a local image using a ``file://`` URL.
The formatter reads the file from disk and converts it to a base64 data
@@ -101,7 +101,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call gpt-4.1 (Responses API) with a local image using explicit base64.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -133,6 +133,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/scripts/model_examples/xai_multiagent_multimodal.py b/scripts/model_examples/xai_multiagent_multimodal.py
index 845d0b96f..ae1678e8d 100644
--- a/scripts/model_examples/xai_multiagent_multimodal.py
+++ b/scripts/model_examples/xai_multiagent_multimodal.py
@@ -19,7 +19,7 @@ TEST_IMAGE_URL = (
)
-async def example_multiagent_multimodal() -> None:
+async def example_multiagent_image_url() -> None:
"""Multi-agent conversation where Alice shares an image for the group."""
formatter = XAIMultiAgentFormatter()
@@ -80,4 +80,4 @@ async def example_multiagent_multimodal() -> None:
if __name__ == "__main__":
- asyncio.run(example_multiagent_multimodal())
+ asyncio.run(example_multiagent_image_url())
diff --git a/scripts/model_examples/xai_multimodal.py b/scripts/model_examples/xai_multimodal.py
index 303d4fa52..bee93d0a7 100644
--- a/scripts/model_examples/xai_multimodal.py
+++ b/scripts/model_examples/xai_multimodal.py
@@ -27,7 +27,7 @@ TEST_IMAGE_URL = (
)
-async def example_multimodal() -> None:
+async def example_image_url() -> None:
"""Call grok-4.3 with an image URL and ask what is in the image."""
model = XAIChatModel(
credential=XAICredential(
@@ -71,7 +71,7 @@ def _build_model() -> XAIChatModel:
)
-async def example_local_path() -> None:
+async def example_image_local_path() -> None:
"""Call grok-4.3 with a local image using a ``file://`` URL.
The XAI formatter automatically reads the file from disk and converts
@@ -103,7 +103,7 @@ async def example_local_path() -> None:
await stream_and_collect(await model(msgs))
-async def example_file_url() -> None:
+async def example_image_base64() -> None:
"""Call grok-4.3 with a local image using explicit base64 encoding.
Use ``Base64Source`` when you already have the binary data in memory or
@@ -135,6 +135,6 @@ async def example_file_url() -> None:
if __name__ == "__main__":
- asyncio.run(example_multimodal())
- asyncio.run(example_local_path())
- asyncio.run(example_file_url())
+ asyncio.run(example_image_url())
+ asyncio.run(example_image_local_path())
+ asyncio.run(example_image_base64())
diff --git a/src/agentscope/credential/_dashscope.py b/src/agentscope/credential/_dashscope.py
index bdcc62762..dac93481e 100644
--- a/src/agentscope/credential/_dashscope.py
+++ b/src/agentscope/credential/_dashscope.py
@@ -9,6 +9,8 @@ from ._base import CredentialBase
if TYPE_CHECKING:
from ..model import ChatModelBase
+_DASHSCOPE_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
+
class DashScopeCredential(CredentialBase):
"""The credential for DashScope API."""
@@ -21,10 +23,12 @@ class DashScopeCredential(CredentialBase):
title="API Key",
)
- base_http_api_url: str | None = Field(
- default=None,
+ base_url: str = Field(
+ default=_DASHSCOPE_BASE_URL,
title="API Base URL",
- description="The base URL of the DashScope API.",
+ description=(
+ "The base URL for the DashScope OpenAI-compatible API endpoint."
+ ),
)
@classmethod
diff --git a/src/agentscope/formatter/_dashscope_formatter.py b/src/agentscope/formatter/_dashscope_formatter.py
index 56c9fded0..67b5bf5b9 100644
--- a/src/agentscope/formatter/_dashscope_formatter.py
+++ b/src/agentscope/formatter/_dashscope_formatter.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-"""The dashscope formatter module."""
+"""The DashScope formatter module (OpenAI-compatible format)."""
import base64
from typing import Any
@@ -25,8 +25,8 @@ from ..message import (
class _DashScopeFormatterBase(FormatterBase, ABC):
- """Base class for DashScope formatters, providing shared data block
- formatting logic."""
+ """Base class for DashScope formatters (OpenAI-compatible format),
+ providing shared data block formatting logic."""
input_types: list[str] = Field(
default_factory=lambda: [
@@ -64,17 +64,28 @@ class _DashScopeFormatterBase(FormatterBase, ABC):
def _format_dashscope_data_block(
self,
block: DataBlock,
+ role: str = "user",
) -> dict[str, Any] | None:
- """Format a DataBlock into the required format for DashScope API.
+ """Format a DataBlock into the OpenAI-compatible format for
+ DashScope API.
+
+ Supports:
+ - Images: ``{"type": "image_url", "image_url": {"url": ...}}``
+ - Videos: ``{"type": "video_url", "video_url": {"url": ...}}``
+ - Audio: ``{"type": "input_audio", "input_audio": {...}}``
Args:
block (`DataBlock`):
The DataBlock to format.
+ role (`str`, defaults to ``"user"``):
+ The role of the message containing this block. Audio blocks
+ in assistant messages are skipped to avoid errors in
+ subsequent model calls.
Returns:
`dict[str, Any] | None`:
- A dictionary representing the formatted DataBlock for
- DashScope API.
+ A dictionary representing the formatted DataBlock, or ``None``
+ if the media type is unsupported.
"""
if not any(
fnmatch(block.source.media_type, pattern)
@@ -90,56 +101,142 @@ class _DashScopeFormatterBase(FormatterBase, ABC):
main_type = block.source.media_type.split("/")[0]
- if isinstance(block.source, URLSource):
- url_str = str(block.source.url)
+ if main_type == "image":
+ return self._format_image_source(block.source)
+
+ if main_type == "video":
+ return self._format_video_source(block.source)
+
+ if main_type == "audio":
+ if role == "assistant":
+ return None
+ return self._format_audio_source(block.source)
+
+ logger.warning(
+ "Unsupported main media type %s for DashScope API. "
+ "This block will be skipped.",
+ main_type,
+ )
+ return None
+
+ @staticmethod
+ def _format_image_source(
+ source: URLSource | Base64Source,
+ ) -> dict[str, Any]:
+ """Convert an image source to OpenAI-compatible ``image_url`` format.
+
+ Local ``file://`` URLs are read from disk and converted to base64
+ data URIs. Remote URLs are passed through unchanged.
+ """
+ if isinstance(source, Base64Source):
+ url = f"data:{source.media_type};base64,{source.data}"
+ elif isinstance(source, URLSource):
+ url_str = str(source.url)
if url_str.startswith("file://"):
- # Local file — read and encode as base64 data URI
local_path = url_str.removeprefix("file://")
with open(local_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
- return {
- main_type: f"data:{block.source.media_type};"
- f"base64,{encoded}",
- }
- return {main_type: url_str}
+ url = f"data:{source.media_type};base64,{encoded}"
+ else:
+ url = url_str
+ else:
+ raise ValueError(f"Unsupported image source type: {type(source)}")
- if isinstance(block.source, Base64Source):
- return {
- main_type: f"data:{block.source.media_type};"
- f"base64,{block.source.data}",
- }
+ return {
+ "type": "image_url",
+ "image_url": {"url": url},
+ }
- return None
+ @staticmethod
+ def _format_video_source(
+ source: URLSource | Base64Source,
+ ) -> dict[str, Any]:
+ """Convert a video source to DashScope's ``video_url`` format
+ (OpenAI-compatible extension).
+ Local ``file://`` URLs are read from disk and converted to base64
+ data URIs. Remote URLs are passed through unchanged.
+ """
+ if isinstance(source, Base64Source):
+ url = f"data:{source.media_type};base64,{source.data}"
+ elif isinstance(source, URLSource):
+ url_str = str(source.url)
+ if url_str.startswith("file://"):
+ local_path = url_str.removeprefix("file://")
+ with open(local_path, "rb") as f:
+ encoded = base64.b64encode(f.read()).decode("utf-8")
+ url = f"data:{source.media_type};base64,{encoded}"
+ else:
+ url = url_str
+ else:
+ raise ValueError(f"Unsupported video source type: {type(source)}")
-class DashScopeChatFormatter(_DashScopeFormatterBase):
- """The DashScope formatter class for chatbot scenario, where only a user
- and an agent are involved. We use the `role` field to identify different
- entities in the conversation.
+ return {
+ "type": "video_url",
+ "video_url": {"url": url},
+ }
- .. warning::
- Known Issues with DashScope API:
+ @staticmethod
+ def _format_audio_source(
+ source: URLSource | Base64Source,
+ ) -> dict[str, Any]:
+ """Convert an audio source to DashScope ``input_audio`` format.
- 1. **Missing content field**: When messages lack the 'content' field,
- qwen-vl-max models will raise ``KeyError: 'content'``.
+ DashScope's compatible API accepts URLs directly in the ``data``
+ field (unlike standard OpenAI which requires base64). Local files
+ are still read and base64-encoded.
+ """
+ if isinstance(source, Base64Source):
+ fmt = source.media_type.split("/")[-1]
+ return {
+ "type": "input_audio",
+ "input_audio": {
+ "data": source.data,
+ "format": fmt,
+ },
+ }
- 2. **None content value**: When content is ``None``, qwen-vl-max models
- will raise ``TypeError: 'NoneType' object is not iterable``.
+ if isinstance(source, URLSource):
+ url_str = str(source.url)
+ fmt = source.media_type.split("/")[-1]
+ if url_str.startswith("file://"):
+ local_path = url_str.removeprefix("file://")
+ with open(local_path, "rb") as f:
+ data = base64.b64encode(f.read()).decode("utf-8")
+ return {
+ "type": "input_audio",
+ "input_audio": {
+ "data": data,
+ "format": fmt,
+ },
+ }
+ else:
+ return {
+ "type": "input_audio",
+ "input_audio": {
+ "data": url_str,
+ "format": fmt,
+ },
+ }
- 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": ""``.
+ raise ValueError(f"Unsupported audio source type: {type(source)}")
+
+
+class DashScopeChatFormatter(_DashScopeFormatterBase):
+ """The DashScope formatter class for chatbot scenario (OpenAI-compatible
+ format), where only a user and an agent are involved. We use the ``role``
+ field to identify different entities in the conversation.
- To avoid these issues, this formatter assigns content as an empty
- list ``[]`` for messages without valid content blocks.
+ This formatter outputs messages in the OpenAI Chat Completions format,
+ with DashScope-specific extensions for video (``video_url``) and
+ thinking (``reasoning_content``).
"""
async def format(
self,
msgs: list[Msg],
) -> list[dict[str, Any]]:
- """Format message objects into DashScope API format.
+ """Format message objects into DashScope OpenAI-compatible format.
Args:
msgs (`list[Msg]`):
@@ -161,29 +258,25 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
for block in msg.get_content_blocks():
if isinstance(block, TextBlock):
- content_blocks.append({"text": block.text})
+ content_blocks.append({"type": "text", "text": block.text})
elif isinstance(block, DataBlock):
- formatted_block = self._format_dashscope_data_block(block)
+ formatted_block = self._format_dashscope_data_block(
+ block,
+ role=msg.role,
+ )
if formatted_block:
content_blocks.append(formatted_block)
elif isinstance(block, HintBlock):
- # Insert a new user message with the hint content right
- # after the current message, and go on processing the
- # rest of the blocks in the current message
if content_blocks or tool_calls:
- formatted_msgs.append(
- {
- "role": "user",
- "content": content_blocks,
- "tool_calls": tool_calls
- if tool_calls
- else None,
- },
- )
- # Refresh content_blocks and tool_calls for the last
- # blocks
+ msg_openai: dict[str, Any] = {
+ "role": msg.role,
+ "content": content_blocks or None,
+ }
+ if tool_calls:
+ msg_openai["tool_calls"] = tool_calls
+ formatted_msgs.append(msg_openai)
content_blocks = []
tool_calls = []
@@ -209,7 +302,6 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
multimodal_data,
) = self.convert_tool_result_to_string(block.output)
- # First add the tool result message in DashScope API format
formatted_msgs.append(
{
"role": "tool",
@@ -219,8 +311,6 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
},
)
- # If we have multimodal data that needs to be prompted to
- # the out-tool-result message
if multimodal_data:
msgs.insert(
i + 1,
@@ -236,9 +326,9 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
type(block),
)
- msg_dashscope = {
+ msg_dashscope: dict[str, Any] = {
"role": msg.role,
- "content": content_blocks,
+ "content": content_blocks or None,
}
if tool_calls:
@@ -250,50 +340,26 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
if msg_dashscope["content"] or msg_dashscope.get("tool_calls"):
formatted_msgs.append(msg_dashscope)
- # Move to next message
i += 1
- # Merge adjacent text blocks into one to avoid API issues.
- # Tool-result messages have content as a plain string (not a list),
- # so skip merging for those.
- cleaned_msgs: list = []
- for formatted_msg in formatted_msgs:
- if not isinstance(formatted_msg.get("content"), list):
- cleaned_msgs.append(formatted_msg)
- continue
- new_content = []
- for block in formatted_msg["content"]:
- if (
- block.get("text")
- and new_content
- and new_content[-1].get("text")
- ):
- new_content[-1]["text"] += "\n" + block["text"]
- else:
- new_content.append(block)
- formatted_msg["content"] = new_content
- cleaned_msgs.append(formatted_msg)
-
- return cleaned_msgs
+ return formatted_msgs
class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
- """DashScope formatter for multi-agent conversations, where more than
- a user and an agent are involved.
+ """DashScope formatter for multi-agent conversations (OpenAI-compatible
+ format), where more than a user and an agent are involved.
.. note:: This formatter will combine previous messages (except tool
calls/results) into a history section in the first system message with
the conversation history prompt.
.. note:: For tool calls/results, they will be presented as separate
- messages as required by the DashScope API. Therefore, the tool calls/
- results messages are expected to be placed at the end of the input
- messages.
+ messages as required by the API. Therefore, the tool calls/results
+ messages are expected to be placed at the end of the input messages.
.. tip:: Telling the assistant's name in the system prompt is very
important in multi-agent conversations. So that LLM can know who it
is playing as.
-
"""
conversation_history_prompt: str = Field(
@@ -307,7 +373,7 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
async def format(self, msgs: list[Msg]) -> list[dict]:
"""Format input messages into the structure required by the DashScope
- API.
+ OpenAI-compatible API.
To support multi-agent conversations, this formatter processes messages
as follows:
@@ -315,8 +381,8 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
- Prepends an instruction before the first conversation history
section.
- Combines conversation turns into a history section, where each entry
- is formatted as `{name}: {content}`.
- - Wraps the conversation history with `<history>` and `</history>`
+ is formatted as ``{name}: {content}``.
+ - Wraps the conversation history with ``<history>`` and ``</history>``
tags.
Returns:
@@ -355,16 +421,7 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
msgs: list[Msg],
) -> list[dict[str, Any]]:
"""Given a sequence of tool call/result messages, format them into
- the required format for the DashScope API.
-
- Args:
- msgs (`list[Msg]`):
- The list of messages containing tool calls/results to format.
-
- Returns:
- `list[dict[str, Any]]`:
- A list of dictionaries formatted for the DashScope API.
- """
+ the required format for the DashScope API."""
return await DashScopeChatFormatter(
input_types=self.input_types,
).format(msgs)
@@ -375,50 +432,34 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
is_first: bool = True,
) -> list[dict[str, Any]]:
"""Given a sequence of messages without tool calls/results, format
- them into a user message with conversation history tags. For the
- first agent message, it will include the conversation history prompt.
-
- Args:
- msgs (`list[Msg]`):
- A list of Msg objects to be formatted.
- is_first (`bool`, defaults to `True`):
- Whether this is the first agent message in the conversation.
- If `True`, the conversation history prompt will be included.
-
- Returns:
- `list[dict[str, Any]]`:
- A list of dictionaries formatted for the DashScope API.
- """
+ them into a user message with conversation history tags."""
if is_first:
conversation_history_prompt = self.conversation_history_prompt
else:
conversation_history_prompt = ""
- # Format into required DashScope format
formatted_msgs: list[dict] = []
-
- # Collect the multimodal files
- conversation_blocks = []
+ conversation_blocks: list = []
accumulated_text = []
+ media_blocks: list[dict] = []
+
for msg in msgs:
for block in msg.get_content_blocks():
if isinstance(block, TextBlock):
accumulated_text.append(f"{msg.name}: {block.text}")
elif isinstance(block, DataBlock):
- # Handle the accumulated text as a single block
- if accumulated_text:
- conversation_blocks.append(
- {"text": "\n".join(accumulated_text)},
- )
- accumulated_text.clear()
-
- formatted_block = self._format_dashscope_data_block(block)
+ formatted_block = self._format_dashscope_data_block(
+ block,
+ role=msg.role,
+ )
if formatted_block is not None:
- conversation_blocks.append(formatted_block)
+ media_blocks.append(formatted_block)
if accumulated_text:
- conversation_blocks.append({"text": "\n".join(accumulated_text)})
+ conversation_blocks.append(
+ {"text": "\n".join(accumulated_text)},
+ )
if conversation_blocks:
if conversation_blocks[0].get("text"):
@@ -427,36 +468,30 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
+ "<history>\n"
+ conversation_blocks[0]["text"]
)
-
else:
conversation_blocks.insert(
0,
- {
- "text": conversation_history_prompt + "<history>\n",
- },
+ {"text": conversation_history_prompt + "<history>\n"},
)
- conversation_blocks.append({"text": "</history>"})
-
- # Merge the adjacent text blocks into one text blocks to avoid API
- # issues
- new_content = []
- for block in conversation_blocks:
- if (
- block.get("text")
- and new_content
- and new_content[-1].get("text")
- ):
- new_content[-1]["text"] += "\n" + block["text"]
- else:
- new_content.append(block)
+ if conversation_blocks[-1].get("text"):
+ conversation_blocks[-1]["text"] += "\n</history>"
+ else:
+ conversation_blocks.append({"text": "</history>"})
- formatted_msgs.append(
- {
- "role": "user",
- "content": new_content,
- },
+ conversation_blocks_text = "\n".join(
+ b.get("text", "") for b in conversation_blocks
+ )
+
+ content_list: list[dict[str, Any]] = []
+ if conversation_blocks_text:
+ content_list.append(
+ {"type": "text", "text": conversation_blocks_text},
)
+ content_list.extend(media_blocks)
+
+ if content_list:
+ formatted_msgs.append({"role": "user", "content": content_list})
return formatted_msgs
diff --git a/src/agentscope/model/_dashscope/_model.py b/src/agentscope/model/_dashscope/_model.py
index 6705f1f31..5ffdfe520 100644
--- a/src/agentscope/model/_dashscope/_model.py
+++ b/src/agentscope/model/_dashscope/_model.py
@@ -1,36 +1,44 @@
# -*- coding: utf-8 -*-
-"""The DashScope chat model class."""
-import uuid
+"""The DashScope chat model class (OpenAI-compatible implementation)."""
import warnings
+from collections import OrderedDict
from datetime import datetime
-from http import HTTPStatus
-from typing import Any, AsyncGenerator, Generator, List, Literal, TYPE_CHECKING
+from typing import Any, AsyncGenerator, List, Literal, TYPE_CHECKING
from pydantic import BaseModel, Field
-from aioitertools import iter as giter
from .._base import ChatModelBase, _TOOL_CHOICE_LITERAL_MODES
from .._model_response import ChatResponse
from .._model_usage import ChatUsage
from ...credential import DashScopeCredential
from ...formatter import FormatterBase, DashScopeChatFormatter
-from ...message import Msg, TextBlock, ThinkingBlock, ToolCallBlock
+from ...message import (
+ Msg,
+ TextBlock,
+ ThinkingBlock,
+ ToolCallBlock,
+ DataBlock,
+ Base64Source,
+)
from ...tool import ToolChoice
from ...tracing import trace_llm
-
if TYPE_CHECKING:
- from dashscope.api_entities.dashscope_response import GenerationResponse
- from dashscope.api_entities.dashscope_response import (
- MultiModalConversationResponse,
- )
+ from openai.types.chat import ChatCompletion
+ from openai import AsyncStream
else:
- GenerationResponse = Any
- MultiModalConversationResponse = Any
+ ChatCompletion = Any
+ AsyncStream = Any
class DashScopeChatModel(ChatModelBase):
- """The DashScope chat model."""
+ """The DashScope chat model (OpenAI-compatible implementation).
+
+ This implementation uses the OpenAI Python SDK to call DashScope's
+ OpenAI-compatible endpoint (``compatible-mode/v1``), which supports
+ both text-only and multimodal (image/video) inputs through the same
+ unified API.
+ """
class Parameters(BaseModel):
"""The parameters for DashScope LLM API."""
@@ -96,7 +104,6 @@ class DashScopeChatModel(ChatModelBase):
stream: bool = True,
max_retries: int = 3,
context_size: int = 131072,
- multimodality: bool | None = None,
formatter: FormatterBase | None = None,
) -> None:
"""Initialize the DashScope chat model.
@@ -116,11 +123,6 @@ class DashScopeChatModel(ChatModelBase):
The maximum number of retries for the DashScope API.
context_size (`int`, defaults to `131072`):
The model context size used for context compression.
- multimodality (`bool | None`, defaults to `None`):
- Whether to call the MultiModalConversation API. ``True``
- forces multimodal mode, ``False`` forces text-only mode,
- and ``None`` auto-detects from the model name
- (e.g. ``qvq`` or ``-vl`` suffix).
formatter (`FormatterBase | None`, defaults to `None`):
The formatter that converts ``Msg`` objects to the format
required by the DashScope API. When ``None``, a
@@ -134,7 +136,6 @@ class DashScopeChatModel(ChatModelBase):
)
self.credential = credential
self.parameters = parameters or self.Parameters()
- self.multimodality = multimodality
self.formatter = formatter or DashScopeChatFormatter()
@trace_llm
@@ -146,367 +147,305 @@ class DashScopeChatModel(ChatModelBase):
tool_choice: ToolChoice | None = None,
**kwargs: Any,
) -> ChatResponse | AsyncGenerator[ChatResponse, None]:
- """Get the response from the dashscope
- Generation/MultimodalConversation API by the given arguments.
-
- .. note:: We unify the DashScope generation and multimodal conversation
- APIs into one method, since they support similar arguments and share
- the same functionality.
+ """Call the DashScope chat completions API via OpenAI-compatible
+ endpoint.
Args:
- messages (`list[Msg]`):
- The Msg object that will be formatted and sent to the LLM API
- as the conversation messages.
+ model_name (`str`):
+ The model name to use for this call.
+ messages (`list`):
+ The Msg objects that will be formatted and sent to the API.
tools (`list[dict] | None`, default `None`):
The tools JSON schemas that the model can use.
tool_choice (`ToolChoice | None`, default `None`):
Controls which (if any) tool is called by the model.
- Note: DashScope API only supports "auto" and "none", so
- "required" will be converted to "auto".
- For more details, please refer to
- https://help.aliyun.com/zh/model-studio/qwen-function-calling
**kwargs (`Any`):
The keyword arguments for DashScope chat completions API,
- e.g. `temperature`, `max_tokens`, `top_p`, etc. Please
- refer to `DashScope documentation
- <https://help.aliyun.com/zh/dashscope/developer-reference/api-details>`_
- for more detailed arguments.
+ e.g. ``temperature``, ``max_tokens``, ``top_p``, etc.
"""
- import dashscope
+ import openai
+
+ client = openai.AsyncClient(
+ api_key=self.credential.api_key.get_secret_value(),
+ base_url=self.credential.base_url,
+ )
- # 1. Format the messages to the format required by DashScope API
- formatted_msg = await self.formatter.format(messages)
+ formatted_messages = await self.formatter.format(messages)
- # 2. Prepare the generation keyword arguments
- kwargs = {
- "messages": formatted_msg,
+ request_kwargs: dict[str, Any] = {
"model": model_name,
+ "messages": formatted_messages,
"stream": self.stream,
- "result_format": "message",
- # In agentscope, the `incremental_output` must be `True` when
- # `self.stream` is True
- "incremental_output": self.stream,
- **kwargs,
}
+ if self.parameters.max_tokens is not None:
+ request_kwargs["max_tokens"] = self.parameters.max_tokens
+
+ if self.parameters.temperature is not None:
+ request_kwargs["temperature"] = self.parameters.temperature
+
+ if self.parameters.top_p is not None:
+ request_kwargs["top_p"] = self.parameters.top_p
+
+ request_kwargs.update(kwargs)
+
fmt_tools, fmt_tool_choice = self._format_tools(tools, tool_choice)
if fmt_tools is not None:
- kwargs["tools"] = fmt_tools
+ request_kwargs["tools"] = fmt_tools
+ if not self.parameters.parallel_tool_calls:
+ request_kwargs["parallel_tool_calls"] = False
if fmt_tool_choice is not None:
- kwargs["tool_choice"] = fmt_tool_choice
+ request_kwargs["tool_choice"] = fmt_tool_choice
- # thinking related options — map to DashScope API parameter names
- kwargs["enable_thinking"] = self.parameters.thinking_enable
+ extra_body: dict[str, Any] = {}
+ if self.parameters.thinking_enable is not None:
+ extra_body["enable_thinking"] = self.parameters.thinking_enable
if self.parameters.thinking_budget is not None:
- kwargs["thinking_budget"] = self.parameters.thinking_budget
+ extra_body["thinking_budget"] = self.parameters.thinking_budget
+ if self.parameters.top_k is not None:
+ extra_body["top_k"] = self.parameters.top_k
- # 3. Call the API and parse the response
- start_datetime = datetime.now()
- # Use MultiModalConversation API if multimodality is True, or if the
- # model is multimodal based on the model name.
- api_key = self.credential.api_key.get_secret_value()
- if self.multimodality or (
- self.multimodality is None
- and (
- "qvq" in model_name
- or "-vl" in model_name
- or "-omni" in model_name
- )
- ):
- response = await dashscope.AioMultiModalConversation.call(
- api_key=api_key,
- **kwargs,
- )
+ if extra_body:
+ request_kwargs.setdefault("extra_body", {})
+ request_kwargs["extra_body"].update(extra_body)
- else:
- response = await dashscope.aigc.generation.AioGeneration.call(
- api_key=api_key,
- **kwargs,
- )
+ if self.stream:
+ request_kwargs["stream_options"] = {"include_usage": True}
+
+ start_datetime = datetime.now()
+ response = await client.chat.completions.create(**request_kwargs)
if self.stream:
- return self._parse_dashscope_stream_response(
+ audio_cfg = request_kwargs.get("audio")
+ audio_fmt = (
+ audio_cfg.get("format", "wav")
+ if isinstance(audio_cfg, dict)
+ else "wav"
+ )
+ return self._parse_stream_response(
start_datetime,
response,
+ audio_fmt,
)
- parsed_response = await self._parse_dashscope_generation_response(
- start_datetime,
- response,
- )
-
- return parsed_response
+ return self._parse_completion_response(start_datetime, response)
- async def _parse_dashscope_stream_response(
+ async def _parse_stream_response(
self,
start_datetime: datetime,
- response: AsyncGenerator[GenerationResponse, None]
- | AsyncGenerator[MultiModalConversationResponse, None]
- | Generator[MultiModalConversationResponse, None, None],
- ) -> AsyncGenerator[ChatResponse, Any]:
- """Given a DashScope streaming response generator, extract the content
- blocks and usages from it and yield ChatResponse objects.
+ response: AsyncStream,
+ audio_format: str = "wav",
+ ) -> AsyncGenerator[ChatResponse, None]:
+ """Parse the DashScope streaming response (OpenAI-compatible format).
Args:
start_datetime (`datetime`):
The start datetime of the response generation.
- response (
- `AsyncGenerator[GenerationResponse, None] | \
- AsyncGenerator[MultiModalConversationResponse, None] | \
- Generator[MultiModalConversationResponse, None, None]`
- ):
- DashScope streaming response (async) generator
- (GenerationResponse or MultiModalConversationResponse).
+ response (`AsyncStream`):
+ The OpenAI-compatible async stream object.
+ audio_format (`str`, defaults to ``"wav"``):
+ The audio format requested (used to set the media type on
+ the output ``DataBlock``).
- Returns:
- `AsyncGenerator[ChatResponse, Any]`:
- An async generator that yields ChatResponse objects containing
- the content blocks and usage information for each chunk in the
- streaming response.
+ Yields:
+ `ChatResponse`:
+ Incremental ``ChatResponse`` objects with ``is_last=False``
+ followed by a final one with ``is_last=True``.
"""
- # All delta should have the same block identifier
- acc_text = TextBlock(text="")
- acc_thinking = ThinkingBlock(thinking="")
- acc_tool_calls = {}
usage = None
response_id: str | None = None
+ acc_text = TextBlock(text="")
+ acc_thinking = ThinkingBlock(thinking="")
+ acc_tool_calls: OrderedDict = OrderedDict()
+ acc_audio_data: str = ""
+
+ async with response as stream:
+ async for chunk in stream:
+ if chunk.usage:
+ u = chunk.usage
+ ptd = getattr(u, "prompt_tokens_details", None)
+ if ptd and hasattr(ptd, "cached_tokens"):
+ cache_read = ptd.cached_tokens or 0
+ else:
+ cache_read = 0
+ usage = ChatUsage(
+ input_tokens=u.prompt_tokens or 0,
+ output_tokens=u.completion_tokens or 0,
+ time=(datetime.now() - start_datetime).total_seconds(),
+ cache_input_tokens=cache_read,
+ )
- async for chunk in giter(response):
- # The yield delta content blocks in the current chunk
- delta_content: list = []
-
- if chunk.status_code != HTTPStatus.OK:
- raise RuntimeError(
- f"Failed to get response from DashScope API: {chunk}",
- )
-
- # Capture response_id from the first chunk if not already set
- response_id = response_id or chunk.request_id
+ response_id = response_id or getattr(chunk, "id", None)
- # The message field in the chunk
- message = chunk.output.choices[0].message
+ if not chunk.choices:
+ continue
- # Update reasoning content
- if isinstance(message.get("reasoning_content"), str):
- acc_thinking.thinking += message["reasoning_content"]
- delta_content.append(
- ThinkingBlock(
- id=acc_thinking.id,
- thinking=message["reasoning_content"],
- ),
- )
+ choice = chunk.choices[0]
+ delta = choice.delta
- # Update text content
- if isinstance(message.content, str):
- acc_text.text += message.content
- delta_content.append(
- TextBlock(
- id=acc_text.id,
- text=message.content,
- ),
- )
- elif isinstance(message.content, list):
- delta_text = "".join(
- [
- _["text"]
- for _ in message.content
- if isinstance(_, dict)
- and isinstance(_.get("text"), str)
- ],
+ delta_thinking = (
+ getattr(delta, "reasoning_content", None) or ""
)
+ delta_text = getattr(delta, "content", None) or ""
+
+ # Collect audio output from Omni models (delta.audio.data)
+ delta_audio = getattr(delta, "audio", None)
+ if delta_audio is not None:
+ if isinstance(delta_audio, dict):
+ audio_chunk = delta_audio.get("data", "")
+ else:
+ audio_chunk = getattr(delta_audio, "data", "") or ""
+ if audio_chunk:
+ acc_audio_data += audio_chunk
+
+ acc_thinking.thinking += delta_thinking
acc_text.text += delta_text
- delta_content.append(
- ThinkingBlock(id=acc_text.id, thinking=delta_text),
- )
- # Update tool calls
- for tool_call in message.get("tool_calls") or []:
- # Must be a dictionary
- if not isinstance(tool_call, dict):
- continue
-
- # Use the index to identify different tool calls
- index = tool_call.get("index", 0)
-
- # Initialize accumulator for a new tool call index
- acc_tool_calls.setdefault(index, {})
-
- # Avoid appending duplicate id
- if "id" in tool_call and tool_call["id"] != acc_tool_calls[
- index
- ].get("id"):
- acc_tool_calls[index]["id"] = tool_call["id"]
-
- # Arguments
- if isinstance(tool_call.get("function"), dict):
- func = tool_call["function"]
-
- # Function name
- if "name" in func:
- acc_tool_calls[index]["name"] = func["name"]
-
- # Function input arguments
- if isinstance(func.get("arguments"), str):
- acc_tool_calls[index]["arguments"] = (
- acc_tool_calls[index].get("arguments", "")
- + func["arguments"]
- )
-
- # Append the tool call block for this chunk
- delta_content.append(
- ToolCallBlock(
- id=acc_tool_calls[index].get(
- "id",
- uuid.uuid4().hex,
+ for tool_call in getattr(delta, "tool_calls", None) or []:
+ idx = tool_call.index
+ if idx in acc_tool_calls:
+ if tool_call.function and tool_call.function.arguments:
+ acc_tool_calls[idx][
+ "input"
+ ] += tool_call.function.arguments
+ else:
+ acc_tool_calls[idx] = {
+ "id": tool_call.id or "",
+ "name": (
+ tool_call.function.name
+ if tool_call.function
+ else ""
),
- name=func.get("name", ""),
- input=func.get("arguments", ""),
+ "input": (
+ tool_call.function.arguments
+ if tool_call.function
+ else ""
+ )
+ or "",
+ }
+
+ delta_contents: List[
+ TextBlock | ToolCallBlock | ThinkingBlock
+ ] = []
+ if delta_thinking:
+ delta_contents.append(
+ ThinkingBlock(
+ id=acc_thinking.id,
+ thinking=delta_thinking,
),
)
+ if delta_text:
+ delta_contents.append(
+ TextBlock(id=acc_text.id, text=delta_text),
+ )
- # The chunk usage
- if chunk.usage:
- u = chunk.usage
- ptd = u.get("prompt_tokens_details", None)
- if isinstance(ptd, dict):
- cache_creation = ptd.get("cache_creation_input_tokens", 0)
- cache_read = ptd.get("cached_tokens", 0)
- else:
- cache_creation = 0
- cache_read = u.get("cached_tokens", 0)
- usage = ChatUsage(
- input_tokens=u.input_tokens,
- output_tokens=u.output_tokens,
- time=(datetime.now() - start_datetime).total_seconds(),
- cache_creation_input_tokens=cache_creation,
- cache_input_tokens=cache_read,
- )
-
- # Yield the chat response
- yield ChatResponse(
- id=response_id,
- content=delta_content,
- is_last=False,
- usage=usage,
- )
-
- # Yield a final complete response with the accumulated content and
- # final usage. Thinking comes before text to preserve the natural
- # generation order (reasoning first, then answer).
- content = []
+ if delta_contents:
+ _kwargs: dict[str, Any] = {
+ "content": delta_contents,
+ "usage": usage,
+ "is_last": False,
+ }
+ if response_id:
+ _kwargs["id"] = response_id
+ yield ChatResponse(**_kwargs)
+
+ final_contents: List[
+ TextBlock | ToolCallBlock | ThinkingBlock | DataBlock
+ ] = []
if acc_thinking.thinking:
- content.append(acc_thinking)
-
+ final_contents.append(acc_thinking)
if acc_text.text:
- content.append(acc_text)
-
- if acc_tool_calls:
- content.extend(
+ final_contents.append(acc_text)
+ for tc in acc_tool_calls.values():
+ final_contents.append(
ToolCallBlock(
- id=tc.get("id", uuid.uuid4().hex),
- name=tc.get("name", ""),
- input=tc.get("arguments", "{}"),
- )
- for tc in acc_tool_calls.values()
+ id=tc["id"],
+ name=tc["name"],
+ input=tc["input"],
+ ),
+ )
+ if acc_audio_data:
+ final_contents.append(
+ DataBlock(
+ source=Base64Source(
+ data=acc_audio_data,
+ media_type=f"audio/{audio_format}",
+ ),
+ ),
)
- yield ChatResponse(
- id=response_id or uuid.uuid4().hex,
- content=content,
- is_last=True,
- usage=usage,
- )
+ _final_kwargs: dict[str, Any] = {
+ "content": final_contents,
+ "usage": usage,
+ "is_last": True,
+ }
+ if response_id:
+ _final_kwargs["id"] = response_id
+ yield ChatResponse(**_final_kwargs)
- async def _parse_dashscope_generation_response(
+ def _parse_completion_response(
self,
start_datetime: datetime,
- response: GenerationResponse | MultiModalConversationResponse,
+ response: ChatCompletion,
) -> ChatResponse:
- """Given a DashScope GenerationResponse object, extract the content
- blocks and usages from it.
+ """Parse the DashScope non-streaming response (OpenAI-compatible
+ format).
Args:
start_datetime (`datetime`):
The start datetime of the response generation.
- response (
- `GenerationResponse | MultiModalConversationResponse`
- ):
- Dashscope GenerationResponse | MultiModalConversationResponse
- object to parse.
+ response (`ChatCompletion`):
+ The OpenAI-compatible chat completion object.
Returns:
`ChatResponse`:
- A ChatResponse object containing the content blocks and usage.
+ A single ``ChatResponse`` with ``is_last=True``.
"""
- # Collect the content blocks from the response.
- if response.status_code != HTTPStatus.OK:
- raise RuntimeError(
- f"Failed to get response from DashScope API: {response}",
- )
-
- content_blocks: List[TextBlock | ToolCallBlock] = []
+ content_blocks: List[TextBlock | ToolCallBlock | ThinkingBlock] = []
- message = response.output.choices[0].message
+ if response.choices:
+ choice = response.choices[0]
+ reasoning = getattr(choice.message, "reasoning_content", None)
+ if isinstance(reasoning, str) and reasoning:
+ content_blocks.append(ThinkingBlock(thinking=reasoning))
- # The content and tool calls in the message
- content, tool_calls = message.get("content"), message.get("tool_calls")
-
- if isinstance(content, str):
- content_blocks.append(TextBlock(text=content))
-
- elif isinstance(content, list):
- content_blocks.append(
- TextBlock(
- text="".join(
- [
- _["text"]
- for _ in content
- if isinstance(_, dict)
- and isinstance(_.get("text"), str)
- ],
- ),
- ),
- )
-
- if isinstance(tool_calls, list):
- for tool_call in tool_calls:
- function = tool_call.get("function") or {}
- name = function.get("name") or ""
- input_ = function.get("arguments") or "{}"
+ if choice.message.content:
+ content_blocks.append(TextBlock(text=choice.message.content))
+ for tool_call in choice.message.tool_calls or []:
content_blocks.append(
ToolCallBlock(
- id=tool_call.get("id", uuid.uuid4().hex),
- name=name,
- input=input_,
+ id=tool_call.id,
+ name=tool_call.function.name,
+ input=tool_call.function.arguments,
),
)
- # Usage information
usage = None
if response.usage:
u = response.usage
- ptd = u.get("prompt_tokens_details", None)
- if isinstance(ptd, dict):
- cache_creation = ptd.get("cache_creation_input_tokens", 0)
- cache_read = ptd.get("cached_tokens", 0)
+ ptd = getattr(u, "prompt_tokens_details", None)
+ if ptd and hasattr(ptd, "cached_tokens"):
+ cache_read = ptd.cached_tokens or 0
else:
- cache_creation = 0
- cache_read = u.get("cached_tokens", 0)
+ cache_read = 0
usage = ChatUsage(
- input_tokens=u.input_tokens,
- output_tokens=u.output_tokens,
+ input_tokens=u.prompt_tokens,
+ output_tokens=u.completion_tokens,
time=(datetime.now() - start_datetime).total_seconds(),
- cache_creation_input_tokens=cache_creation,
cache_input_tokens=cache_read,
)
- return ChatResponse(
- id=getattr(response, "request_id", uuid.uuid4().hex),
- content=content_blocks,
- is_last=True,
- usage=usage,
- )
+ resp_kwargs: dict[str, Any] = {
+ "content": content_blocks,
+ "is_last": True,
+ "usage": usage,
+ }
+ response_id = getattr(response, "id", None)
+ if response_id:
+ resp_kwargs["id"] = response_id
+
+ return ChatResponse(**resp_kwargs)
def _format_tools(
self,
@@ -515,12 +454,11 @@ class DashScopeChatModel(ChatModelBase):
) -> tuple[list[dict] | None, str | dict | None]:
"""Validate and format tools and tool_choice for DashScope.
- DashScope only supports "auto" and "none" modes; "required" is
- converted to "auto" with a warning. When ``tool_choice.tools``
- is specified the schemas list is filtered to only those tools.
- When ``tool_choice.mode`` is a specific tool name (str) the
- model is forced to call exactly that tool without needing to
- filter the list, preserving prompt-cache efficiency.
+ DashScope supports "auto", "none", and "required" modes in
+ OpenAI-compatible format. When ``tool_choice.tools`` is specified
+ the schemas list is filtered to only those tools. When
+ ``tool_choice.mode`` is a specific tool name (str) the model is
+ forced to call exactly that tool.
Args:
tools (`list[dict] | None`, optional):
@@ -559,7 +497,6 @@ class DashScopeChatModel(ChatModelBase):
mode = tool_choice.mode
if mode not in _TOOL_CHOICE_LITERAL_MODES:
- # mode is a specific tool name — force call it
return fmt_tools, {
"type": "function",
"function": {"name": mode},
@@ -567,9 +504,10 @@ class DashScopeChatModel(ChatModelBase):
if mode == "required":
warnings.warn(
- f"'{mode}' is not supported by DashScope API. "
+ f"'{mode}' is not fully supported by DashScope API. "
"It will be converted to 'auto'.",
DeprecationWarning,
+ stacklevel=2,
)
return fmt_tools, "auto"
diff --git a/tests/formatter_dashscope_test.py b/tests/formatter_dashscope_test.py
index 31235f139..870b79847 100644
--- a/tests/formatter_dashscope_test.py
+++ b/tests/formatter_dashscope_test.py
@@ -46,7 +46,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
media_type="audio/mpeg",
)
self.image_url = str(_img_src.url)
- audio_url = str(_aud_src.url)
+ self.audio_url = str(_aud_src.url)
# --- Base64 fixtures ---
self.image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
@@ -94,7 +94,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
),
DataBlock(
source=URLSource(
- url=audio_url,
+ url=self.audio_url,
media_type="audio/mpeg",
),
),
@@ -152,44 +152,78 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
]
# ---------------------------------------------------------------
- # Ground truth: DashScopeChatFormatter
- # - System/user/assistant content is always a list of dicts.
+ # Ground truth: DashScopeChatFormatter (OpenAI-compatible format)
+ # - Content blocks use {"type": "text", "text": ...} format.
+ # - Images use {"type": "image_url", "image_url": {"url": ...}}.
+ # - Audio uses {"type": "input_audio", "input_audio": {...}}.
# - Tool-result content is a plain string.
# ---------------------------------------------------------------
self.gt_chat = [
{
"role": "system",
- "content": [{"text": "You're a helpful assistant."}],
+ "content": [
+ {"type": "text", "text": "You're a helpful assistant."},
+ ],
},
{
"role": "user",
"content": [
- {"text": "What is the capital of France?"},
- {"image": self.image_url},
+ {
+ "type": "text",
+ "text": "What is the capital of France?",
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_url},
+ },
],
},
{
"role": "assistant",
- "content": [{"text": "The capital of France is Paris."}],
+ "content": [
+ {
+ "type": "text",
+ "text": "The capital of France is Paris.",
+ },
+ ],
},
{
"role": "user",
"content": [
- {"text": "What is the capital of Germany?"},
- {"audio": audio_url},
+ {
+ "type": "text",
+ "text": "What is the capital of Germany?",
+ },
+ {
+ "type": "input_audio",
+ "input_audio": {
+ "data": self.audio_url,
+ "format": "mpeg",
+ },
+ },
],
},
{
"role": "assistant",
- "content": [{"text": "The capital of Germany is Berlin."}],
+ "content": [
+ {
+ "type": "text",
+ "text": "The capital of Germany is Berlin.",
+ },
+ ],
},
{
"role": "user",
- "content": [{"text": "What is the capital of Japan?"}],
+ "content": [
+ {
+ "type": "text",
+ "text": "What is the capital of Japan?",
+ },
+ ],
},
{
"role": "assistant",
- "content": [],
+ "content": None,
"tool_calls": [
{
"id": "call_1",
@@ -209,7 +243,12 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
},
{
"role": "assistant",
- "content": [{"text": "The capital of Japan is Tokyo."}],
+ "content": [
+ {
+ "type": "text",
+ "text": "The capital of Japan is Tokyo.",
+ },
+ ],
},
]
@@ -222,18 +261,11 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
DashScopeMultiAgentFormatter().conversation_history_prompt
)
- # The trailing assistant message ("The capital of Japan is Tokyo.")
- # is always converted to a user <history> message by the multiagent
- # formatter. Whether it carries the full history_prompt prefix
- # depends on whether an "agent_message" group appeared before it:
- # * is_first=False (conversation preceded the tool sequence):
- # only <history>...</history>
- # * is_first=True (no prior agent_message, e.g. tools-only):
- # full history_prompt + <history>...</history>
_gt_trailing_asst_nonfirst = {
"role": "user",
"content": [
{
+ "type": "text",
"text": (
"<history>\n"
"assistant: The capital of Japan is Tokyo.\n"
@@ -246,6 +278,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
"role": "user",
"content": [
{
+ "type": "text",
"text": (
_hist_prompt + "<history>\n"
"assistant: The capital of Japan is Tokyo.\n"
@@ -257,7 +290,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
self._gt_tool_call = {
"role": "assistant",
- "content": [],
+ "content": None,
"tool_calls": [
{
"id": "call_1",
@@ -285,32 +318,32 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
"role": "user",
"content": [
{
+ "type": "text",
"text": (
_hist_prompt + "<history>\n"
- "user: What is the capital of France?"
- ),
- },
- {"image": self.image_url},
- {
- "text": (
+ "user: What is the capital of France?\n"
"assistant: The capital of France is Paris.\n"
- "user: What is the capital of Germany?"
- ),
- },
- {"audio": audio_url},
- {
- "text": (
+ "user: What is the capital of Germany?\n"
"assistant: The capital of Germany is Berlin.\n"
"user: What is the capital of Japan?\n"
"</history>"
),
},
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_url},
+ },
+ {
+ "type": "input_audio",
+ "input_audio": {
+ "data": self.audio_url,
+ "format": "mpeg",
+ },
+ },
],
},
self._gt_tool_call,
self._gt_tool_result,
- # Trailing assistant message: is_first=False because conversation
- # preceded the tool sequence.
_gt_trailing_asst_nonfirst,
]
@@ -373,8 +406,11 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
{
"role": "user",
"content": [
- {"text": "What's in this image?"},
- {"image": self.image_data_uri},
+ {"type": "text", "text": "What's in this image?"},
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_data_uri},
+ },
],
},
],
@@ -440,7 +476,10 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
# The promoted multimodal user message
self.assertEqual(res[2]["role"], "user")
image_blocks = [
- b for b in res[2]["content"] if b.get("image") == self.image_url
+ b
+ for b in res[2]["content"]
+ if b.get("type") == "image_url"
+ and b.get("image_url", {}).get("url") == self.image_url
]
self.assertEqual(len(image_blocks), 1)
diff --git a/tests/model_dashscope_test.py b/tests/model_dashscope_test.py
index f126ce75f..a74a6e11d 100644
--- a/tests/model_dashscope_test.py
+++ b/tests/model_dashscope_test.py
@@ -7,7 +7,6 @@ Formatter tests have been moved to tests/formatter_dashscope_test.py.
import json
from typing import Any
from datetime import datetime
-from http import HTTPStatus
import unittest
from unittest import IsolatedAsyncioTestCase
from unittest.mock import MagicMock
@@ -28,7 +27,6 @@ def _make_model() -> Any:
credential=DashScopeCredential(api_key="test"),
model="qwen3-max",
stream=False,
- multimodality=True,
max_retries=3,
context_size=int(1500),
parameters=DashScopeChatModel.Parameters(
@@ -56,29 +54,32 @@ class TestDashScopeModelParsing(IsolatedAsyncioTestCase):
self,
content: Any = None,
tool_calls: Any = None,
+ reasoning_content: Any = None,
) -> "MagicMock":
- """Build a minimal DashScope GenerationResponse mock."""
- message = {}
- if content is not None:
- message["content"] = content
- if tool_calls is not None:
- message["tool_calls"] = tool_calls
+ """Build a minimal OpenAI ChatCompletion-style mock."""
+ message = MagicMock()
+ message.content = content
+ message.tool_calls = tool_calls
+ message.reasoning_content = reasoning_content
- msg_mock = MagicMock()
- msg_mock.get = lambda key, default=None: message.get(key, default)
+ choice = MagicMock()
+ choice.message = message
+
+ usage = MagicMock()
+ usage.prompt_tokens = 10
+ usage.completion_tokens = 5
+ usage.prompt_tokens_details = None
resp = MagicMock()
- resp.status_code = HTTPStatus.OK
- resp.output.choices[0].message = msg_mock
- resp.request_id = "req-1"
- resp.usage.input_tokens = 10
- resp.usage.output_tokens = 5
+ resp.id = "req-1"
+ resp.choices = [choice]
+ resp.usage = usage
return resp
async def test_parse_text_response(self) -> None:
"""Parsing a text response creates a TextBlock."""
resp = self._mock_response(content="Hello!")
- result = await self.model._parse_dashscope_generation_response(
+ result = self.model._parse_completion_response(
self.start,
resp,
)
@@ -88,17 +89,13 @@ class TestDashScopeModelParsing(IsolatedAsyncioTestCase):
async def test_parse_tool_call_response(self) -> None:
"""Parsing a tool-call response creates a ToolCallBlock."""
- tool_calls = [
- {
- "id": "call-1",
- "function": {
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
- },
- },
- ]
- resp = self._mock_response(tool_calls=tool_calls)
- result = await self.model._parse_dashscope_generation_response(
+ tc_mock = MagicMock()
+ tc_mock.id = "call-1"
+ tc_mock.function.name = "get_weather"
+ tc_mock.function.arguments = '{"city":"Beijing"}'
+
+ resp = self._mock_response(tool_calls=[tc_mock])
+ result = self.model._parse_completion_response(
self.start,
resp,
)
@@ -108,16 +105,6 @@ class TestDashScopeModelParsing(IsolatedAsyncioTestCase):
self.assertEqual(tcs[0].name, "get_weather")
self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
- async def test_parse_response_with_status_error(self) -> None:
- """Non-OK status raises RuntimeError."""
- resp = self._mock_response(content="text")
- resp.status_code = HTTPStatus.BAD_REQUEST
- with self.assertRaises(RuntimeError):
- await self.model._parse_dashscope_generation_response(
- self.start,
- resp,
- )
-
# ---------------------------------------------------------------------------
# Shared _format_tools fixtures