Skip to content

Commit 971177d

Browse files
jaycee-licopybara-github
authored andcommitted
feat: Add httpOptions field to type classes
PiperOrigin-RevId: 757957248
1 parent c184615 commit 971177d

14 files changed

+230
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Usage:
19+
*
20+
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
21+
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
22+
*
23+
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
24+
*
25+
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
26+
*
27+
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
28+
*
29+
* <p>1b. If you are using Gemini Developer AI, set an API key environment variable. You can find a
30+
* list of available API keys here: https://aistudio.google.com/app/apikey
31+
*
32+
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
33+
*
34+
* <p>2. Compile the java package and run the sample code.
35+
*
36+
* <p>mvn clean compile
37+
*
38+
* <p>mvn exec:java -Dexec.mainClass="com.google.genai.examples.RequestLevelHttpOptions"
39+
*/
40+
package com.google.genai.examples;
41+
42+
import com.google.common.collect.ImmutableMap;
43+
import com.google.genai.Client;
44+
import com.google.genai.types.GenerateContentConfig;
45+
import com.google.genai.types.GenerateContentResponse;
46+
import com.google.genai.types.HttpOptions;
47+
48+
/** An example of setting http options at request level. */
49+
public class RequestLevelHttpOptions {
50+
public static void main(String[] args) {
51+
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
52+
// key from the environment variable `GOOGLE_API_KEY`. Vertex AI API can be used by setting the
53+
// environment variables `GOOGLE_CLOUD_LOCATION` and `GOOGLE_CLOUD_PROJECT`, as well as setting
54+
// `GOOGLE_GENAI_USE_VERTEXAI` to "true".
55+
//
56+
// Do not set http options at client level.
57+
Client client = new Client();
58+
59+
if (client.vertexAI()) {
60+
System.out.println("Using Vertex AI");
61+
} else {
62+
System.out.println("Using Gemini Developer API");
63+
}
64+
65+
// Set a customized header per request config.
66+
GenerateContentConfig config =
67+
GenerateContentConfig.builder()
68+
.httpOptions(
69+
HttpOptions.builder().headers(ImmutableMap.of("my-header", "my-value")).build())
70+
.build();
71+
72+
GenerateContentResponse response =
73+
client.models.generateContent("gemini-2.0-flash-001", "Tell me the history of LLM", config);
74+
75+
System.out.println("Response: " + response.text());
76+
}
77+
}

src/main/java/com/google/genai/types/ComputeTokensConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919
package com.google.genai.types;
2020

2121
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
2223
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2324
import com.google.auto.value.AutoValue;
2425
import com.google.genai.JsonSerializable;
26+
import java.util.Optional;
2527

2628
/** Optional parameters for computing tokens. */
2729
@AutoValue
2830
@JsonDeserialize(builder = ComputeTokensConfig.Builder.class)
2931
public abstract class ComputeTokensConfig extends JsonSerializable {
32+
/** Used to override HTTP request options. */
33+
@JsonProperty("httpOptions")
34+
public abstract Optional<HttpOptions> httpOptions();
35+
3036
/** Instantiates a builder for ComputeTokensConfig. */
3137
public static Builder builder() {
3238
return new AutoValue_ComputeTokensConfig.Builder();
@@ -44,6 +50,9 @@ private static Builder create() {
4450
return new AutoValue_ComputeTokensConfig.Builder();
4551
}
4652

53+
@JsonProperty("httpOptions")
54+
public abstract Builder httpOptions(HttpOptions httpOptions);
55+
4756
public abstract ComputeTokensConfig build();
4857
}
4958

src/main/java/com/google/genai/types/CountTokensConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
@AutoValue
3131
@JsonDeserialize(builder = CountTokensConfig.Builder.class)
3232
public abstract class CountTokensConfig extends JsonSerializable {
33+
/** Used to override HTTP request options. */
34+
@JsonProperty("httpOptions")
35+
public abstract Optional<HttpOptions> httpOptions();
36+
3337
/** Instructions for the model to steer it toward better performance. */
3438
@JsonProperty("systemInstruction")
3539
public abstract Optional<Content> systemInstruction();
@@ -65,6 +69,9 @@ private static Builder create() {
6569
return new AutoValue_CountTokensConfig.Builder();
6670
}
6771

72+
@JsonProperty("httpOptions")
73+
public abstract Builder httpOptions(HttpOptions httpOptions);
74+
6875
@JsonProperty("systemInstruction")
6976
public abstract Builder systemInstruction(Content systemInstruction);
7077

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Auto-generated code. Do not edit.
18+
19+
package com.google.genai.types;
20+
21+
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.google.auto.value.AutoValue;
25+
import com.google.genai.JsonSerializable;
26+
import java.util.Optional;
27+
28+
/** Used to override the default configuration. */
29+
@AutoValue
30+
@JsonDeserialize(builder = DownloadFileConfig.Builder.class)
31+
public abstract class DownloadFileConfig extends JsonSerializable {
32+
/** Used to override HTTP request options. */
33+
@JsonProperty("httpOptions")
34+
public abstract Optional<HttpOptions> httpOptions();
35+
36+
/** Instantiates a builder for DownloadFileConfig. */
37+
public static Builder builder() {
38+
return new AutoValue_DownloadFileConfig.Builder();
39+
}
40+
41+
/** Creates a builder with the same values as this instance. */
42+
public abstract Builder toBuilder();
43+
44+
/** Builder for DownloadFileConfig. */
45+
@AutoValue.Builder
46+
public abstract static class Builder {
47+
/** For internal usage. Please use `DownloadFileConfig.builder()` for instantiation. */
48+
@JsonCreator
49+
private static Builder create() {
50+
return new AutoValue_DownloadFileConfig.Builder();
51+
}
52+
53+
@JsonProperty("httpOptions")
54+
public abstract Builder httpOptions(HttpOptions httpOptions);
55+
56+
public abstract DownloadFileConfig build();
57+
}
58+
59+
/** Deserializes a JSON string to a DownloadFileConfig object. */
60+
public static DownloadFileConfig fromJson(String jsonString) {
61+
return JsonSerializable.fromJsonString(jsonString, DownloadFileConfig.class);
62+
}
63+
}

src/main/java/com/google/genai/types/EditImageConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
@AutoValue
3131
@JsonDeserialize(builder = EditImageConfig.Builder.class)
3232
public abstract class EditImageConfig extends JsonSerializable {
33+
/** Used to override HTTP request options. */
34+
@JsonProperty("httpOptions")
35+
public abstract Optional<HttpOptions> httpOptions();
36+
3337
/** Cloud Storage URI used to store the generated images. */
3438
@JsonProperty("outputGcsUri")
3539
public abstract Optional<String> outputGcsUri();
@@ -121,6 +125,9 @@ private static Builder create() {
121125
return new AutoValue_EditImageConfig.Builder();
122126
}
123127

128+
@JsonProperty("httpOptions")
129+
public abstract Builder httpOptions(HttpOptions httpOptions);
130+
124131
@JsonProperty("outputGcsUri")
125132
public abstract Builder outputGcsUri(String outputGcsUri);
126133

src/main/java/com/google/genai/types/EmbedContentConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
@AutoValue
3030
@JsonDeserialize(builder = EmbedContentConfig.Builder.class)
3131
public abstract class EmbedContentConfig extends JsonSerializable {
32+
/** Used to override HTTP request options. */
33+
@JsonProperty("httpOptions")
34+
public abstract Optional<HttpOptions> httpOptions();
35+
3236
/** Type of task for which the embedding will be used. */
3337
@JsonProperty("taskType")
3438
public abstract Optional<String> taskType();
@@ -74,6 +78,9 @@ private static Builder create() {
7478
return new AutoValue_EmbedContentConfig.Builder();
7579
}
7680

81+
@JsonProperty("httpOptions")
82+
public abstract Builder httpOptions(HttpOptions httpOptions);
83+
7784
@JsonProperty("taskType")
7885
public abstract Builder taskType(String taskType);
7986

src/main/java/com/google/genai/types/FetchPredictOperationConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@
1919
package com.google.genai.types;
2020

2121
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
2223
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2324
import com.google.auto.value.AutoValue;
2425
import com.google.genai.JsonSerializable;
26+
import java.util.Optional;
2527

2628
/** None */
2729
@AutoValue
2830
@JsonDeserialize(builder = FetchPredictOperationConfig.Builder.class)
2931
public abstract class FetchPredictOperationConfig extends JsonSerializable {
32+
/** Used to override HTTP request options. */
33+
@JsonProperty("httpOptions")
34+
public abstract Optional<HttpOptions> httpOptions();
35+
3036
/** Instantiates a builder for FetchPredictOperationConfig. */
3137
public static Builder builder() {
3238
return new AutoValue_FetchPredictOperationConfig.Builder();
@@ -44,6 +50,9 @@ private static Builder create() {
4450
return new AutoValue_FetchPredictOperationConfig.Builder();
4551
}
4652

53+
@JsonProperty("httpOptions")
54+
public abstract Builder httpOptions(HttpOptions httpOptions);
55+
4756
public abstract FetchPredictOperationConfig build();
4857
}
4958

src/main/java/com/google/genai/types/GenerateContentConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
@AutoValue
3838
@JsonDeserialize(builder = GenerateContentConfig.Builder.class)
3939
public abstract class GenerateContentConfig extends JsonSerializable {
40+
/** Used to override HTTP request options. */
41+
@JsonProperty("httpOptions")
42+
public abstract Optional<HttpOptions> httpOptions();
43+
4044
/**
4145
* Instructions for the model to steer it toward better performance. For example, "Answer as
4246
* concisely as possible" or "Don't use technical terms in your response".
@@ -205,6 +209,9 @@ private static Builder create() {
205209
return new AutoValue_GenerateContentConfig.Builder();
206210
}
207211

212+
@JsonProperty("httpOptions")
213+
public abstract Builder httpOptions(HttpOptions httpOptions);
214+
208215
@JsonProperty("systemInstruction")
209216
public abstract Builder systemInstruction(Content systemInstruction);
210217

src/main/java/com/google/genai/types/GenerateImagesConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
@AutoValue
3131
@JsonDeserialize(builder = GenerateImagesConfig.Builder.class)
3232
public abstract class GenerateImagesConfig extends JsonSerializable {
33+
/** Used to override HTTP request options. */
34+
@JsonProperty("httpOptions")
35+
public abstract Optional<HttpOptions> httpOptions();
36+
3337
/** Cloud Storage URI used to store the generated images. */
3438
@JsonProperty("outputGcsUri")
3539
public abstract Optional<String> outputGcsUri();
@@ -118,6 +122,9 @@ private static Builder create() {
118122
return new AutoValue_GenerateImagesConfig.Builder();
119123
}
120124

125+
@JsonProperty("httpOptions")
126+
public abstract Builder httpOptions(HttpOptions httpOptions);
127+
121128
@JsonProperty("outputGcsUri")
122129
public abstract Builder outputGcsUri(String outputGcsUri);
123130

src/main/java/com/google/genai/types/GenerateVideosConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
@AutoValue
3030
@JsonDeserialize(builder = GenerateVideosConfig.Builder.class)
3131
public abstract class GenerateVideosConfig extends JsonSerializable {
32+
/** Used to override HTTP request options. */
33+
@JsonProperty("httpOptions")
34+
public abstract Optional<HttpOptions> httpOptions();
35+
3236
/** Number of output videos. */
3337
@JsonProperty("numberOfVideos")
3438
public abstract Optional<Integer> numberOfVideos();
@@ -102,6 +106,9 @@ private static Builder create() {
102106
return new AutoValue_GenerateVideosConfig.Builder();
103107
}
104108

109+
@JsonProperty("httpOptions")
110+
public abstract Builder httpOptions(HttpOptions httpOptions);
111+
105112
@JsonProperty("numberOfVideos")
106113
public abstract Builder numberOfVideos(Integer numberOfVideos);
107114

0 commit comments

Comments
 (0)