Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(dashscope): fix KeyError in dashscope response (#1615)
qbce2ed7f69d957
diff --git a/scripts/model_examples/_utils.py b/scripts/model_examples/_utils.pyindex 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 chunkfor block in chunk.content:if isinstance(block, ThinkingBlock):- if not block.thinking:- continue # Skip empty delta blocksif not in_thinking:print("[Thinking] ", end="", flush=True)in_thinking = Trueprint(block.thinking, end="", flush=True)elif isinstance(block, TextBlock):- if not block.text:- continue # Skip empty delta blocksif in_thinking:print() # Newline after thinking contentprint("--- Answer ---")diff --git a/src/agentscope/model/_dashscope/_model.py b/src/agentscope/model/_dashscope/_model.pyindex 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 usageif 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 = Noneif 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,