Features¶
The DashScope Python SDK is a typed, resource-style client for Alibaba Cloud Model Studio. A Client lets resources share an HTTP connection pool. Build requests with keyword arguments and read typed results such as resp.output_text.
Resource map¶
| Capability | Client accessor | Guide |
|---|---|---|
| Chat and tool calls | client.chat |
Chat and streaming |
| Multimodal understanding | client.multimodal |
Multimodal |
| Embeddings and rerank | client.embeddings, client.rerank |
Embeddings and rerank |
| Image and video generation | client.images, client.videos |
Images and videos |
| Speech and meetings | client.audio, client.tingwu |
Audio |
| Files, models, and fine-tuning | client.files, client.models, client.fine_tunes, client.deployments |
Files and fine-tuning |
| Managed agents and Workflows | client.agentstudio |
AgentStudio |
| Token counting | client.tokenization |
Tokenization |
| Applications and decisions | client.apps, client.decision |
Client resources |
Find short calls in capability recipes, and source code and run commands in the examples hub.
Sync and async¶
- Use
Clientfor sync calls. WithAsyncClient, await requests and consume streams withasync for. Manage client lifetime withwithorasync with, respectively. - Duplex voice WebSocket sessions use the sync client; choose HTTP TTS or batch transcription for async speech operations.
See async client.
Streaming¶
Chat streams return incremental output over SSE. Choose text_stream, reasoning_stream, or get_final_message() on ChatStream depending on the result you need. The stream is single-pass, and interruption does not trigger automatic resume.
See chat and streaming.
Protocols and transport¶
- Chat: regular requests use HTTP/JSON and streamed responses use SSE. Choose the
native,openai, oranthropicchat protocol withClient(protocol=...)orDASHSCOPE_PROTOCOL. See protocol adapters. - Duplex audio: speech and realtime APIs that need bidirectional transport use WebSocket. Choose an HTTP or WebSocket audio API, or follow the separate Qwen Live guide.
Configuration and request options¶
Configuration resolves from explicit arguments → dashscope.settings → environment variables → ~/.dashscope/config.toml → defaults. The API key can also fall back to a saved key file. Set endpoints, region, workspace, timeouts, and retries on the client. Supported resource methods also accept per-request overrides for credentials, workspace, headers, and retry count.
See configuration.
Errors and diagnosis¶
Failed requests raise typed DashScopeError subclasses by default. Read the error code, HTTP status, and request ID. HTTP calls retry eligible failures according to policy; streams do not reconnect once iteration has started. Read error.diagnosis for local guidance when a rule matches.
See errors and diagnosis.
Local development¶
- Mock debugging: inject
httpx.MockTransportfor custom test responses, or use the dev mock'sstaticandreplaymodes to test HTTP offline.recordsends real requests and may incur charges. See local mock debugging. - Tokenization: use
client.tokenization.count()for server-side counts orget_tokenizer()with an available Qwen vocabulary to estimate budgets locally. First use may download the vocabulary. See tokenization.
SDK-specific capabilities¶
- CLI and SDK Expert: use the
dashscopeCLI for scripts and machine-readable output. Installdashscope[expert]for the optional SDK Expert. - Agentic RL:
client.rlmanages datasets, functions, and jobs. Usedashscope.applications.finetune.AgenticRLto submit training jobs and work with custom reward and rollout code. See the Agentic RL reference.
Legacy compatibility¶
Use 2.x resources for new code. Frozen 1.x public APIs still import from historical paths (for example, from dashscope import Generation) and are implemented under dashscope.legacy_1x. See migration and the legacy API.