{"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":"en","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,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}