dashscope 1.27.7


pip install dashscope

  Latest version

Released: Sep 24, 2026

Project Links

Meta
Author: Alibaba Cloud
Requires Python: >=3.9

Classifiers

Development Status
  • 4 - Beta

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14

DashScope Python SDK

English | 中文

The DashScope Python SDK provides a comprehensive interface to Alibaba Cloud Model Studio (Bailian) APIs, covering text generation, multi-modal understanding, embeddings, reranking, image/video generation, speech synthesis & recognition, and more.

What is New

v1.27.0 ships an interactive AI assistant — DashScope SDK Expert. Run dashscope with no arguments (or ask directly with dashscope expert "how do I stream Generation output") to get SDK/API answers, runnable examples, CLI usage, and error diagnosis right in your terminal. Guidance is drawn from per-domain quick-reference skills (text, multimodal, speech, retrieval, fine-tuning, agent, cli) built on the SDK's public interfaces — parameters, outputs, and error codes — so you can ask instead of reading the docs. Type /help inside the assistant to view available commands.

Installation

To install the DashScope Python SDK, simply run:

pip install dashscope

The base install covers SDK API calls and the dashscope CLI command. Optional feature groups are available as extras:

Extra Provides Install
acli Interactive AI assistant (DashScope SDK Expert) pip install "dashscope[acli]"
rl Agentic RL fine-tuning pip install "dashscope[rl]"
tokenizer Local tokenizer without downloads pip install "dashscope[tokenizer]"

If you clone the code from github, you can install from source by running:

pip install -e .

Quick Start

from http import HTTPStatus
from dashscope import Generation

responses = Generation.call(
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who are you?"},
    ],
    result_format="message",
)

if responses.status_code == HTTPStatus.OK:
    print(responses.output.choices[0].message.content)
else:
    print(f"Error: {responses.code} - {responses.message}")

API Key Authentication

The SDK uses API key for authentication. To obtain an API Key, see How to get an API Key. Please refer to official documentation for alibabacloud china and official documentation for alibabacloud international regarding how to obtain your api-key.

Using the API Key

  1. Set the API key via code
import dashscope

dashscope.api_key = 'YOUR-DASHSCOPE-API-KEY'
# Or specify the API key file path via code
# dashscope.api_key_file_path='~/.dashscope/api_key'
  1. Set the API key via environment variables

a. Set the API key directly using the environment variable below

export DASHSCOPE_API_KEY='YOUR-DASHSCOPE-API-KEY'

b. Specify the API key file path via an environment variable

export DASHSCOPE_API_KEY_FILE_PATH='~/.dashscope/api_key'
  1. Save the API key to a file
from dashscope import save_api_key

save_api_key(api_key='YOUR-DASHSCOPE-API-KEY',
             api_key_file_path='api_key_file_location or (None, will save to default location "~/.dashscope/api_key"')

Region and Endpoint Configuration

By default the SDK sends requests to the China (Beijing) public endpoint dashscope.aliyuncs.com. If your Model Studio (Bailian) workspace lives in another region, switch the endpoint before making calls.

Using set_region

dashscope.set_region(region, workspace_id) points the HTTP, WebSocket and OpenAI-compatible base URLs at the given region in a single call. workspace_id is required and is used as the endpoint subdomain.

import dashscope

# Switch to the Singapore region for workspace "ws-xxx123"
dashscope.set_region(region="ap-southeast-1", workspace_id="ws-xxx123")

# All subsequent calls use:
#   https://ws-xxx123.ap-southeast-1.maas.aliyuncs.com/api/v1
print(dashscope.base_http_api_url)

Supported regions:

Region Location
cn-beijing China (Beijing)
cn-hongkong China (Hong Kong)
ap-southeast-1 Singapore
ap-northeast-1 Japan (Tokyo)
eu-central-1 Germany (Frankfurt)
us-east-1 US (Virginia)

API keys are region-specific. Each region issues its own API keys (sk- prefix) in its Model Studio console, and keys cannot be mixed across regions — using a key from another region fails with 401. Switch api_key together with the region.

Region-specific notes:

  • WebSocket endpoints (wss://.../api-ws/v1/inference) are only served in cn-beijing and ap-southeast-1. set_region still sets base_websocket_api_url for every region, but WebSocket-based realtime APIs (realtime speech recognition/synthesis, multimodal dialog, etc.) are not available in the other regions.
  • eu-central-1 / ap-northeast-1: the deployment scope (Global, or EU / Japan) is chosen when the workspace is created in the console, not per API call.
  • us-east-1: model names with the -us suffix (e.g. qwen-plus-us) restrict inference to the US; names without the suffix default to global inference.
  • Batch inference, model fine-tuning and application development are currently only available in cn-beijing and ap-southeast-1.

set_region updates process-wide globals, so it is not concurrency-safe when a single process talks to multiple regions at the same time. Call it once at startup, or re-call it before each switch.

Using environment variables

You can also select the region without code:

export DASHSCOPE_API_REGION='ap-southeast-1'   # default: cn-beijing
export DASHSCOPE_WORKSPACE_ID='ws-xxx123'      # used to resolve the endpoint subdomain

When a MaaS region is set via DASHSCOPE_API_REGION, the SDK builds the regional endpoints and substitutes DASHSCOPE_WORKSPACE_ID into them. You can also override each base URL directly:

Environment variable Overrides
DASHSCOPE_HTTP_BASE_URL HTTP endpoint (dashscope.base_http_api_url)
DASHSCOPE_WEBSOCKET_BASE_URL WebSocket endpoint (dashscope.base_websocket_api_url)
DASHSCOPE_COMPATIBLE_BASE_URL OpenAI-compatible endpoint (dashscope.base_compatible_api_url)

set_region always builds workspace-exclusive endpoints. Some regions also offer shared domains without a workspace subdomain — dashscope.aliyuncs.com (Beijing), dashscope-intl.aliyuncs.com (Singapore) and dashscope-us.aliyuncs.com (US Virginia); use the override variables above to point at them.

OpenAI-compatible chat completions

The SDK exposes an OpenAI-compatible chat completions entry that talks to dashscope.base_compatible_api_url (request path chat/completions) — no extra openai package required. It follows the region configured above.

import dashscope
from dashscope.aigc.chat_completion import Completions

dashscope.set_region(region="cn-hongkong", workspace_id="ws-hk-789")

response = Completions.create(
    model="qwen-max",
    messages=[{"role": "user", "content": "Hello"}],
    api_key="YOUR-DASHSCOPE-API-KEY",
    stream=False,  # set True to get a generator of ChatCompletionChunk
)
print(response)

A complete runnable example is available in samples/set_region_example.py.

AI Assistant: DashScope SDK Expert

The SDK ships with an interactive AI assistant, DashScope SDK Expert, built on the bundled Agentic CLI (dashscope/acli) framework. For DashScope SDK/CLI users it is the recommended way to get development consultation and AI coding help — answering SDK/API questions, generating runnable examples, showing CLI usage, and diagnosing errors, right in your terminal.

  • Run dashscope with no arguments to start the assistant. On first run it offers to install the SDK Expert knowledge pack (per-domain quick-reference skills: text, multimodal, speech, retrieval, fine-tuning, agent, cli), so guidance comes from the SDK's public interfaces — parameters, outputs, error codes — without reading the source
  • Ask it instead of reading docs — e.g. dashscope expert "how do I stream Generation output" or dashscope expert "CLI command to cancel a fine-tuning job", each of which asks once and exits. Type /help inside the assistant to list available commands (/setup, /skill, /stats, ...); classic SDK subcommands still work, plain dashscope expert enters the assistant interactively, and dashscope expert --help shows all the forms
  • Full walkthrough: DashScope SDK Expert guide

Supported Models

Category Recommended Models SDK Class
Text Generation qwen3.8-max, qwen3.7-max, qwen3.7-plus, qwen3.6-flash Generation
Multi-Modal Understanding qwen3.5-omni-plus, qwen3.7-plus (vision) MultiModalConversation
Text Embedding text-embedding-v4, text-embedding-v3 TextEmbedding
Multi-Modal Embedding tongyi-embedding-vision-plus, qwen3-vl-embedding MultiModalEmbedding
Text ReRank qwen3-rerank, gte-rerank-v2 TextReRank
Image Generation wan2.7-image-pro, qwen-image-2.0-pro ImageSynthesis
Video Generation wan2.7-t2v, wan2.7-i2v, happyhorse-1.0-t2v/i2v VideoSynthesis
Speech Synthesis (TTS) cosyvoice-v3.5-plus, cosyvoice-v1 SpeechSynthesizer, HttpSpeechSynthesizer
Speech Recognition (ASR) fun-asr-realtime, fun-asr, paraformer-v1 Transcription
Omni (Real-time) qwen3.5-omni-plus-realtime MultiModalConversation

For the latest model list, visit Bailian Model Plaza.

Shell Completion

Run the appropriate command once, then restart your shell (or re-source your config file):

Shell Install command
bash dashscope --install-completion bash
zsh dashscope --install-completion zsh
fish dashscope --install-completion fish

To preview the completion script without installing:

dashscope --show-completion bash

Logging

To output Dashscope logs, you need to configure the logger.

export DASHSCOPE_LOGGING_LEVEL='info'

Output

The output contains the following fields:

     request_id (str): The request id.
     status_code (int): HTTP status code, 200 indicates that the
         request was successful, others indicate an error.
     code (str): Error code if error occurs, otherwise empty str.
     message (str): Set to error message on error.
     output (Any): The request output.
     usage (Any): The request usage information.

License

This project is licensed under the Apache License (Version 2.0).

1.27.7 Sep 24, 2026
1.27.6 Sep 17, 2026
1.27.5 Sep 11, 2026
1.27.4 Sep 04, 2026
1.27.3 Sep 01, 2026
1.27.2 Aug 27, 2026
1.27.1 Aug 21, 2026
1.27.0 Aug 18, 2026
1.26.7 Aug 12, 2026
1.26.6 Aug 07, 2026
1.26.5 Jul 31, 2026
1.26.4 Jul 17, 2026
1.26.3 Jul 10, 2026
1.26.2 Jul 02, 2026
1.26.1 Jul 02, 2026
1.26.0 Jun 25, 2026
1.25.24 Jun 23, 2026
1.25.23 Jun 18, 2026
1.25.22 Jun 16, 2026
1.25.21 Jun 04, 2026
1.25.20 Jun 01, 2026
1.25.19 May 25, 2026
1.25.18 May 13, 2026
1.25.17 Apr 16, 2026
1.25.16 Apr 07, 2026
1.25.15 Mar 24, 2026
1.25.14 Mar 16, 2026
1.25.13 Mar 03, 2026
1.25.12 Feb 09, 2026
1.25.11 Feb 03, 2026
1.25.10 Jan 29, 2026
1.25.9 Jan 21, 2026
1.25.8 Jan 16, 2026
1.25.7 Jan 09, 2026
1.25.6 Jan 07, 2026
1.25.5 Dec 18, 2025
1.25.4 Dec 15, 2025
1.25.3 Dec 10, 2025
1.25.2 Nov 23, 2025
1.25.1 Nov 12, 2025
1.25.0 Nov 05, 2025
1.24.10 Nov 05, 2025
1.24.9 Oct 29, 2025
1.24.8 Oct 27, 2025
1.24.7 Oct 21, 2025
1.24.6 Sep 22, 2025
1.24.5 Sep 16, 2025
1.24.4 Sep 08, 2025
1.24.3 Sep 05, 2025
1.24.2 Aug 20, 2025
1.24.1 Aug 01, 2025
1.24.0 Jul 24, 2025
1.23.9 Jul 19, 2025
1.23.8 Jul 08, 2025
1.23.7 Jul 04, 2025
1.23.6 Jun 24, 2025
1.23.5 Jun 17, 2025
1.23.4 Jun 03, 2025
1.23.3 May 14, 2025
1.23.2 Apr 28, 2025
1.23.1 Apr 07, 2025
1.23.0 Apr 02, 2025
1.22.2 Mar 06, 2025
1.22.1 Jan 20, 2025
1.22.0 Jan 17, 2025
1.21.0 Jan 14, 2025
1.20.14 Dec 03, 2024
1.20.13 Nov 18, 2024
1.20.12 Oct 23, 2024
1.20.11 Oct 14, 2024
1.20.10 Sep 13, 2024
1.20.9 Sep 06, 2024
1.20.8 Sep 03, 2024
1.20.7 Sep 01, 2024
1.20.6 Aug 28, 2024
1.20.5 Aug 26, 2024
1.20.4 Aug 13, 2024
1.20.3 Jul 19, 2024
1.20.2 Jul 16, 2024
1.20.1 Jul 02, 2024
1.20.0 Jun 21, 2024
1.19.3 Jun 12, 2024
1.19.2 May 24, 2024
1.19.1 May 17, 2024
1.19.0 May 13, 2024
1.18.1 May 07, 2024
1.18.0 Apr 30, 2024
1.17.1 Apr 19, 2024
1.17.0 Mar 31, 2024
1.16.0 Mar 29, 2024
1.15.0 Mar 16, 2024
1.14.1 Jan 25, 2024
1.14.0 Jan 11, 2024
1.13.6 Dec 18, 2023
1.13.5 Dec 11, 2023
1.13.4 Dec 07, 2023
1.13.3 Nov 15, 2023
1.13.2 Nov 10, 2023
1.13.1 Nov 01, 2023
1.13.0 Oct 28, 2023
1.12.0 Oct 13, 2023
1.11.0 Sep 22, 2023
1.10.1 Sep 15, 2023
1.10.0 Sep 13, 2023
1.9.1 Sep 11, 2023
1.9.0 Sep 07, 2023
1.8.1 Sep 01, 2023
1.8.0 Aug 31, 2023
1.7.2 Aug 28, 2023
1.7.1 Aug 28, 2023
1.7.0 Aug 25, 2023
1.6.0 Aug 15, 2023
1.5.0 Jul 26, 2023
1.4.0 Jul 26, 2023
1.3.1 Jun 25, 2023
1.3.0 Jun 20, 2023
1.2.0 Jun 01, 2023
1.1.1 May 24, 2023
1.1.0 May 18, 2023
1.0.4 Apr 27, 2023
1.0.3 Apr 10, 2023
1.0.2 Apr 10, 2023
1.0.1 Apr 10, 2023
1.0.0 Apr 10, 2023

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
aiohttp
requests
websocket-client
cryptography
certifi
typer (>=0.9.0)
rich (>=13.0.0)
httpx (>=0.27.0)
httpx-sse (>=0.4.0)
typing_extensions (>=4.0)