dashscope 1.25.15


pip install dashscope

  Latest version

Released: Mar 24, 2026

Project Links

Meta
Author: Alibaba Cloud
Requires Python: >=3.8.0

Classifiers

Development Status
  • 4 - Beta

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.8
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11

English

DashScope Python Library

Installation

To install the DashScope Python SDK, simply run:

pip install dashscope

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

pip install -e .

To use tokenizer in local mode without downloading any files, run:

pip install dashscope[tokenizer]

QuickStart

You can use Generation api to call model qwen-turbo(通义千问).

from http import HTTPStatus
import dashscope
from dashscope import Generation

dashscope.api_key = 'YOUR-DASHSCOPE-API-KEY'
responses = Generation.call(model=Generation.Models.qwen_turbo,
                            prompt='今天天气好吗?')

if responses.status_code == HTTPStatus.OK:
    print('Result is: %s' % responses.output)
else:
    print('Failed request_id: %s, status_code: %s, code: %s, message:%s' %
            (responses.request_id, responses.status_code, responses.code,
            responses.message))

API Key Authentication

The SDK uses API key for authentication. 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"')

Sample Code

call function provides synchronous call, the function call will return when computation is done on the server side.

from http import HTTPStatus
from dashscope import Generation
# export DASHSCOPE_API_KEY='YOUR-DASHSCOPE-API-KEY' in environment
def sync_dashscope_sample():
    responses = Generation.call(
        model=Generation.Models.qwen_turbo,
        prompt='Is the weather good today?')

    if responses.status_code == HTTPStatus.OK:
        print('Result is: %s'%responses.output)
    else:
        print('Code: %s, status_code: %s, code: %s, message: %s'%(responses.status_code,
                                                   responses.code,
                                                   responses.message))

if __name__ == '__main__':
    sync_dashscope_sample()

For requests with longer processing times, you can obtain partial results before the full output is generated. Set the stream parameter to True. In this case, the results will be returned in batches, and the current output mode is incremental (output will overwrite the previous content). When the output is in stream mode, the interface returns a generator, and you need to iterate through the generator to get the results. Each output contains partial data for streaming, and the last output contains the final generated result.

Example with simple streaming:

from http import HTTPStatus
from dashscope import Generation

def sample_sync_call_stream():
    prompt_text = 'Give me a recipe using carrots, potatoes, and eggplants'
    response_generator = Generation.call(
        model=Generation.Models.qwen_turbo,
        prompt=prompt_text,
        stream=True,
        max_length=512,
        top_p=0.8)
    for resp in response_generator:  # Iterate through the streaming output results
        if resp.status_code == HTTPStatus.OK:
            print(resp.output)
        else:
            print('Request failed, message: %s'%resp.message)

if __name__ == '__main__':
    sample_sync_call_stream()

Stream with Messages

from http import HTTPStatus
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role


def stream_with_messages():
    messages = [{'role': Role.SYSTEM, 'content': 'You are a helpful assistant.'},
                {'role': Role.USER, 'content': '如何做西红柿炖牛腩?'}]
    responses = Generation.call(
        Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
        stream=True,
    )
    for response in responses:
       if response.status_code == HTTPStatus.OK:
           print(response)
       else:
           print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
               response.request_id, response.status_code,
               response.code, response.message
           ))

if __name__ == '__main__':
    stream_with_messages()

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.

Error Handling

Currently, errors are thrown as exceptions.

Contributing

Coming soon.

License

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

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