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
Theme

Connect

Connect to Live without query parameters. Send session.start with model and session configuration, then wait for session.started. Follow the Live prompting guide before writing instructions.

WebSocket wss://api.openai.com/v1/live/sessions

Connect

import com.openai.client.okhttp.OkHttpClient;
import com.openai.core.ClientOptions;
import com.openai.helpers.LiveConnection;
import com.openai.models.live.*;

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

class ConnectExample {
  public static void main(String[] args) throws Exception {
    ClientOptions options =
        ClientOptions.builder().fromEnv().httpClient(OkHttpClient.builder().build()).build();
    try {
      // New primary session; model belongs in session.start, not the URL.
      // Always release the transport, including on failure.
      try (LiveConnection connection = LiveConnection.connect(options)) {

        connection.send(
            ClientEvent.ofSessionStart(
                SessionStartEvent.builder()
                    .session(SessionConfig.builder().model("gpt-live-1").build())
                    .build()));
        boolean closeRequested = false;
        // One deadline for the exchange; unrelated events must not extend it.
        long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
        while (true) {
          long remaining = deadline - System.nanoTime();
          if (remaining <= 0) {
            throw new TimeoutException("Live event deadline expired after 60 seconds.");
          }
          ServerEvent event = connection.receiveAsync().get(remaining, TimeUnit.NANOSECONDS);
          if (event.isErrorEvent()) {
            throw new IllegalStateException("Live reported a session error.");
          } else if (event.isSessionStarted()) {
            // Wait for startup before closing the session this example owns.
            System.out.println(
                "Session ready: "
                    + event.asSessionStarted().session().id()
                    + " "
                    + event.asSessionStarted().session().model());
            connection.send(ClientEvent.ofSessionClose(SessionCloseEvent.builder().build()));
            closeRequested = true;
          } else if (event.isSessionClosed()) {
            if (!closeRequested
                || !event
                    .asSessionClosed()
                    .reason()
                    .equals(SessionClosedEvent.Reason.CLOSE_REQUESTED)) {
              throw new IllegalStateException(
                  "Session ended unexpectedly: "
                      + event.asSessionClosed().reason()
                      + "; usage="
                      + event.asSessionClosed().usage());
            }
            System.out.println("Final usage: " + event.asSessionClosed().usage());
            break;
          }
        }
      }
    } finally {
      options.close();
    }
  }
}