Async Client¶
DashScopeAsyncClient mirrors the sync resource tree exactly — same accessors, same params
builders — but unary calls return CompletableFuture<T> and streaming calls return RxJava
Flowable<T> (2.x parity). Under the hood the async services delegate to the sync
implementations on an executor, so behavior (retries, errors, config chain) is identical.
Adapted from the runnable ChatCompletionsCreateAsync.
Full runnable example: ChatCompletionsCreateAsync.java (auto-synced)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.chat;
import com.alibaba.dashscope.DashScopeAsyncClient;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.types.completions.ChatCompletion;
import com.alibaba.dashscope.types.completions.ChatCompletionChunk;
import com.alibaba.dashscope.types.completions.ChatCompletionCreateParams;
import com.alibaba.dashscope.types.completions.Delta;
import com.alibaba.dashscope.types.messages.Message;
import io.reactivex.Flowable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Async chat completions: {@link DashScopeAsyncClient} mirrors the sync resource tree, returning
* {@link CompletableFuture} for unary calls and RxJava {@link Flowable} for streaming (2.x parity).
*
* <p>Work runs on a daemon executor owned by the client; try-with-resources shuts it down. Compare
* with the sync {@link ChatCompletionsCreate}.
*/
public final class ChatCompletionsCreateAsync {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "qwen3.7-plus");
List<Message> messages =
Arrays.asList(Message.system("You are a helpful assistant."), Message.user("请用一句话介绍杭州"));
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder().model(model).messages(messages).build();
try (DashScopeAsyncClient client = ExampleSupport.asyncClient()) {
// Non-streaming: the call returns immediately; compose on the future.
System.out.println("----- async non-streaming request -----");
CompletableFuture<ChatCompletion> future = client.chat().completions().create(params);
CompletableFuture<String> answer =
future.thenApply(
resp -> {
System.out.println(resp.outputText());
System.out.println(
"usage: "
+ resp.getUsage().getTotalTokens()
+ " tokens, request_id: "
+ resp.getRequestId());
return resp.outputText();
});
answer.join();
// Streaming: chunks surface as a Flowable; blockingForEach consumes them on this thread.
System.out.println("----- async streaming request -----");
Flowable<ChatCompletionChunk> stream = client.chat().completions().createStreaming(params);
stream.blockingForEach(
chunk -> {
Delta delta = chunk.delta();
if (delta != null && delta.getContent() != null) {
System.out.print(delta.getContent());
System.out.flush();
}
});
System.out.println();
}
}
}
Unary calls: CompletableFuture¶
import com.alibaba.dashscope.DashScopeAsyncClient;
import com.alibaba.dashscope.types.completions.ChatCompletion;
import java.util.concurrent.CompletableFuture;
try (DashScopeAsyncClient client = DashScopeAsyncClient.builder().build()) {
// The call returns immediately; compose on the future.
CompletableFuture<ChatCompletion> future = client.chat().completions().create(params);
CompletableFuture<String> answer = future.thenApply(resp -> {
System.out.println(resp.outputText());
System.out.println("usage: " + resp.getUsage().getTotalTokens()
+ " tokens, request_id: " + resp.getRequestId());
return resp.outputText();
});
answer.join();
}
Failures complete the future exceptionally with the same typed
DashScopeException family the sync client throws — unwrap
CompletionException.getCause() when handling.
Streaming: Flowable¶
import com.alibaba.dashscope.types.completions.ChatCompletionChunk;
import com.alibaba.dashscope.types.completions.Delta;
import io.reactivex.Flowable;
Flowable<ChatCompletionChunk> stream = client.chat().completions().createStreaming(params);
// blockingForEach consumes chunks on the calling thread; subscribe(...) for reactive pipelines.
stream.blockingForEach(chunk -> {
Delta delta = chunk.delta();
if (delta != null && delta.getContent() != null) {
System.out.print(delta.getContent());
System.out.flush();
}
});
Lifecycle & executor¶
- Work runs on a daemon executor owned by the client; try-with-resources (
close()) shuts it down. Create one client and share it — do not build a client per call. - Inject your own executor with
DashScopeAsyncClient.builder().executor(...)to control threading; when injected, you own its lifecycle (the SDK will not shut it down). The same applies tohttpClient(...). See Configuration.
Sync vs async at a glance¶
DashScopeClient |
DashScopeAsyncClient |
|
|---|---|---|
| Unary | ChatCompletion create(params) |
CompletableFuture<ChatCompletion> create(params) |
| Streaming | ResponseStream<ChatCompletionChunk> (for-each, try-with-resources) |
Flowable<ChatCompletionChunk> (blockingForEach / subscribe) |
| Per-request options | (params, RequestOptions) overload |
same |
| Closing | try-with-resources | try-with-resources (also shuts down the owned executor) |