{"id":2915,"library":"dashscope","title":"DashScope Python SDK","description":"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.","status":"active","version":"1.25.16","language":"python","source_language":"en","source_url":"https://github.com/dashscope/dashscope-sdk-python","tags":["AI","LLM","Alibaba Cloud","Generative AI","Model Inference","Multimodal"],"install":[{"cmd":"pip install dashscope","lang":"bash","label":"Standard install"},{"cmd":"pip install dashscope[tokenizer]","lang":"bash","label":"Install with local tokenizer support"}],"dependencies":[{"reason":"Often used for managing API keys in local development environments.","package":"python-dotenv","optional":true}],"imports":[{"note":"Generation is a direct import from the dashscope package for text generation APIs.","wrong":"import dashscope.Generation","symbol":"Generation","correct":"from dashscope import Generation"},{"note":"Used specifically for multimodal models that handle image and text inputs.","wrong":null,"symbol":"MultiModalConversation","correct":"from dashscope import MultiModalConversation"},{"note":"A utility function to persist your API key to a file.","wrong":null,"symbol":"save_api_key","correct":"from dashscope import save_api_key"},{"note":"Needed for global configurations like 'dashscope.api_key' or 'dashscope.base_http_api_url'.","wrong":null,"symbol":"dashscope","correct":"import dashscope"}],"quickstart":{"code":"import os\nfrom http import HTTPStatus\nfrom dashscope import Generation\n\n# Set your DashScope API key via environment variable for security\n# export DASHSCOPE_API_KEY='YOUR_API_KEY'\ndashscope_api_key = os.environ.get('DASHSCOPE_API_KEY', '')\n\nif not dashscope_api_key:\n    print(\"Error: DASHSCOPE_API_KEY environment variable not set.\")\n    print(\"Please set it using: export DASHSCOPE_API_KEY='YOUR_API_KEY'\")\nelse:\n    # Set the API key programmatically if not using environment variable or for testing\n    # dashscope.api_key = dashscope_api_key \n    \n    # For models in specific regions, you might need to set the base URL.\n    # For Singapore region (international users):\n    # dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'\n    # For China (Beijing) region:\n    # dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'\n\n    print(\"Calling DashScope LLM...\")\n    responses = Generation.call(\n        model=Generation.Models.qwen_turbo, # or 'qwen-plus', 'qwen-max', etc.\n        prompt='Tell me a short story about a brave knight.',\n        api_key=dashscope_api_key # It's good practice to explicitly pass the key or ensure it's set globally\n    )\n\n    if responses.status_code == HTTPStatus.OK:\n        print('Story generated:')\n        print(responses.output.text)\n    else:\n        print(f'Failed request_id: {responses.request_id}, status_code: {responses.status_code}, ')\n        print(f'code: {responses.code}, message: {responses.message}')\n","lang":"python","description":"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."},"warnings":[{"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`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If `stream=True`, iterate through the `responses` object (e.g., `for chunk in responses: print(chunk.output.choices[0].text)`).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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'.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.25.16':48 'access':22 'activ':41 'ai':19,60,65 'alibaba':15,62 'client':10 'cloud':16,63 'current':45 'dashscop':1,5,18 'develop':42 'embed':36 'featur':59 'frequent':53 'generat':64 'imag':34 'includ':26 'incorpor':55 'infer':67 'interact':13 'languag':28 'larg':27 'librari':11,39 'llm':61 'llms':30 'model':25,29,37,66 'multimod':68 'new':50,58 'occur':52 'offer':21 'provid':8 'python':2,6 'releas':51 'sdk':3,7 'servic':20 'text':32 'text-to-imag':31 'updat':56 'various':24 'version':46","created_at":"2026-04-11T09:13:04.035243+00:00","updated_at":"2026-04-16T05:09:19.822078+00:00","problems":[{"fix":"Run `pip install dashscope` in your terminal to install the library.","cause":"The 'dashscope' Python package is not installed in the current environment or is not accessible on the Python path.","error":"ModuleNotFoundError: No module named 'dashscope'"},{"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.","cause":"The DashScope API key is either missing, incorrect, expired, or not correctly configured, sometimes due to an incorrect API base URL for the region.","error":"invalid API key (or similar HTTP 400/401 errors related to authentication)"},{"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.","cause":"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`.","error":"AttributeError: 'NoneType' object has no attribute 'choices' (or 'results')"},{"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.","cause":"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.","error":"ValueError: \"DashScopeEmbedding\" object has no field \"_api_key\" (or similar ValueError related to parameters/initialization)"}],"ecosystem":"pypi","meta_description":"dashscope · Alibaba Cloud AI SDK · pip install dashscope · from dashscope import Generation · dashscope.api_key=\"YOUR_KEY\" · Generation.call(model=\"qwen-turbo\")","install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.27.2","cli_name":"","cli_version":null,"type":"library","homepage":"https://dashscope.aliyun.com/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/dashscope/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["llm-agents","ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}