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

Fork a stored Live session over WebSocket. Send session.start with an overrides object (which may be empty), then wait for session.started. The model is inherited.

WebSocket wss://api.openai.com/v1/live/sessions/{session_id}/fork

Connect

import com.openai.client.okhttp.OkHttpClient;
import com.openai.core.ClientOptions;
import com.openai.helpers.LiveForkConnection;
import com.openai.models.live.*;
import com.openai.models.live.forks.ForkClientEvent;
import com.openai.models.live.forks.ForkServerEvent;
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 {
      // Use the ID of a stored, closed session whose recording has finalized.
      // Always release the transport, including on failure.
      try (LiveForkConnection connection = LiveForkConnection.connect(options, "sess_123")) {

        connection.send(
            ForkClientEvent.ofSessionStart(
                ForkSessionStartEvent.builder()
                    .session(ForkSessionConfig.builder().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.");
          }
          ForkServerEvent 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(ForkClientEvent.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();
    }
  }
}