Skip to content

Commit 873ad39

Browse files
google-genai-botcopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 743353678
1 parent e452491 commit 873ad39

22 files changed

+821
-300
lines changed

src/main/java/com/google/genai/ApiClient.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.auth.oauth2.GoogleCredentials;
2222
import com.google.common.collect.ImmutableMap;
23+
import com.google.genai.errors.GenAiIOException;
2324
import com.google.genai.types.HttpOptions;
2425
import java.io.IOException;
2526
import java.util.Map;
@@ -103,13 +104,7 @@ abstract class ApiClient {
103104
throw new IllegalArgumentException("Location must not be empty.");
104105
}
105106

106-
try {
107-
this.credentials = Optional.of(credentials.orElse(defaultCredentials()));
108-
} catch (IOException e) {
109-
throw new IllegalArgumentException(
110-
"Failed to get application default credentials, please explicitly provide credentials.",
111-
e);
112-
}
107+
this.credentials = Optional.of(credentials.orElse(defaultCredentials()));
113108

114109
this.httpOptions = defaultHttpOptions(/* vertexAI= */ true, this.location);
115110

@@ -133,7 +128,7 @@ private CloseableHttpClient createHttpClient(Optional<Integer> timeout) {
133128
}
134129

135130
/** Sends a Http Post request given the path and request json string. */
136-
public abstract ApiResponse post(String path, String requestJson) throws IOException;
131+
public abstract ApiResponse post(String path, String requestJson);
137132

138133
/** Returns the library version. */
139134
static String libraryVersion() {
@@ -216,8 +211,14 @@ static HttpOptions defaultHttpOptions(boolean vertexAI, Optional<String> locatio
216211
return defaultHttpOptionsBuilder.build();
217212
}
218213

219-
GoogleCredentials defaultCredentials() throws IOException {
220-
return GoogleCredentials.getApplicationDefault()
221-
.createScoped("https://www.googleapis.com/auth/cloud-platform");
214+
GoogleCredentials defaultCredentials() {
215+
try {
216+
return GoogleCredentials.getApplicationDefault()
217+
.createScoped("https://www.googleapis.com/auth/cloud-platform");
218+
} catch (IOException e) {
219+
throw new GenAiIOException(
220+
"Failed to get application default credentials, please explicitly provide credentials.",
221+
e);
222+
}
222223
}
223224
}

src/main/java/com/google/genai/ApiResponse.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package com.google.genai;
1818

19-
import java.io.IOException;
2019
import org.apache.http.HttpEntity;
21-
import org.apache.http.HttpException;
2220

2321
/** The API response contains a response to a call to the GenAI APIs. */
2422
public abstract class ApiResponse implements AutoCloseable {
2523
/** Gets the HttpEntity. */
26-
public abstract HttpEntity getEntity() throws HttpException;
24+
public abstract HttpEntity getEntity();
2725

28-
public abstract void close() throws IOException;
26+
@Override
27+
public abstract void close();
2928
}

src/main/java/com/google/genai/AsyncLive.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.fasterxml.jackson.databind.node.ObjectNode;
2222
import com.google.auth.oauth2.GoogleCredentials;
23+
import com.google.genai.errors.GenAiIOException;
2324
import com.google.genai.types.LiveClientSetup;
2425
import com.google.genai.types.LiveConnectConfig;
2526
import com.google.genai.types.LiveServerMessage;
@@ -120,7 +121,7 @@ private Map<String, String> getWebSocketHeaders() {
120121
credentials.refreshIfExpired();
121122
headers.put("Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
122123
} catch (IOException e) {
123-
throw new IllegalStateException("Failed to get credentials for Vertex AI.", e);
124+
throw new GenAiIOException("Failed to refresh credentials for Vertex AI.", e);
124125
}
125126
}
126127
return headers;
@@ -230,7 +231,7 @@ public void onClose(int code, String reason, boolean remote) {
230231
System.out.println("Live session closed with code: " + code + " and reason: " + reason);
231232
if (!sessionFuture.isDone()) {
232233
sessionFuture.completeExceptionally(
233-
new IOException("WebSocket closed unexpectedly: " + reason));
234+
new GenAiIOException("WebSocket closed unexpectedly: " + reason));
234235
}
235236
}
236237

src/main/java/com/google/genai/AsyncModels.java

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131
import com.google.genai.types.Image;
3232
import com.google.genai.types.UpscaleImageConfig;
3333
import com.google.genai.types.UpscaleImageResponse;
34-
import java.io.IOException;
3534
import java.util.List;
3635
import java.util.concurrent.CompletableFuture;
37-
import org.apache.http.HttpException;
3836

3937
public final class AsyncModels {
4038
Models models;
@@ -46,14 +44,7 @@ public AsyncModels(ApiClient apiClient) {
4644
@BetaApi
4745
public CompletableFuture<GenerateVideosOperation> generateVideos(
4846
String model, String prompt, Image image, GenerateVideosConfig config) {
49-
return CompletableFuture.supplyAsync(
50-
() -> {
51-
try {
52-
return models.generateVideos(model, prompt, image, config);
53-
} catch (IOException | HttpException e) {
54-
throw new RuntimeException(e);
55-
}
56-
});
47+
return CompletableFuture.supplyAsync(() -> models.generateVideos(model, prompt, image, config));
5748
}
5849

5950
/**
@@ -65,19 +56,10 @@ public CompletableFuture<GenerateVideosOperation> generateVideos(
6556
* the optional configurations
6657
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
6758
* response contents and other metadata
68-
* @throws IOException if an I/O error occurs while making the API call
69-
* @throws HttpException if an HTTP error occurs while making the API call
7059
*/
7160
public CompletableFuture<GenerateContentResponse> generateContent(
7261
String model, List<Content> contents, GenerateContentConfig config) {
73-
return CompletableFuture.supplyAsync(
74-
() -> {
75-
try {
76-
return models.generateContent(model, contents, config);
77-
} catch (IOException | HttpException e) {
78-
throw new RuntimeException(e);
79-
}
80-
});
62+
return CompletableFuture.supplyAsync(() -> models.generateContent(model, contents, config));
8163
}
8264

8365
/**
@@ -89,12 +71,9 @@ public CompletableFuture<GenerateContentResponse> generateContent(
8971
* the optional configurations
9072
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
9173
* response contents and other metadata
92-
* @throws IOException if an I/O error occurs while making the API call
93-
* @throws HttpException if an HTTP error occurs while making the API call
9474
*/
9575
public CompletableFuture<GenerateContentResponse> generateContent(
96-
String model, Content content, GenerateContentConfig config)
97-
throws IOException, HttpException {
76+
String model, Content content, GenerateContentConfig config) {
9877
return generateContent(model, Transformers.tContents(null, (Object) content), config);
9978
}
10079

@@ -107,11 +86,9 @@ public CompletableFuture<GenerateContentResponse> generateContent(
10786
* the optional configurations
10887
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
10988
* response contents and other metadata
110-
* @throws IOException if an I/O error occurs while making the API call
111-
* @throws HttpException if an HTTP error occurs while making the API call
11289
*/
11390
public CompletableFuture<GenerateContentResponse> generateContent(
114-
String model, String text, GenerateContentConfig config) throws IOException, HttpException {
91+
String model, String text, GenerateContentConfig config) {
11592
return generateContent(model, Transformers.tContents(null, (Object) text), config);
11693
}
11794

@@ -125,19 +102,11 @@ public CompletableFuture<GenerateContentResponse> generateContent(
125102
* the optional configurations
126103
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
127104
* response contents and other metadata
128-
* @throws IOException if an I/O error occurs while making the API call
129-
* @throws HttpException if an HTTP error occurs while making the API call
130105
*/
131106
public CompletableFuture<ResponseStream<GenerateContentResponse>> generateContentStream(
132107
String model, List<Content> contents, GenerateContentConfig config) {
133108
return CompletableFuture.supplyAsync(
134-
() -> {
135-
try {
136-
return models.generateContentStream(model, contents, config);
137-
} catch (IOException | HttpException e) {
138-
throw new RuntimeException(e);
139-
}
140-
});
109+
() -> models.generateContentStream(model, contents, config));
141110
}
142111

143112
/**
@@ -150,12 +119,9 @@ public CompletableFuture<ResponseStream<GenerateContentResponse>> generateConten
150119
* the optional configurations
151120
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
152121
* response contents and other metadata
153-
* @throws IOException if an I/O error occurs while making the API call
154-
* @throws HttpException if an HTTP error occurs while making the API call
155122
*/
156123
public CompletableFuture<ResponseStream<GenerateContentResponse>> generateContentStream(
157-
String model, Content content, GenerateContentConfig config)
158-
throws IOException, HttpException {
124+
String model, Content content, GenerateContentConfig config) {
159125
return generateContentStream(model, Transformers.tContents(null, (Object) content), config);
160126
}
161127

@@ -168,11 +134,9 @@ public CompletableFuture<ResponseStream<GenerateContentResponse>> generateConten
168134
* the optional configurations
169135
* @return a {@link com.google.genai.types.GenerateContentResponse} instance that contains
170136
* response contents and other metadata
171-
* @throws IOException if an I/O error occurs while making the API call
172-
* @throws HttpException if an HTTP error occurs while making the API call
173137
*/
174138
public CompletableFuture<ResponseStream<GenerateContentResponse>> generateContentStream(
175-
String model, String text, GenerateContentConfig config) throws IOException, HttpException {
139+
String model, String text, GenerateContentConfig config) {
176140
return generateContentStream(model, Transformers.tContents(null, (Object) text), config);
177141
}
178142

@@ -185,19 +149,10 @@ public CompletableFuture<ResponseStream<GenerateContentResponse>> generateConten
185149
* optional configurations
186150
* @return a {@link com.google.genai.types.GenerateImagesResponse} instance that contains the
187151
* generated images.
188-
* @throws IOException if an I/O error occurs while making the API call
189-
* @throws HttpException if an HTTP error occurs while making the API call
190152
*/
191153
public CompletableFuture<GenerateImagesResponse> generateImages(
192-
String model, String prompt, GenerateImagesConfig config) throws IOException, HttpException {
193-
return CompletableFuture.supplyAsync(
194-
() -> {
195-
try {
196-
return models.generateImages(model, prompt, config);
197-
} catch (IOException | HttpException e) {
198-
throw new RuntimeException(e);
199-
}
200-
});
154+
String model, String prompt, GenerateImagesConfig config) {
155+
return CompletableFuture.supplyAsync(() -> models.generateImages(model, prompt, config));
201156
}
202157

203158
/**
@@ -210,20 +165,11 @@ public CompletableFuture<GenerateImagesResponse> generateImages(
210165
* optional configurations
211166
* @return a {@link com.google.genai.types.UpscaleImageResponse} instance that contains the
212167
* upscaled image.
213-
* @throws IOException if an I/O error occurs while making the API call
214-
* @throws HttpException if an HTTP error occurs while making the API call
215168
*/
216169
public CompletableFuture<UpscaleImageResponse> upscaleImage(
217-
String model, Image image, String upscaleFactor, UpscaleImageConfig config)
218-
throws IOException, HttpException {
170+
String model, Image image, String upscaleFactor, UpscaleImageConfig config) {
219171
return CompletableFuture.supplyAsync(
220-
() -> {
221-
try {
222-
return models.upscaleImage(model, image, upscaleFactor, config);
223-
} catch (IOException | HttpException e) {
224-
throw new RuntimeException(e);
225-
}
226-
});
172+
() -> models.upscaleImage(model, image, upscaleFactor, config));
227173
}
228174

229175
/**
@@ -233,19 +179,10 @@ public CompletableFuture<UpscaleImageResponse> upscaleImage(
233179
* @param text the text string to send to the embedding model
234180
* @return a {@link com.google.genai.types.EmbedContentResponse} instance that contains the
235181
* embedding.
236-
* @throws IOException if an I/O error occurs while making the API call
237-
* @throws HttpException if an HTTP error occurs while making the API call
238182
*/
239183
public CompletableFuture<EmbedContentResponse> embedContent(
240-
String model, String text, EmbedContentConfig config) throws IOException, HttpException {
241-
return CompletableFuture.supplyAsync(
242-
() -> {
243-
try {
244-
return models.embedContent(model, text, config);
245-
} catch (IOException | HttpException e) {
246-
throw new RuntimeException(e);
247-
}
248-
});
184+
String model, String text, EmbedContentConfig config) {
185+
return CompletableFuture.supplyAsync(() -> models.embedContent(model, text, config));
249186
}
250187

251188
/**
@@ -255,19 +192,9 @@ public CompletableFuture<EmbedContentResponse> embedContent(
255192
* @param texts the list of text strings to send to the embedding model
256193
* @return a {@link com.google.genai.types.EmbedContentResponse} instance that contains the
257194
* embedding.
258-
* @throws IOException if an I/O error occurs while making the API call
259-
* @throws HttpException if an HTTP error occurs while making the API call
260195
*/
261196
public CompletableFuture<EmbedContentResponse> embedContent(
262-
String model, List<String> texts, EmbedContentConfig config)
263-
throws IOException, HttpException {
264-
return CompletableFuture.supplyAsync(
265-
() -> {
266-
try {
267-
return models.embedContent(model, texts, config);
268-
} catch (IOException | HttpException e) {
269-
throw new RuntimeException(e);
270-
}
271-
});
197+
String model, List<String> texts, EmbedContentConfig config) {
198+
return CompletableFuture.supplyAsync(() -> models.embedContent(model, texts, config));
272199
}
273200
}

src/main/java/com/google/genai/AsyncOperations.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import com.google.api.core.BetaApi;
2222
import com.google.genai.types.GenerateVideosOperation;
2323
import com.google.genai.types.GetOperationConfig;
24-
import java.io.IOException;
2524
import java.util.concurrent.CompletableFuture;
26-
import org.apache.http.HttpException;
2725

2826
public final class AsyncOperations {
2927
Operations operations;
@@ -38,20 +36,10 @@ public AsyncOperations(ApiClient apiClient) {
3836
* @param operation A GenerateVideosOperation.
3937
* @param config The configuration for getting the operation.
4038
* @return A GenerateVideosOperation with the updated status of the operation.
41-
* @throws IOException If an I/O error occurs while reading the operation.
42-
* @throws HttpException If an HTTP error occurs while reading the operation.
4339
*/
4440
@BetaApi
4541
public CompletableFuture<GenerateVideosOperation> getVideoOperation(
46-
GenerateVideosOperation operation, GetOperationConfig config)
47-
throws IOException, HttpException {
48-
return CompletableFuture.supplyAsync(
49-
() -> {
50-
try {
51-
return operations.getVideoOperation(operation, config);
52-
} catch (IOException | HttpException e) {
53-
throw new RuntimeException(e);
54-
}
55-
});
42+
GenerateVideosOperation operation, GetOperationConfig config) {
43+
return CompletableFuture.supplyAsync(() -> operations.getVideoOperation(operation, config));
5644
}
5745
}

src/main/java/com/google/genai/AsyncSession.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.genai.types.LiveSendToolResponseParameters;
2828
import com.google.genai.types.LiveServerMessage;
2929
import java.util.concurrent.CompletableFuture;
30-
import java.util.concurrent.CompletionException;
3130
import java.util.function.Consumer;
3231

3332
/**
@@ -101,18 +100,7 @@ public CompletableFuture<Void> sendToolResponse(LiveSendToolResponseParameters t
101100
* will fail if the message cannot be sent.
102101
*/
103102
private CompletableFuture<Void> send(LiveClientMessage input) {
104-
return CompletableFuture.runAsync(
105-
() -> {
106-
try {
107-
websocket.send(input.toJson());
108-
} catch (RuntimeException e) {
109-
throw new CompletionException("Failed to send message to live session.", e);
110-
}
111-
});
112-
}
113-
114-
public CompletableFuture<Void> sendContent(LiveClientMessage input) {
115-
return send(input);
103+
return CompletableFuture.runAsync(() -> websocket.send(input.toJson()));
116104
}
117105

118106
/**
@@ -133,17 +121,12 @@ public CompletableFuture<Void> receive(Consumer<LiveServerMessage> onMessage) {
133121
/**
134122
* Closes the WebSocket connection.
135123
*
136-
* @return A {@link CompletableFuture} that completes when the connection has been closed. Fails
137-
* with a {@link CompletionException} wrapping the underlying cause if closing fails.
124+
* @return A {@link CompletableFuture} that completes when the connection has been closed.
138125
*/
139126
public CompletableFuture<Void> close() {
140127
return CompletableFuture.runAsync(
141128
() -> {
142-
try {
143-
websocket.close();
144-
} catch (RuntimeException e) {
145-
throw new CompletionException("Failed to close websocket connection.", e);
146-
}
129+
websocket.close();
147130
});
148131
}
149132
}

0 commit comments

Comments
 (0)