Dashscope Basic

Run Qwen Plus on DashScope with sync, async, and streaming responses.

basic.py
"""
Dashscope Basic
===============

Cookbook example for `dashscope/basic.py`.
"""

from agno.agent import Agent, RunOutput  # noqa
from agno.models.dashscope import DashScope
import asyncio

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

agent = Agent(model=DashScope(id="qwen-plus", temperature=0.5), markdown=True)

# Get the response in a variable
# run: RunOutput = agent.run("Share a 2 sentence horror story")
# print(run.content)

# Print the response in the terminal

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    # --- Sync ---
    agent.print_response("Share a 2 sentence horror story")

    # --- Sync + Streaming ---
    agent.print_response("Share a 2 sentence horror story", stream=True)

    # --- Async ---
    asyncio.run(agent.aprint_response("Share a 2 sentence horror story"))

    # --- Async + Streaming ---
    async def main():
        # Get the response in a variable
        # async for chunk in agent.arun("Share a 2 sentence horror story", stream=True):
        #     print(chunk.content, end="", flush=True)

        # Print the response in the terminal
        await agent.aprint_response("Share a 2 sentence horror story", stream=True)

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai

Configure regional DashScope access

Activate Alibaba Cloud Model Studio and create an API key for your region and workspace. Agno's default endpoint is Singapore: https://dashscope-intl.aliyuncs.com/compatible-mode/v1.

export DASHSCOPE_API_KEY="your_singapore_api_key"

For another region or a workspace-specific domain, pass the matching base_url to DashScope. Use the OpenAI-compatible endpoint guide to match the URL, key and available model. The documented Singapore default remains functional.

Invoke async streaming

Save the source as basic.py. After the main() function definition, add asyncio.run(main()) inside the if __name__ == "__main__": block, at the same indentation as async def main():. The source defines this function but never calls it.

Run the example

Save the code above as basic.py, then run:

python basic.py

Full source: cookbook/90_models/dashscope/basic.py