DashScope Python SDK
The DashScope Python SDK provides a client library for interacting with Alibaba Cloud's DashScope AI services, offering access to various models including Large Language Models (LLMs), text-to-image, and embedding models. The library is actively developed, with its current version at 1.25.16, and new releases occurring frequently to incorporate updates and new features.
Warnings
- gotcha Missing or incorrect API Key configuration leads to `AuthenticationError`. The SDK requires an API key for authentication, which can be set via `dashscope.api_key`, the `DASHSCOPE_API_KEY` environment variable, or by specifying an API key file path.
- gotcha Incorrect `base_http_api_url` for your region can cause 'invalid API key' or 'url error' even if the key is correct. Alibaba Cloud DashScope has different endpoints for international (e.g., Singapore) and Mainland China regions.
- gotcha Using the wrong API endpoint/method for the model type (e.g., `Generation.call()` for multimodal models or `MultiModalConversation.call()` for plain text models) results in 'url error' or invalid parameter errors.
- gotcha When `stream=True` is enabled for generation, the API returns a generator object, not a direct response. You must iterate over this generator to receive streaming chunks of results.
- gotcha Multimodal models have strict limits on local file sizes (e.g., 10MB after Base64 encoding for images) and specific URL requirements. Exceeding these limits or providing corrupted/incorrect formats will lead to errors like 'Multimodal file size is too large' or 'read image error'.
Install
-
pip install dashscope -
pip install dashscope[tokenizer]
Imports
- Generation
from dashscope import Generation
- MultiModalConversation
from dashscope import MultiModalConversation
- save_api_key
from dashscope import save_api_key
- dashscope
import dashscope
Quickstart
import os
from http import HTTPStatus
from dashscope import Generation
# Set your DashScope API key via environment variable for security
# export DASHSCOPE_API_KEY='YOUR_API_KEY'
dashscope_api_key = os.environ.get('DASHSCOPE_API_KEY', '')
if not dashscope_api_key:
print("Error: DASHSCOPE_API_KEY environment variable not set.")
print("Please set it using: export DASHSCOPE_API_KEY='YOUR_API_KEY'")
else:
# Set the API key programmatically if not using environment variable or for testing
# dashscope.api_key = dashscope_api_key
# For models in specific regions, you might need to set the base URL.
# For Singapore region (international users):
# dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# For China (Beijing) region:
# dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
print("Calling DashScope LLM...")
responses = Generation.call(
model=Generation.Models.qwen_turbo, # or 'qwen-plus', 'qwen-max', etc.
prompt='Tell me a short story about a brave knight.',
api_key=dashscope_api_key # It's good practice to explicitly pass the key or ensure it's set globally
)
if responses.status_code == HTTPStatus.OK:
print('Story generated:')
print(responses.output.text)
else:
print(f'Failed request_id: {responses.request_id}, status_code: {responses.status_code}, ')
print(f'code: {responses.code}, message: {responses.message}')