DashScope Java SDK¶
The DashScope Java SDK is the official Java client for Alibaba Cloud Model Studio (Bailian): chat completions, multimodal understanding, embeddings, reranking, image and video generation, speech synthesis & recognition, realtime voice, fine-tuning, and managed agents and applications.
The SDK is a typed, resource-style client — DashScopeClient.builder().build() — modeled on the
ergonomics of the OpenAI SDK: one client holds a shared connection pool, every capability hangs off
it as a typed resource, responses are typed objects, and failures throw typed exceptions by default.
Migrating existing 2.x code? See Migrating from 2.x.
Features¶
-
Typed resources
client.chat(),client.images(),client.audio(), … — every capability hangs off one client as a typed resource. -
Sync & async
DashScopeAsyncClientusesCompletableFuturefor supported unary calls and RxJavaFlowablefor streams. See async limits. -
Text-like streaming
Iterate
ResponseStream<ChatCompletionChunk>like a plain collection; each chunk's delta carries a content increment. -
Typed errors & diagnosis
Exceptions carry
codeandrequest_id; opt-in retries and a local mock make failures debuggable offline. -
Encrypted channel
Opt-in RSA envelope encryption protects sensitive payloads end to end.
-
Frozen 2.x facade
Frozen 2.x APIs remain available in 3.x; classes moved to
legacy_2xrequire updated imports.
The full list: Features.
Requirements¶
- JDK 8 or later.
- A Model Studio API key — create one in the Model Studio console, then export it:
Installation¶
Examples¶
Create it¶
import com.alibaba.dashscope.DashScopeClient;
// Reads DASHSCOPE_API_KEY from the config chain (env / -Ddashscope.api.key / config file).
try (DashScopeClient client = DashScopeClient.builder().build()) {
// every capability hangs off the client
}
Run it¶
List<Message> messages = Arrays.asList(
Message.system("You are a helpful assistant."),
Message.user("Tell me a joke"));
ChatCompletion resp = client.chat().completions().create(
ChatCompletionCreateParams.builder()
.model("qwen-plus")
.messages(messages)
.build());
System.out.println(resp.outputText());
System.out.println(resp.getUsage().getTotalTokens());
Stream it¶
Pass createStreaming to get a ResponseStream. Each chunk's delta carries a content increment;
the stream is AutoCloseable and closes automatically when exhausted:
try (ResponseStream<ChatCompletionChunk> stream =
client.chat().completions().createStreaming(
ChatCompletionCreateParams.builder()
.model("qwen-plus")
.messages(messages)
.build())) {
for (ChatCompletionChunk chunk : stream) {
Delta delta = chunk.delta();
if (delta != null && delta.getContent() != null) {
System.out.print(delta.getContent());
}
}
}
See Quickstart for the complete walkthrough and Features for the full capability map.
Protocols and transport¶
The Java SDK's chat().completions() uses the native Model Studio API: HTTP/JSON for regular calls and SSE for streamed responses. Duplex realtime audio APIs use WebSocket.
Unlike the Python SDK, the Java client has no protocol= option for switching among native, OpenAI-compatible, and Anthropic-compatible chat protocols. baseUrl(...) and wsBaseUrl(...) configure endpoints, not the request protocol. See protocols and transport.
Errors and diagnosis¶
Failures raise typed DashScopeException subclasses with code, request_id, and the response body by default. Non-streaming HTTP calls are retried according to the failure and request settings, not the operation's idempotency. SSE bypasses that retry policy. See Errors and Diagnosis and Configuration.
Legacy 2.x¶
When upgrading from 2.x to 3.x, update imports for frozen APIs moved to com.alibaba.dashscope.legacy_2x; the old package names are not preserved for those classes. AgentStudio and shared are outside the frozen layer. See Legacy 2.x and Migration.
Dependencies¶
The SDK builds on OkHttp (+ SSE), Jackson, Gson, RxJava 2, SLF4J and victools jsonschema-generator; Lombok is compile-time only. The full list: see About.
License¶
This project is licensed under the Apache License 2.0.