diff --git a/src/main/java/com/google/genai/ApiClient.java b/src/main/java/com/google/genai/ApiClient.java index d9f8b2f5575..f72a7e8af28 100644 --- a/src/main/java/com/google/genai/ApiClient.java +++ b/src/main/java/com/google/genai/ApiClient.java @@ -82,6 +82,7 @@ public abstract class ApiClient implements AutoCloseable { final OkHttpClient httpClient; HttpOptions httpOptions; final boolean vertexAI; + private final boolean defaultApiVersion; final Optional clientOptions; final Optional customBaseUrl; @@ -114,6 +115,9 @@ protected ApiClient( this.location = Optional.empty(); this.credentials = Optional.empty(); this.vertexAI = false; + this.defaultApiVersion = + computeIsDefaultApiVersion( + customHttpOptions, defaultEnvironmentVariables().get("apiVersion")); this.clientOptions = clientOptions; this.customBaseUrl = customHttpOptions.flatMap(HttpOptions::baseUrl).map(url -> url.replaceAll("/$", "")); @@ -273,6 +277,8 @@ protected ApiClient( this.httpOptions = mergeHttpOptions(customHttpOptions.get()); } this.vertexAI = true; + this.defaultApiVersion = + computeIsDefaultApiVersion(customHttpOptions, environmentVariables.get("apiVersion")); this.httpClient = createHttpClient(httpOptions, clientOptions); } @@ -651,11 +657,22 @@ static String libraryVersion() { return libraryLabel + " " + languageLabel; } + private static boolean computeIsDefaultApiVersion( + Optional customHttpOptions, @Nullable String envApiVersion) { + return !customHttpOptions.flatMap(HttpOptions::apiVersion).isPresent() + && (envApiVersion == null || envApiVersion.isEmpty()); + } + /** Returns whether the client is using Vertex AI APIs. */ public boolean vertexAI() { return vertexAI; } + /** Returns whether no API version was configured for the client. */ + public boolean isDefaultApiVersion() { + return defaultApiVersion; + } + /** Returns the project ID for Vertex AI APIs. */ public @Nullable String project() { return project.orElse(null); diff --git a/src/main/java/com/google/genai/Client.java b/src/main/java/com/google/genai/Client.java index 05218589cc5..6a1d2bd1c0b 100644 --- a/src/main/java/com/google/genai/Client.java +++ b/src/main/java/com/google/genai/Client.java @@ -174,7 +174,7 @@ public Client build() { asyncRetryScheduler); } - /** Sets the API key for Gemini API. */ + /** Sets the API key for the Gemini Developer API or the Gemini Enterprise API. */ @CanIgnoreReturnValue public Builder apiKey(String apiKey) { checkNotNull(apiKey, "apiKey cannot be null"); @@ -182,7 +182,7 @@ public Builder apiKey(String apiKey) { return this; } - /** Sets the project ID for Vertex AI APIs. */ + /** Sets the project ID for the Gemini Enterprise API. */ @CanIgnoreReturnValue public Builder project(String project) { checkNotNull(project, "project cannot be null"); @@ -190,7 +190,7 @@ public Builder project(String project) { return this; } - /** Sets the location for Vertex AI APIs. */ + /** Sets the location for the Gemini Enterprise API. */ @CanIgnoreReturnValue public Builder location(String location) { checkNotNull(location, "location cannot be null"); @@ -198,7 +198,7 @@ public Builder location(String location) { return this; } - /** Sets the {@link GoogleCredentials} for Vertex AI APIs. */ + /** Sets the {@link GoogleCredentials} for the Gemini Enterprise API. */ @CanIgnoreReturnValue public Builder credentials(GoogleCredentials credentials) { checkNotNull(credentials, "credentials cannot be null"); @@ -222,8 +222,9 @@ public Builder httpOptions(HttpOptions httpOptions) { return this; } - /** Sets whether to use Gemini Enterprise Agent Platform. - * When both enterprise and vertexAI are set, and they have different values, an IllegalArgumentException will be thrown. + /** + * Sets whether to use the Gemini Enterprise API. When both enterprise and vertexAI are set, and + * they have different values, an IllegalArgumentException will be thrown. */ @CanIgnoreReturnValue public Builder enterprise(boolean enterprise) { @@ -231,7 +232,7 @@ public Builder enterprise(boolean enterprise) { return this; } - /** Sets whether to use Vertex AI APIs. */ + /** Sets whether to use the Gemini Enterprise API. Prefer {@link #enterprise(boolean)}. */ @CanIgnoreReturnValue public Builder vertexAI(boolean vertexAI) { this.vertexAI = Optional.of(vertexAI); @@ -605,29 +606,41 @@ public void onResponse(Call call, Response okResponse) { }); } } + // android:strip_end - /** Returns whether the client is using Vertex AI APIs. */ + /** + * Returns whether the client is using the Gemini Enterprise API. Prefer {@link #enterprise()}. + */ public boolean vertexAI() { return apiClient.vertexAI(); } - /** Returns whether the client is using Gemini Enterprise Agent Platform. */ + /** Returns whether the client is using the Gemini Enterprise API. */ public boolean enterprise() { return apiClient.vertexAI(); } - /** Returns the project ID for Vertex AI APIs. */ + /** + * Returns {@code true} if no API version was configured via {@link HttpOptions#apiVersion()} or + * the {@code GOOGLE_GENAI_API_VERSION} environment variable, so the client is using the SDK's + * default API version for its backend. + */ + public boolean isDefaultApiVersion() { + return apiClient.isDefaultApiVersion(); + } + + /** Returns the project ID for the Gemini Enterprise API. */ public String project() { return apiClient.project(); } - /** Returns the location for Vertex AI APIs. */ + /** Returns the location for the Gemini Enterprise API. */ public String location() { return apiClient.location(); } - /** Returns the API key for Gemini API. */ + /** Returns the API key for the Gemini Developer API or the Gemini Enterprise API. */ public String apiKey() { return apiClient.apiKey(); } @@ -673,7 +686,7 @@ public void close() { } /** - * Overrides the base URLs for the Gemini API and/or Vertex AI API. + * Overrides the base URLs for the Gemini Developer API and/or the Gemini Enterprise API. * *

Note: This function should be called before initializing the SDK. If the base URLs are set * after initializing the SDK, the base URLs will not be updated. diff --git a/src/test/java/com/google/genai/ClientTest.java b/src/test/java/com/google/genai/ClientTest.java index c8a4a7ee5c8..48da846a514 100644 --- a/src/test/java/com/google/genai/ClientTest.java +++ b/src/test/java/com/google/genai/ClientTest.java @@ -550,6 +550,162 @@ void explicitApiVersion_overridesEnvVar_withoutThrowing() { } } + @Test + void isDefaultApiVersion_trueWhenUnconfigured() { + try (MockedStatic mocked = + Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) { + mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of()); + + Client mldevClient = Client.builder().apiKey(API_KEY).enterprise(false).build(); + assertTrue(mldevClient.isDefaultApiVersion()); + + Client enterpriseClient = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .build(); + assertTrue(enterpriseClient.isDefaultApiVersion()); + + HttpOptions nonVersionOptions = + HttpOptions.builder() + .baseUrl("https://custom.example.com") + .headers(ImmutableMap.of("X-Test", "1")) + .timeout(5000) + .build(); + Client mldevWithNonVersionOptions = + Client.builder().apiKey(API_KEY).enterprise(false).httpOptions(nonVersionOptions).build(); + assertTrue(mldevWithNonVersionOptions.isDefaultApiVersion()); + + Client enterpriseWithNonVersionOptions = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .httpOptions(nonVersionOptions) + .build(); + assertTrue(enterpriseWithNonVersionOptions.isDefaultApiVersion()); + } + } + + @Test + void isDefaultApiVersion_falseWhenHttpOptionsApiVersionSet() { + try (MockedStatic mocked = + Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) { + mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of()); + + for (String version : + new String[] {ApiClient.GOOGLE_AI_API_DEFAULT_VERSION, "v1", "v1beta", "v1alpha", ""}) { + Client mldevClient = + Client.builder() + .apiKey(API_KEY) + .enterprise(false) + .httpOptions(HttpOptions.builder().apiVersion(version).build()) + .build(); + assertFalse(mldevClient.isDefaultApiVersion(), "version=" + version); + } + + for (String version : new String[] {"v1beta1", "v1", ""}) { + Client enterpriseClient = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .httpOptions(HttpOptions.builder().apiVersion(version).build()) + .build(); + assertFalse(enterpriseClient.isDefaultApiVersion(), "version=" + version); + } + } + } + + @Test + void isDefaultApiVersion_envVarHandling() { + try (MockedStatic mocked = + Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) { + HttpOptions nonVersionOptions = + HttpOptions.builder() + .baseUrl("https://custom.example.com") + .headers(ImmutableMap.of("X-Test", "1")) + .timeout(5000) + .build(); + for (String envVersion : new String[] {"v1", "v1alpha", "beta"}) { + mocked + .when(ApiClient::defaultEnvironmentVariables) + .thenReturn(ImmutableMap.of("apiVersion", envVersion)); + + Client mldevClient = Client.builder().apiKey(API_KEY).enterprise(false).build(); + assertFalse(mldevClient.isDefaultApiVersion(), "envVersion=" + envVersion); + + Client enterpriseClient = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .build(); + assertFalse(enterpriseClient.isDefaultApiVersion(), "envVersion=" + envVersion); + + Client mldevWithNonVersionOptions = + Client.builder() + .apiKey(API_KEY) + .enterprise(false) + .httpOptions(nonVersionOptions) + .build(); + assertFalse(mldevWithNonVersionOptions.isDefaultApiVersion(), "envVersion=" + envVersion); + + Client enterpriseWithNonVersionOptions = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .httpOptions(nonVersionOptions) + .build(); + assertFalse( + enterpriseWithNonVersionOptions.isDefaultApiVersion(), "envVersion=" + envVersion); + } + + mocked + .when(ApiClient::defaultEnvironmentVariables) + .thenReturn(ImmutableMap.of("googleApiKey", API_KEY, "apiVersion", "")); + Client emptyEnvClient = new Client(); + assertTrue(emptyEnvClient.isDefaultApiVersion()); + + Client emptyEnvEnterpriseClient = + Client.builder() + .project(PROJECT) + .location(LOCATION) + .credentials(CREDENTIALS) + .enterprise(true) + .build(); + assertTrue(emptyEnvEnterpriseClient.isDefaultApiVersion()); + } + } + + @Test + void isDefaultApiVersion_replayClient() { + try (MockedStatic mocked = + Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) { + mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of()); + + Client defaultReplay = + Client.builder().apiKey(API_KEY).enterprise(false).debugConfig(DEBUG_CONFIG).build(); + assertTrue(defaultReplay.isDefaultApiVersion()); + + Client configuredReplay = + Client.builder() + .apiKey(API_KEY) + .enterprise(false) + .debugConfig(DEBUG_CONFIG) + .httpOptions(HttpOptions.builder().apiVersion("v1alpha").build()) + .build(); + assertFalse(configuredReplay.isDefaultApiVersion()); + } + } + static class TestApiClient extends ApiClient { TestApiClient(HttpOptions httpOptions) { super(Optional.of("dummy-api-key"), Optional.of(httpOptions), Optional.empty());