Skip to content

DashScope Java SDK

v3.0.0 JDK 8+ Apache-2.0

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


    DashScopeAsyncClient mirrors the sync surface: CompletableFuture for unary calls, RxJava Flowable for streaming.

  • Text-like streaming


    Iterate ResponseStream<ChatCompletionChunk> like a plain collection; each chunk's delta carries a content increment.

  • Typed errors & diagnosis


    Exceptions carry code and request_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:
export DASHSCOPE_API_KEY="sk-..."

Installation

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dashscope-sdk-java</artifactId>
    <version>3.0.0</version>
</dependency>
implementation 'com.alibaba:dashscope-sdk-java:3.0.0'

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());
try (DashScopeAsyncClient client = DashScopeAsyncClient.builder().build()) {
  CompletableFuture<ChatCompletion> future = client.chat().completions().create(params);
  future.thenAccept(resp -> System.out.println(resp.outputText())).join();
}

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.