Skip to content

☕ [ALPHA] Official Stagehand AI Browser Automation SDK for Java users. Built by Browserbase.com

License

Notifications You must be signed in to change notification settings

browserbase/stagehand-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stagehand Java API Library

Maven Central javadoc

The Stagehand Java SDK provides convenient access to the Stagehand REST API from applications written in Java.

The AI Browser Automation Framework
Read the Docs

MIT License Discord Community

browserbase%2Fstagehand | Trendshift

If you're looking for other languages, you can find them here

Vibe code Stagehand with Director Director

What is Stagehand?

The Stagehand Java SDK is similar to the Stagehand Kotlin SDK but with minor differences that make it more ergonomic for use in Java, such as Optional instead of nullable values, Stream instead of Sequence, and CompletableFuture instead of suspend functions.

It is generated with Stainless.

Stagehand is a browser automation framework used to control web browsers with natural language and code. By combining the power of AI with the precision of code, Stagehand makes web automation flexible, maintainable, and actually reliable.

Why Stagehand?

Most existing browser automation tools either require you to write low-level code in a framework like Selenium, Playwright, or Puppeteer, or use high-level agents that can be unpredictable in production. By letting developers choose what to write in code vs. natural language (and bridging the gap between the two) Stagehand is the natural choice for browser automations in production.

  1. Choose when to write code vs. natural language: use AI when you want to navigate unfamiliar pages, and use code when you know exactly what you want to do.

  2. Go from AI-driven to repeatable workflows: Stagehand lets you preview AI actions before running them, and also helps you easily cache repeatable actions to save time and tokens.

  3. Write once, run forever: Stagehand's auto-caching combined with self-healing remembers previous actions, runs without LLM inference, and knows when to involve AI whenever the website changes and your automation breaks.

Installation

Gradle

implementation("com.browserbase.api:stagehand-java:0.7.0")

Maven

<dependency>
  <groupId>com.browserbase.api</groupId>
  <artifactId>stagehand-java</artifactId>
  <version>0.7.0</version>
</dependency>

Requirements

This library requires Java 8 through Java 21. Java 22+ is not currently supported.

Running the Example

Examples live at:

  • stagehand-java-example/src/main/java/com/stagehand/api/example/Main.java
  • stagehand-java-example/src/main/java/com/stagehand/api/example/RemoteBrowserPlaywrightExample.java
  • stagehand-java-example/src/main/java/com/stagehand/api/example/LocalBrowserPlaywrightExample.java

Set your environment variables (from examples/.env.example):

  • STAGEHAND_API_URL
  • MODEL_API_KEY
  • BROWSERBASE_API_KEY
  • BROWSERBASE_PROJECT_ID
cp examples/.env.example examples/.env
# Edit examples/.env with your credentials.

The examples load examples/.env automatically.

Example dependencies:

  • Main.java: stagehand-java only
  • RemoteBrowserPlaywrightExample.java: Playwright for Java + Playwright browsers
  • LocalBrowserPlaywrightExample.java: Playwright for Java + Playwright browsers

Install Playwright for Java and its browsers before running the Playwright examples.

./gradlew :stagehand-java-example:run
./gradlew :stagehand-java-example:run -Pexample=RemoteBrowserPlaywright
./gradlew :stagehand-java-example:run -Pexample=LocalBrowserPlaywright

Usage

This mirrors stagehand-java-example/src/main/java/com/stagehand/api/example/RemoteBrowserPlaywrightExample.java.

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import com.browserbase.api.core.JsonValue;
import com.browserbase.api.core.http.StreamResponse;
import com.browserbase.api.models.sessions.*;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

import java.util.List;
import java.util.Map;

public class RemoteBrowserPlaywrightExample {
    public static void main(String[] args) {
        Env.load();
        StagehandClient client = StagehandOkHttpClient.builder().fromEnv().build();

        SessionStartResponse startResponse = client.sessions().start(
                SessionStartParams.builder()
                        .modelName("anthropic/claude-sonnet-4-6")
                        .browser(SessionStartParams.Browser.builder()
                                .type(SessionStartParams.Browser.Type.BROWSERBASE)
                                .build())
                        .build());

        String sessionId = startResponse.data().sessionId();
        String cdpUrl = startResponse.data().cdpUrl().orElse(null);
        if (cdpUrl == null || cdpUrl.isEmpty()) {
            throw new IllegalStateException("No cdpUrl returned for remote session.");
        }

        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().connectOverCDP(cdpUrl);
            BrowserContext context = browser.contexts().isEmpty() ? browser.newContext() : browser.contexts().get(0);
            Page page = context.pages().isEmpty() ? context.newPage() : context.pages().get(0);

            client.sessions().navigate(SessionNavigateParams.builder()
                    .id(sessionId)
                    .url("https://news.ycombinator.com")
                    .build());

            SessionObserveParams observeParams = SessionObserveParams.builder()
                    .id(sessionId)
                    .instruction("find the link to view comments for the top post")
                    .xStreamResponse(SessionObserveParams.XStreamResponse.TRUE)
                    .build();
            try (StreamResponse<StreamEvent> observeStream = client.sessions().observeStreaming(observeParams)) {
                observeStream.stream().forEach(event -> System.out.println("[observe] " + event.type()));
            }

            SessionActParams actParams = SessionActParams.builder()
                    .id(sessionId)
                    .input("Click the comments link for the top post")
                    .xStreamResponse(SessionActParams.XStreamResponse.TRUE)
                    .build();
            try (StreamResponse<StreamEvent> actStream = client.sessions().actStreaming(actParams)) {
                actStream.stream().forEach(event -> System.out.println("[act] " + event.type()));
            }

            SessionExtractParams.Schema schema = SessionExtractParams.Schema.builder()
                    .putAdditionalProperty("type", JsonValue.from("object"))
                    .putAdditionalProperty("properties", JsonValue.from(Map.of(
                            "commentText", Map.of("type", "string"),
                            "author", Map.of("type", "string")
                    )))
                    .putAdditionalProperty("required", JsonValue.from(List.of("commentText")))
                    .build();

            SessionExtractParams extractParams = SessionExtractParams.builder()
                    .id(sessionId)
                    .instruction("extract the text of the top comment on this page")
                    .schema(schema)
                    .xStreamResponse(SessionExtractParams.XStreamResponse.TRUE)
                    .build();
            try (StreamResponse<StreamEvent> extractStream = client.sessions().extractStreaming(extractParams)) {
                extractStream.stream().forEach(event -> System.out.println("[extract] " + event.type()));
            }

            SessionExecuteParams executeParams = SessionExecuteParams.builder()
                    .id(sessionId)
                    .executeOptions(SessionExecuteParams.ExecuteOptions.builder()
                            .instruction("Click the 'Learn more' link if available")
                            .maxSteps(3.0)
                            .build())
                    .agentConfig(SessionExecuteParams.AgentConfig.builder()
                            .model(ModelConfig.builder()
                                    .modelName("anthropic/claude-opus-4-6")
                                    .apiKey(System.getProperty("stagehand.modelApiKey"))
                                    .build())
                            .cua(false)
                            .build())
                    .xStreamResponse(SessionExecuteParams.XStreamResponse.TRUE)
                    .build();
            try (StreamResponse<StreamEvent> executeStream = client.sessions().executeStreaming(executeParams)) {
                executeStream.stream().forEach(event -> System.out.println("[execute] " + event.type()));
            }
        } finally {
            client.sessions().end(SessionEndParams.builder().id(sessionId).build());
        }
    }
}

Client configuration

Configure the client using system properties or environment variables:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
StagehandClient client = StagehandOkHttpClient.fromEnv();

Or manually:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

StagehandClient client = StagehandOkHttpClient.builder()
    .browserbaseApiKey("My Browserbase API Key")
    .browserbaseProjectId("My Browserbase Project ID")
    .modelApiKey("My Model API Key")
    .build();

Or using a combination of the two approaches:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

StagehandClient client = StagehandOkHttpClient.builder()
    // Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
    // Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
    .fromEnv()
    .browserbaseApiKey("My Browserbase API Key")
    .build();

See this table for the available options:

Setter System property Environment variable Required Default value
browserbaseApiKey stagehand.browserbaseApiKey BROWSERBASE_API_KEY true -
browserbaseProjectId stagehand.browserbaseProjectId BROWSERBASE_PROJECT_ID true -
modelApiKey stagehand.modelApiKey MODEL_API_KEY true -
baseUrl stagehand.baseUrl STAGEHAND_BASE_URL true "https://api.stagehand.browserbase.com"

System properties take precedence over environment variables.

Tip

Don't create more than one client in the same application. Each client has a connection pool and thread pools, which are more efficient to share between requests.

Modifying configuration

To temporarily use a modified client configuration, while reusing the same connection and thread pools, call withOptions() on any client or service:

import com.browserbase.api.client.StagehandClient;

StagehandClient clientWithOptions = client.withOptions(optionsBuilder -> {
    optionsBuilder.modelApiKey("sk-your-llm-api-key-here");
    optionsBuilder.maxRetries(42);
});

The withOptions() method does not affect the original client or service.

Requests and responses

To send a request to the Stagehand API, build an instance of some Params class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a Java class.

For example, client.sessions().act(...) should be called with an instance of SessionActParams, and it will return an instance of SessionActResponse.

Immutability

Each class in the SDK has an associated builder or factory method for constructing it.

Each class is immutable once constructed. If the class has an associated builder, then it has a toBuilder() method, which can be used to convert it back to a builder for making a modified copy.

Because each class is immutable, builder modification will never affect already built class instances.

Asynchronous execution

The default client is synchronous. To switch to asynchronous execution, call the async() method:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import com.browserbase.api.models.sessions.SessionActParams;
import com.browserbase.api.models.sessions.SessionActResponse;
import java.util.concurrent.CompletableFuture;

// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
StagehandClient client = StagehandOkHttpClient.fromEnv();

SessionActParams params = SessionActParams.builder()
    .id("00000000-your-session-id-000000000000")
    .input("click the first link on the page")
    .build();
CompletableFuture<SessionActResponse> response = client.async().sessions().act(params);

Or create an asynchronous client from the beginning:

import com.browserbase.api.client.StagehandClientAsync;
import com.browserbase.api.client.okhttp.StagehandOkHttpClientAsync;
import com.browserbase.api.models.sessions.SessionActParams;
import com.browserbase.api.models.sessions.SessionActResponse;
import java.util.concurrent.CompletableFuture;

// Configures using the `stagehand.browserbaseApiKey`, `stagehand.browserbaseProjectId`, `stagehand.modelApiKey` and `stagehand.baseUrl` system properties
// Or configures using the `BROWSERBASE_API_KEY`, `BROWSERBASE_PROJECT_ID`, `MODEL_API_KEY` and `STAGEHAND_BASE_URL` environment variables
StagehandClientAsync client = StagehandOkHttpClientAsync.fromEnv();

SessionActParams params = SessionActParams.builder()
    .id("00000000-your-session-id-000000000000")
    .input("click the first link on the page")
    .build();
CompletableFuture<SessionActResponse> response = client.sessions().act(params);

The asynchronous client supports the same options as the synchronous one, except most methods return CompletableFutures.

Streaming

The SDK defines methods that return response "chunk" streams, where each chunk can be individually processed as soon as it arrives instead of waiting on the full response. Streaming methods generally correspond to SSE or JSONL responses.

Some of these methods may have streaming and non-streaming variants, but a streaming method will always have a Streaming suffix in its name, even if it doesn't have a non-streaming variant.

These streaming methods return StreamResponse for synchronous clients:

import com.browserbase.api.core.http.StreamResponse;
import com.browserbase.api.models.sessions.StreamEvent;

try (StreamResponse<StreamEvent> streamResponse = client.sessions().actStreaming(params)) {
    streamResponse.stream().forEach(chunk -> {
        System.out.println(chunk);
    });
    System.out.println("No more chunks!");
}

Or AsyncStreamResponse for asynchronous clients:

import com.browserbase.api.core.http.AsyncStreamResponse;
import com.browserbase.api.models.sessions.StreamEvent;
import java.util.Optional;

client.async().sessions().actStreaming(params).subscribe(chunk -> {
    System.out.println(chunk);
});

// If you need to handle errors or completion of the stream
client.async().sessions().actStreaming(params).subscribe(new AsyncStreamResponse.Handler<>() {
    @Override
    public void onNext(StreamEvent chunk) {
        System.out.println(chunk);
    }

    @Override
    public void onComplete(Optional<Throwable> error) {
        if (error.isPresent()) {
            System.out.println("Something went wrong!");
            throw new RuntimeException(error.get());
        } else {
            System.out.println("No more chunks!");
        }
    }
});

// Or use futures
client.async().sessions().actStreaming(params)
    .subscribe(chunk -> {
        System.out.println(chunk);
    })
    .onCompleteFuture();
    .whenComplete((unused, error) -> {
        if (error != null) {
            System.out.println("Something went wrong!");
            throw new RuntimeException(error);
        } else {
            System.out.println("No more chunks!");
        }
    });

Async streaming uses a dedicated per-client cached thread pool Executor to stream without blocking the current thread. This default is suitable for most purposes.

To use a different Executor, configure the subscription using the executor parameter:

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Executor executor = Executors.newFixedThreadPool(4);
client.async().sessions().actStreaming(params).subscribe(
    chunk -> System.out.println(chunk), executor
);

Or configure the client globally using the streamHandlerExecutor method:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import java.util.concurrent.Executors;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    .streamHandlerExecutor(Executors.newFixedThreadPool(4))
    .build();

Raw responses

The SDK defines methods that deserialize responses into instances of Java classes. However, these methods don't provide access to the response headers, status code, or the raw response body.

To access this data, prefix any HTTP method call on a client or service with withRawResponse():

import com.browserbase.api.core.http.Headers;
import com.browserbase.api.core.http.HttpResponseFor;
import com.browserbase.api.models.sessions.SessionStartParams;
import com.browserbase.api.models.sessions.SessionStartResponse;

SessionStartParams params = SessionStartParams.builder()
    .modelName("anthropic/claude-sonnet-4-6")
    .build();
HttpResponseFor<SessionStartResponse> response = client.sessions().withRawResponse().start(params);

int statusCode = response.statusCode();
Headers headers = response.headers();

You can still deserialize the response into an instance of a Java class if needed:

import com.browserbase.api.models.sessions.SessionStartResponse;

SessionStartResponse parsedResponse = response.parse();

Error handling

The SDK throws custom unchecked exception types:

Logging

The SDK uses the standard OkHttp logging interceptor.

Enable logging by setting the STAGEHAND_LOG environment variable to info:

export STAGEHAND_LOG=info

Or to debug for more verbose logging:

export STAGEHAND_LOG=debug

ProGuard and R8

Although the SDK uses reflection, it is still usable with ProGuard and R8 because stagehand-java-core is published with a configuration file containing keep rules.

ProGuard and R8 should automatically detect and use the published rules, but you can also manually copy the keep rules if necessary.

Jackson

The SDK depends on Jackson for JSON serialization/deserialization. It is compatible with version 2.13.4 or higher, but depends on version 2.18.2 by default.

The SDK throws an exception if it detects an incompatible Jackson version at runtime (e.g. if the default version was overridden in your Maven or Gradle config).

If the SDK threw an exception, but you're certain the version is compatible, then disable the version check using the checkJacksonVersionCompatibility on StagehandOkHttpClient or StagehandOkHttpClientAsync.

Caution

We make no guarantee that the SDK works correctly when the Jackson version check is disabled.

Also note that there are bugs in older Jackson versions that can affect the SDK. We don't work around all Jackson bugs (example) and expect users to upgrade Jackson for those instead.

Network options

Retries

The SDK automatically retries 2 times by default, with a short exponential backoff between requests.

Only the following error types are retried:

  • Connection errors (for example, due to a network connectivity problem)
  • 408 Request Timeout
  • 409 Conflict
  • 429 Rate Limit
  • 5xx Internal

The API may also explicitly instruct the SDK to retry or not retry a request.

To set a custom number of retries, configure the client using the maxRetries method:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    .maxRetries(4)
    .build();

Timeouts

Requests time out after 1 minute by default.

To set a custom timeout, configure the method call using the timeout method:

import com.browserbase.api.models.sessions.SessionStartResponse;

SessionStartResponse response = client.sessions().start(
  params, RequestOptions.builder().timeout(Duration.ofSeconds(30)).build()
);

Or configure the default for all method calls at the client level:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import java.time.Duration;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    .timeout(Duration.ofSeconds(30))
    .build();

Proxies

To route requests through a proxy, configure the client using the proxy method:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import java.net.InetSocketAddress;
import java.net.Proxy;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    .proxy(new Proxy(
      Proxy.Type.HTTP, new InetSocketAddress(
        "https://example.com", 8080
      )
    ))
    .build();

Connection pooling

To customize the underlying OkHttp connection pool, configure the client using the maxIdleConnections and keepAliveDuration methods:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;
import java.time.Duration;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    // If `maxIdleConnections` is set, then `keepAliveDuration` must be set, and vice versa.
    .maxIdleConnections(10)
    .keepAliveDuration(Duration.ofMinutes(2))
    .build();

If both options are unset, OkHttp's default connection pool settings are used.

HTTPS

Note

Most applications should not call these methods, and instead use the system defaults. The defaults include special optimizations that can be lost if the implementations are modified.

To configure how HTTPS connections are secured, configure the client using the sslSocketFactory, trustManager, and hostnameVerifier methods:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    // If `sslSocketFactory` is set, then `trustManager` must be set, and vice versa.
    .sslSocketFactory(yourSSLSocketFactory)
    .trustManager(yourTrustManager)
    .hostnameVerifier(yourHostnameVerifier)
    .build();

Custom HTTP client

The SDK consists of three artifacts:

This structure allows replacing the SDK's default HTTP client without pulling in unnecessary dependencies.

Customized OkHttpClient

Tip

Try the available network options before replacing the default client.

To use a customized OkHttpClient:

  1. Replace your stagehand-java dependency with stagehand-java-core
  2. Copy stagehand-java-client-okhttp's OkHttpClient class into your code and customize it
  3. Construct StagehandClientImpl or StagehandClientAsyncImpl, similarly to StagehandOkHttpClient or StagehandOkHttpClientAsync, using your customized client

Completely custom HTTP client

To use a completely custom HTTP client:

  1. Replace your stagehand-java dependency with stagehand-java-core
  2. Write a class that implements the HttpClient interface
  3. Construct StagehandClientImpl or StagehandClientAsyncImpl, similarly to StagehandOkHttpClient or StagehandOkHttpClientAsync, using your new client class

Undocumented API functionality

The SDK is typed for convenient usage of the documented API. However, it also supports working with undocumented or not yet supported parts of the API.

Parameters

To set undocumented parameters, call the putAdditionalHeader, putAdditionalQueryParam, or putAdditionalBodyProperty methods on any Params class:

import com.browserbase.api.core.JsonValue;
import com.browserbase.api.models.sessions.SessionActParams;

SessionActParams params = SessionActParams.builder()
    .putAdditionalHeader("Secret-Header", "42")
    .putAdditionalQueryParam("secret_query_param", "42")
    .putAdditionalBodyProperty("secretProperty", JsonValue.from("42"))
    .build();

These can be accessed on the built object later using the _additionalHeaders(), _additionalQueryParams(), and _additionalBodyProperties() methods.

To set undocumented parameters on nested headers, query params, or body classes, call the putAdditionalProperty method on the nested class:

import com.browserbase.api.core.JsonValue;
import com.browserbase.api.models.sessions.SessionActParams;

SessionActParams params = SessionActParams.builder()
    .options(SessionActParams.Options.builder()
        .putAdditionalProperty("secretProperty", JsonValue.from("42"))
        .build())
    .build();

These properties can be accessed on the nested built object later using the _additionalProperties() method.

To set a documented parameter or property to an undocumented or not yet supported value, pass a JsonValue object to its setter:

import com.browserbase.api.core.JsonValue;
import com.browserbase.api.models.sessions.SessionActParams;

SessionActParams params = SessionActParams.builder()
    .input(JsonValue.from(42))
    .build();

The most straightforward way to create a JsonValue is using its from(...) method:

import com.browserbase.api.core.JsonValue;
import java.util.List;
import java.util.Map;

// Create primitive JSON values
JsonValue nullValue = JsonValue.from(null);
JsonValue booleanValue = JsonValue.from(true);
JsonValue numberValue = JsonValue.from(42);
JsonValue stringValue = JsonValue.from("Hello World!");

// Create a JSON array value equivalent to `["Hello", "World"]`
JsonValue arrayValue = JsonValue.from(List.of(
  "Hello", "World"
));

// Create a JSON object value equivalent to `{ "a": 1, "b": 2 }`
JsonValue objectValue = JsonValue.from(Map.of(
  "a", 1,
  "b", 2
));

// Create an arbitrarily nested JSON equivalent to:
// {
//   "a": [1, 2],
//   "b": [3, 4]
// }
JsonValue complexValue = JsonValue.from(Map.of(
  "a", List.of(
    1, 2
  ),
  "b", List.of(
    3, 4
  )
));

Normally a Builder class's build method will throw IllegalStateException if any required parameter or property is unset.

To forcibly omit a required parameter or property, pass JsonMissing:

import com.browserbase.api.core.JsonMissing;
import com.browserbase.api.models.sessions.SessionActParams;

SessionActParams params = SessionActParams.builder()
    .input("Click the login button")
    .id(JsonMissing.of())
    .build();

Response properties

To access undocumented response properties, call the _additionalProperties() method:

import com.browserbase.api.core.JsonValue;
import java.util.Map;

Map<String, JsonValue> additionalProperties = client.sessions().act(params)._additionalProperties();
JsonValue secretPropertyValue = additionalProperties.get("secretProperty");

String result = secretPropertyValue.accept(new JsonValue.Visitor<>() {
    @Override
    public String visitNull() {
        return "It's null!";
    }

    @Override
    public String visitBoolean(boolean value) {
        return "It's a boolean!";
    }

    @Override
    public String visitNumber(Number value) {
        return "It's a number!";
    }

    // Other methods include `visitMissing`, `visitString`, `visitArray`, and `visitObject`
    // The default implementation of each unimplemented method delegates to `visitDefault`, which throws by default, but can also be overridden
});

To access a property's raw JSON value, which may be undocumented, call its _ prefixed method:

import com.browserbase.api.core.JsonField;
import com.browserbase.api.models.sessions.SessionActParams;
import java.util.Optional;

JsonField<SessionActParams.Input> input = client.sessions().act(params)._input();

if (input.isMissing()) {
  // The property is absent from the JSON response
} else if (input.isNull()) {
  // The property was set to literal null
} else {
  // Check if value was provided as a string
  // Other methods include `asNumber()`, `asBoolean()`, etc.
  Optional<String> jsonString = input.asString();

  // Try to deserialize into a custom type
  MyClass myObject = input.asUnknown().orElseThrow().convert(MyClass.class);
}

Response validation

In rare cases, the API may return a response that doesn't match the expected type. For example, the SDK may expect a property to contain a String, but the API could return something else.

By default, the SDK will not throw an exception in this case. It will throw StagehandInvalidDataException only if you directly access the property.

If you would prefer to check that the response is completely well-typed upfront, then either call validate():

import com.browserbase.api.models.sessions.SessionActResponse;

SessionActResponse response = client.sessions().act(params).validate();

Or configure the method call to validate the response using the responseValidation method:

import com.browserbase.api.models.sessions.SessionActResponse;

SessionActResponse response = client.sessions().act(
  params, RequestOptions.builder().responseValidation(true).build()
);

Or configure the default for all method calls at the client level:

import com.browserbase.api.client.StagehandClient;
import com.browserbase.api.client.okhttp.StagehandOkHttpClient;

StagehandClient client = StagehandOkHttpClient.builder()
    .fromEnv()
    .responseValidation(true)
    .build();

FAQ

Why don't you use plain enum classes?

Java enum classes are not trivially forwards compatible. Using them in the SDK could cause runtime exceptions if the API is updated to respond with a new enum value.

Why do you represent fields using JsonField<T> instead of just plain T?

Using JsonField<T> enables a few features:

Why don't you use data classes?

It is not backwards compatible to add new fields to a data class and we don't want to introduce a breaking change every time we add a field to a class.

Why don't you use checked exceptions?

Checked exceptions are widely considered a mistake in the Java programming language. In fact, they were omitted from Kotlin for this reason.

Checked exceptions:

  • Are verbose to handle
  • Encourage error handling at the wrong level of abstraction, where nothing can be done about the error
  • Are tedious to propagate due to the function coloring problem
  • Don't play well with lambdas (also due to the function coloring problem)

Semantic versioning

This package generally follows SemVer conventions, though certain backwards-incompatible changes may be released as minor versions:

  1. Changes to library internals which are technically public but not intended or documented for external use. (Please open a GitHub issue to let us know if you are relying on such internals.)
  2. Changes that we do not expect to impact the vast majority of users in practice.

We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.

We are keen for your feedback; please open an issue with questions, bugs, or suggestions.

About

☕ [ALPHA] Official Stagehand AI Browser Automation SDK for Java users. Built by Browserbase.com

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 5

Languages