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
DashScopeAsyncClientmirrors the sync surface:CompletableFuturefor unary calls, RxJavaFlowablefor streaming. -
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
The complete 2.x surface still works from
com.alibaba.dashscope.legacy_2x.
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.
Errors and diagnosis¶
Failures throw typed DashScopeException subclasses carrying code, request_id and the response
body; the client retries idempotent transport failures with backoff by default. See
Errors and Diagnosis and Configuration.
Legacy 2.x¶
The complete 2.x surface still works from its historical package paths under
com.alibaba.dashscope.legacy_2x, implemented by frozen facades. See Legacy 2.x.
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.