Registry / llm-agents / dashscope

dashscope

JSON →
library1.27.2pypypi✓ verified 29d ago

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.

pip install dashscope
INSTALL
IMPORT
SIG · DASHSCOPE
D
dashscope
llm-agentspythonv1.27.2
Install
11.6s avg
Import
1140ms
Disk
110MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.27.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 1.178s · 109.6MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 11.6s · import 1.102s · 112MB
110MB installed
● package 110MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Generation
✓ from dashscope import Generation
✗ import dashscope.Generation
Generation is a direct import from the dashscope package for text generation APIs.
MultiModalConversation
✓ from dashscope import MultiModalConversation
Used specifically for multimodal models that handle image and text inputs.
save_api_key
✓ from dashscope import save_api_key
A utility function to persist your API key to a file.
dashscope
✓ import dashscope
Needed for global configurations like 'dashscope.api_key' or 'dashscope.base_http_api_url'.

This quickstart demonstrates how to use the `dashscope.Generation` API to interact with an LLM, such as `qwen-turbo`. It emphasizes setting the API key via an environment variable for security and includes basic error handling. It also highlights the importance of potentially setting the `base_http_api_url` for different regions.

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}')
Debug
Known issues
gotchaMissing 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.
fix
Ensure `DASHSCOPE_API_KEY` environment variable is set or `dashscope.api_key = 'YOUR_API_KEY'` is used before making calls. For file-based keys, use `DASHSCOPE_API_KEY_FILE_PATH` or `dashscope.api_key_file_path`.
affects: All versions
gotchaIncorrect `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.
fix
Explicitly set `dashscope.base_http_api_url` to the correct endpoint for your region. For international, often `https://dashscope-intl.aliyuncs.com/api/v1`. For Mainland China, `https://dashscope.aliyuncs.com/api/v1` or `https://dashscope.aliyuncs.com/compatible-mode/v1`.
affects: All versions
gotchaUsing 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.
fix
Use `Generation.call()` for text-only models (like Qwen-Turbo, Qwen-Plus) and `MultiModalConversation.call()` for multimodal models (like Qwen-VL, Qwen-Omni) that accept image or video inputs.
affects: All versions
gotchaWhen `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.
fix
If `stream=True`, iterate through the `responses` object (e.g., `for chunk in responses: print(chunk.output.choices[0].text)`).
affects: All versions
gotchaMultimodal 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'.
fix
Adhere to the specified file size limits and ensure image/video formats are supported. For local files, ensure they are correctly Base64 encoded if required by the API specifications for direct content inclusion.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dashscope'
The 'dashscope' Python package is not installed in the current environment or is not accessible on the Python path.
fix
Run `pip install dashscope` in your terminal to install the library.
invalid API key (or similar HTTP 400/401 errors related to authentication)
The DashScope API key is either missing, incorrect, expired, or not correctly configured, sometimes due to an incorrect API base URL for the region.
fix
Ensure your `DASHSCOPE_API_KEY` environment variable is set to a valid API key, or explicitly set `dashscope.api_key = 'YOUR_DASHSCOPE_API_KEY'` in your code. Verify the API base URL if you are in Mainland China or using an OpenAI-compatible client, as it might need to be set to `https://dashscope.aliyuncs.com/compatible-mode/v1` or another regional endpoint.
AttributeError: 'NoneType' object has no attribute 'choices' (or 'results')
This error occurs when an API call fails (e.g., due to rate limiting, invalid model, or malformed request), causing the response object's `output` or other nested attributes to be `None` or an unexpected type. Subsequent attempts to access properties on this `None` object then raise an `AttributeError`.
fix
Always check the API response's status code or for the presence of `response.output` (e.g., `if response.output and response.output.choices:`) before attempting to access nested attributes. Implement error handling and retry mechanisms for transient failures like rate limits.
ValueError: "DashScopeEmbedding" object has no field "_api_key" (or similar ValueError related to parameters/initialization)
This typically indicates that a required parameter during object initialization (e.g., `api_key` for `DashScopeEmbedding` in integration libraries) was not provided or correctly recognized, or that a model parameter (like `max_tokens` or `seed`) is outside its valid range or in an incorrect format.
fix
Review the documentation for the specific class or function you are using to ensure all required parameters are provided with correct types and values. For integration libraries, confirm how the API key and other configurations are expected to be passed to the DashScope components.
Upgrade
Version history
1.27.2latest on PyPI · released Aug 27, 2026
Audit
Dependencies
python-dotenvoptionalOften used for managing API keys in local development environments.
Agent activity
53 hits · last 30 days
node
46
OpenAI (training)
2
Resources
dashscope — pip install dashscope · libregistry