Google Gen AI Java SDK

repository·main·Indexed 18 days ago

https://github.com/googleapis/java-genai

An idiomatic Java library for interacting with the Gemini Developer APIs and the Gemini Enterprise Agent Platform. It supports content generation with Gemini models, multimodal inputs, Automatic Function Calling (AFC), and image/video generation via Imagen and Veo. The SDK includes features for managing media files, handling streaming and asynchronous responses, and managing chat history through the ChatBase class.

Tokens
6.2K
Snippets
15
Records
19
Agent score
64%

What's inside google-genai

  1. Add the Google Gen AI Java SDK dependency

    main

    To use the Google Gen AI Java SDK in your project, add the following dependency to your pom.xml file if you are using Maven.

    <dependencies>
      <dependency>
        <groupId>com.google.genai</groupId>
        <artifactId>google-genai</artifactId>
        <version>1.64.0</version>
      </dependency>
    </dependencies>
  2. Use Automatic Function Calling (AFC)

    main

    The SDK supports Automatic Function Calling. When you provide a list of methods in the tools list of GenerateContentConfig, the SDK can automatically execute these methods on the client side.

    Requirement: You must enable the -parameters compiler argument in your pom.xml so the compiler preserves parameter names, which the SDK uses for reflection.

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.14.0</version>
      <configuration>
        <compilerArgs>
          <arg>-parameters</arg>
        </compilerArgs>
      </configuration>
    </plugin>

    Usage:

    1. Define a public static method.
    2. Extract the java.lang.reflect.Method object.
    3. Pass it to Tool.builder().functions(method).
    import com.google.genai.Client;
    import com.google.genai.types.GenerateContentConfig;
    import com.google.genai.types.Tool;
    import java.lang.reflect.Method;
    
    public static String getCurrentWeather(String location, String unit) {
        return "The weather in " + location + " is very nice.";
    }
    
    // In main:
    Method method = GenerateContentWithFunctionCall.class.getMethod("getCurrentWeather", String.class, String.class);
    GenerateContentConfig config = GenerateContentConfig.builder()
        .tools(Tool.builder().functions(method))
        .build();
    
    client.models.generateContent("gemini-2.5-flash", "What is the weather in Vancouver?", config);
  3. Instantiate a Client

    main

    The Client class is the primary entry point for interacting with the Gemini API and the Gemini Enterprise Agent Platform API. You can instantiate it using a builder pattern or by relying on environment variables.

    Gemini Developer API

    Use an API key to access the Gemini Developer backend:

    Client client = Client.builder().apiKey("your-api-key").build();

    Gemini Enterprise Agent Platform API

    To use the Enterprise backend, you must set the enterprise(true) flag. You can authenticate using project/location details or an API key (Express Mode).

    Using Project and Location:

    Client client = Client.builder()
      .project("your-project")
      .location("your-location")
      .enterprise(true)
      .build();

    Using API Key (Express Mode):

    Client client = Client.builder()
      .apiKey("your-api-key")
      .enterprise(true)
      .build();

    Using Environment Variables

    You can instantiate a client using new Client() if the following environment variables are set:

    For Gemini Developer API:

    • GOOGLE_API_KEY: Recommended. (GEMINI_API_KEY is legacy and takes lower precedence).

    For Gemini Enterprise Agent Platform:

    • GOOGLE_GENAI_USE_ENTERPRISE=true
    • GOOGLE_CLOUD_PROJECT: Your project ID.
    • GOOGLE_CLOUD_LOCATION: Your location (e.g., us-central1).
    • GOOGLE_API_KEY: For Express Mode.
    import com.google.genai.Client;
    
    // Gemini Developer API
    Client client = Client.builder().apiKey("your-api-key").build();
    
    // Gemini Enterprise Agent Platform
    Client client = Client.builder()
      .project("your-project")
      .location("your-location")
      .enterprise(true)
      .build();
  4. How chat history management works in ChatBase

    main

    The ChatBase class manages the state of a conversation through two distinct history lists: comprehensiveHistory and curatedHistory.

    History Types

    1. Comprehensive History: A complete log of all interactions. It is used to ensure no data is lost, even if a model response is flagged or fails validation.
    2. Curated History: A cleaned version of the conversation used to construct subsequent requests to the model. It only includes messages that follow the required protocol (e.g., alternating 'user' and 'model' roles) and contain valid content.

    Validation Rules

    To maintain a valid conversation state, the SDK enforces several rules during history recording:

    • Role Alternation: The first message in a history must have the role user. Subsequent messages must alternate between user and model.
    • Content Integrity: Every Content object must contain non-empty Part objects.
    • Finish Reason: If a model response's finishReason() indicates an unexpected termination, the message is added to the comprehensiveHistory but is excluded from the curatedHistory to prevent sending invalid context back to the model in future turns.
  5. Configure ClientOptions (Connection Pool and Proxy)

    main

    ClientOptions allows for low-level customization of the underlying HTTP client.

    Connection Pool

    Configure maxConnections (total) and maxConnectionsPerHost (per host).

    Proxy Configuration

    Use ProxyOptions to configure HTTP, SOCKS, or DIRECT connections. DIRECT bypasses system-level proxy settings.

    Custom OkHttpClient

    For advanced control (interceptors, custom SSL, etc.), you can provide your own OkHttpClient instance. The SDK will clone it to preserve your settings while appending its internal RetryInterceptor.

    import com.google.genai.Client;
    import com.google.genai.types.ClientOptions;
    import com.google.genai.types.ProxyOptions;
    import com.google.genai.types.ProxyType;
    
    // Proxy Example
    ClientOptions clientOptions = ClientOptions.builder()
        .proxyOptions(
            ProxyOptions.builder()
                .type(ProxyType.Known.HTTP)
                .host("your-proxy-host")
                .port(8080)
                .username("your-proxy-username")
                .password("your-proxy-password"))
        .build();
    Client client = Client.builder().apiKey("your-api-key").clientOptions(clientOptions).build();
  6. Configure HttpRetryOptions

    main

    Use HttpRetryOptions within HttpOptions to manage automatic retries for failed API calls. You can customize the total number of attempts, specific HTTP status codes that trigger a retry (e.g., 429 for rate limits), and the backoff strategy.

    Note: Providing HttpRetryOptions on a per-request basis will completely override any default retry settings configured at the client level.

    import com.google.genai.types.HttpOptions;
    import com.google.genai.types.HttpRetryOptions;
    
    HttpOptions httpOptions = HttpOptions.builder()
      .retryOptions(
          HttpRetryOptions.builder()
              .attempts(3)
              .httpStatusCodes(408, 429))
      .build();
  7. Configure API Version and HttpOptions

    main

    The SDK defaults to beta API endpoints. To use stable endpoints (v1) or specific preview versions (v1alpha), use HttpOptions via the Client.builder().

    Set API Version

    • Stable (v1) for Enterprise:
      .httpOptions(HttpOptions.builder().apiVersion("v1"))
    • Preview (v1alpha) for Developer API:
      .httpOptions(HttpOptions.builder().apiVersion("v1alpha"))

    Customize HTTP Parameters

    HttpOptions allows customization of baseUrl, headers, and timeout. These can be set at the client level or on a per-request basis.

    import com.google.genai.Client;
    import com.google.genai.types.HttpOptions;
    
    Client client = Client.builder()
      .project("your-project")
      .location("your-location")
      .enterprise(true)
      .httpOptions(HttpOptions.builder().apiVersion("v1"))
      .build();
  8. Switch between Synchronous and Asynchronous APIs

    main

    The Client provides access to two distinct interaction patterns:

    1. Synchronous API: Accessed directly via properties on the Client instance (e.g., client.models, client.chats, client.files). Use these for standard blocking calls.
    2. Asynchronous API: Accessed via the client.async property. This provides asynchronous versions of the same services (e.g., client.async.models, client.async.chats). Use these for non-blocking operations.

    Available Service Groups (both Sync and Async):

    • models: Model management and interaction.
    • batches: Batch processing.
    • caches: Context caching.
    • operations: Long-running operations.
    • chats: Chat sessions.
    • files: File management.
    • tunings: Model tuning.
    • authTokens: Authentication tokens.
    • fileSearchStores: File search storage.
    • live: Live streaming capabilities (Async only).
    • elements: (Note: Async includes AsyncLive, while Client includes Live via Async mapping).
    // Synchronous usage
    client.models.generateContent(...);
    
    // Asynchronous usage
    client.async.models.generateContent(...);
  9. Initialize the GenAI Client

    main

    The Client class is the primary entrypoint for interacting with Gemini APIs. It is thread-safe and implements AutoCloseable, so it should be used within a try-with-resources block to ensure the underlying HTTP client is closed properly.

    You can initialize a client in two ways:

    1. Default Constructor: Uses environment variables for configuration (e.g., apiKey, project, location).
    2. Builder Pattern: Provides explicit control over authentication and API selection (Gemini API vs. Vertex AI).

    Important Constraints:

    • Gemini API: Requires an apiKey. It does not support project or location settings.
    • Vertex AI: Requires project, location, and GoogleCredentials. It does not support apiKey.
    • Setting both apiKey and project/location will throw an IllegalArgumentException.
    // Using the Builder for Gemini API
    try (Client client = Client.builder()
            .apiKey("YOUR_API_KEY")
            .build()) {
        // Use client here
    }
    
    // Using the Builder for Vertex AI
    try (Client client = Client.builder()
            .project("your-project-id")
            .location("us-central1")
            .credentials(googleCredentials)
            .vertexAI(true)
            .build()) {
        // Use client here
    }
  10. Important migration and compatibility warnings

    main

    Automatic Function Calling (AFC) Changes

    In the upcoming major version (2.0.0+), Automatic Function Calling (AFC) behavior is changing. You will no longer be able to invoke AFC via direct calls to Models.generate_content or its stream/async variants. Instead, you must invoke AFC through Chats modules.

    Deprecated Arguments

    In Models.generate_videos (and its async variants), the prompt, text, and image arguments are being replaced. You should use the source argument instead.

    Java Version Requirements

    Starting from SDK version 2.0.0, Java 17 or later is required. To avoid breaking changes if you are on an older Java version, pin your SDK version to < 2.0.0.

  11. Stream, Async, and JSON Responses

    main

    Streaming Responses

    Use generateContentStream to receive responses incrementally. The method returns a ResponseStream which should be closed to avoid connection leaks.

    ResponseStream<GenerateContentResponse> stream = client.models.generateContentStream("gemini-2.5-flash", "Tell a story", null);
    for (GenerateContentResponse res : stream) {
        System.out.print(res.text());
    }
    stream.close();

    Asynchronous Generation

    Use the client.async.models namespace to get a CompletableFuture.

    CompletableFuture<GenerateContentResponse> future = client.async.models.generateContent("gemini-2.5-flash", "Hi", null);
    future.thenAccept(res -> System.out.println(res.text())).join();

    JSON Response Schema

    To force a JSON response, set responseMimeType to application/json and provide a responseSchema (a JSON-formatted map) in the GenerateContentConfig.

  12. Manage Files with the Files API

    main

    The Files API allows you to upload and manage media files (text, images, audio) for use with Gemini models. Note: This feature is exclusively supported by the Gemini API.

    • Capacity: Up to 20 GB per project; max 2 GB per file.
    • Retention: Files are stored for 48 hours and then automatically deleted.
    • Usage: Always use the Files API for requests where the total size (including prompt and instructions) exceeds 20 MB.

    Operations:

    • upload: Upload a media file.
    • get: Retrieve file metadata.
    • list: List uploaded files.
    • delete: Manually delete a file.
    import com.google.genai.Client;
    import com.google.genai.types.File;
    import com.google.genai.types.UploadFileConfig;
    
    Client client = new Client();
    File file = client.files.upload("path/to/file.pdf", UploadFileConfig.builder().mimeType("application/pdf").build());
    
    // Use the file in a prompt
    Content content = Content.fromParts(
        Part.fromText("Summary this pdf."),
        Part.fromUri(file.name().get(), file.mimeType().get()));
    client.models.generateContent("gemini-2.5-flash", content, null);