Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation

Connect

Connect to a persistent Responses API WebSocket. Send response.create events to create responses and response.steer events to queue user input containing text, images, or files for a continuation. Receive response stream events and steering acceptance, pending, or failure events over the socket.

WebSocket wss://api.openai.com/v1/responses

Connect

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.AsyncResponseConnection;
import com.openai.models.responses.ResponsesClientEvent;
import com.openai.models.responses.ResponsesServerEvent;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Main {
    public static void main(String[] args) throws Exception {
        OpenAIClient client = OpenAIOkHttpClient.fromEnv();
        try (AsyncResponseConnection connection = client.async().responses().connect().get()) {
            // One deadline for the exchange; repeated deltas must not extend it.
            long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
            connection.send(
                    ResponsesClientEvent.ofResponseCreate(
                            ResponsesClientEvent.ResponseCreate.builder()
                                    .model("gpt-6-astra")
                                    .input("Say hello.")
                                    .build()));
            while (true) {
                long remaining = deadline - System.nanoTime();
                if (remaining <= 0) {
                    throw new TimeoutException("Response did not complete within 60 seconds.");
                }
                ResponsesServerEvent event =
                        connection.receive().get(remaining, TimeUnit.NANOSECONDS);
                if (event.isResponseOutputTextDelta()) {
                    System.out.print(event.asResponseOutputTextDelta().delta());
                } else if (event.isResponseCompleted()) {
                    System.out.println();
                    return;
                } else if (event.isError()) {
                    throw new IllegalStateException(event.asError().error().message());
                } else if (event.isResponseFailed() || event.isResponseIncomplete()) {
                    throw new IllegalStateException("Response did not complete successfully.");
                }
            }
        } finally {
            client.close();
        }
    }
}