Skip to content

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

fix(dashscope): fix KeyError in dashscope response (#1615)

qbce2ed7f69d957
diff --git a/scripts/model_examples/_utils.py b/scripts/model_examples/_utils.py
index c0cc2bc4f..7f8400a59 100644
--- a/scripts/model_examples/_utils.py
+++ b/scripts/model_examples/_utils.py
@@ -26,15 +26,11 @@ async def stream_and_collect(
continue # Skip printing; full content is in the final chunk
for block in chunk.content:
if isinstance(block, ThinkingBlock):
- if not block.thinking:
- continue # Skip empty delta blocks
if not in_thinking:
print("[Thinking] ", end="", flush=True)
in_thinking = True
print(block.thinking, end="", flush=True)
elif isinstance(block, TextBlock):
- if not block.text:
- continue # Skip empty delta blocks
if in_thinking:
print() # Newline after thinking content
print("--- Answer ---")
diff --git a/src/agentscope/model/_dashscope/_model.py b/src/agentscope/model/_dashscope/_model.py
index 918c2bd77..6705f1f31 100644
--- a/src/agentscope/model/_dashscope/_model.py
+++ b/src/agentscope/model/_dashscope/_model.py
@@ -367,13 +367,13 @@ class DashScopeChatModel(ChatModelBase):
# The chunk usage
if chunk.usage:
u = chunk.usage
- ptd = getattr(u, "prompt_tokens_details", None)
+ 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 = getattr(u, "cached_tokens", 0)
+ cache_read = u.get("cached_tokens", 0)
usage = ChatUsage(
input_tokens=u.input_tokens,
output_tokens=u.output_tokens,
@@ -486,13 +486,13 @@ class DashScopeChatModel(ChatModelBase):
usage = None
if response.usage:
u = response.usage
- ptd = getattr(u, "prompt_tokens_details", None)
+ 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 = getattr(u, "cached_tokens", 0)
+ cache_read = u.get("cached_tokens", 0)
usage = ChatUsage(
input_tokens=u.input_tokens,
output_tokens=u.output_tokens,