","parameterTypes":["long","boolean","boolean","boolean"] }, {"name":"loadClassAndCallAgentmain","parameterTypes":["java.lang.String","java.lang.String"] }, {"name":"loadClassAndCallPremain","parameterTypes":["java.lang.String","java.lang.String"] }, {"name":"transform","parameterTypes":["java.lang.Module","java.lang.ClassLoader","java.lang.String","java.lang.Class","java.security.ProtectionDomain","byte[]","boolean"] }]
5 | },
6 | {
7 | "name":"sun.management.VMManagementImpl",
8 | "fields":[{"name":"compTimeMonitoringSupport"}, {"name":"currentThreadCpuTimeSupport"}, {"name":"objectMonitorUsageSupport"}, {"name":"otherThreadCpuTimeSupport"}, {"name":"remoteDiagnosticCommandsSupport"}, {"name":"synchronizerUsageSupport"}, {"name":"threadAllocatedMemorySupport"}, {"name":"threadContentionMonitoringSupport"}]
9 | }
10 | ]
11 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/errors/ClientException.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | /** Client exception raised by the GenAI API. */
20 | public final class ClientException extends ApiException {
21 |
22 | /** Creates a new ClientException with the specified message. */
23 | public ClientException(int code, String status, String message) {
24 | super(code, status, message);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/errors/ServerException.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | /** Server exception raised by the GenAI API. */
20 | public final class ServerException extends ApiException {
21 |
22 | /** Creates a new ServerException with the specified message. */
23 | public ServerException(int code, String status, String message) {
24 | super(code, status, message);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ReferenceImage.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.types;
18 |
19 | /**
20 | * An interface for a Reference image.
21 | *
22 | * The 5 types of reference images are: {@link com.google.genai.types.RawReferenceImage}, {@link
23 | * com.google.genai.types.MaskReferenceImage}, {@link com.google.genai.types.ControlReferenceImage},
24 | * {@link com.google.genai.types.StyleReferenceImage}, {@link
25 | * com.google.genai.types.SubjectReferenceImage},
26 | */
27 | public interface ReferenceImage {
28 | public ReferenceImageAPI toReferenceImageAPI();
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/ApiResponse.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.api.core.InternalApi;
20 | import okhttp3.Headers;
21 | import okhttp3.ResponseBody;
22 |
23 | /** The API response contains a response to a call to the GenAI APIs. */
24 | @InternalApi
25 | public abstract class ApiResponse implements AutoCloseable {
26 | /** Gets the ResponseBody. */
27 | public abstract ResponseBody getBody();
28 |
29 | /** Returns all of the headers from the response. */
30 | public abstract Headers getHeaders();
31 |
32 | @Override
33 | public abstract void close();
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/ExcludeFromGeneratedCoverageReport.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation to exclude a method or constructor from the Jacoco coverage report.
26 | *
27 | *
Jacoco will exclude methods that have an annotation with the word "generated" in it.
28 | */
29 | @Retention(RetentionPolicy.CLASS)
30 | @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
31 | @interface ExcludeFromGeneratedCoverageReport {}
32 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/errors/ExcludeFromGeneratedCoverageReport.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation to exclude a method or constructor from the Jacoco coverage report.
26 | *
27 | *
Jacoco will exclude methods that have an annotation with the word "generated" in it.
28 | */
29 | @Retention(RetentionPolicy.CLASS)
30 | @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
31 | @interface ExcludeFromGeneratedCoverageReport {}
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ExcludeFromGeneratedCoverageReport.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.types;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Annotation to exclude a method or constructor from the Jacoco coverage report.
26 | *
27 | *
Jacoco will exclude methods that have an annotation with the word "generated" in it.
28 | */
29 | @Retention(RetentionPolicy.CLASS)
30 | @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
31 | @interface ExcludeFromGeneratedCoverageReport {}
32 |
--------------------------------------------------------------------------------
/.github/workflows/stale.yml:
--------------------------------------------------------------------------------
1 | # This workflow warns and then closes issues that have had no activity for a specified amount of time.
2 | name: Mark and close stale issues
3 |
4 | on:
5 | schedule:
6 | # Scheduled to run at 1:30 UTC everyday
7 | - cron: '30 1 * * *'
8 |
9 | jobs:
10 | stale:
11 |
12 | runs-on: ubuntu-latest
13 | permissions:
14 | issues: write
15 |
16 | steps:
17 | - uses: actions/stale@v5
18 | with:
19 | repo-token: ${{ secrets.GITHUB_TOKEN }}
20 | days-before-issue-stale: 7
21 | days-before-issue-close: 2
22 | stale-issue-label: "status:stale"
23 | close-issue-reason: not_planned
24 | any-of-labels: "status:awaiting user response"
25 | remove-stale-when-updated: true
26 | labels-to-remove-when-unstale: 'status:awaiting user response,status:stale'
27 | stale-issue-message: >
28 | This issue has been marked as stale because it has been open for 7 days with no activity. It will be closed in 2 days if no further activity occurs.
29 | close-issue-message: >
30 | This issue was closed because it has been inactive for 9 days.
31 | Please post a new issue if you need further assistance. Thanks!
32 | # Label that can be assigned to issues to exclude them from being marked as stale
33 | exempt-issue-labels: 'override-stale'
34 |
--------------------------------------------------------------------------------
/examples/src/main/java/com/google/genai/examples/LocalCountTokens.java:
--------------------------------------------------------------------------------
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 | *
1. Compile the java package and run the sample code.
21 | *
22 | *
mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.LocalCountTokens"
23 | */
24 | package com.google.genai.examples;
25 |
26 | import com.google.genai.LocalTokenizer;
27 |
28 | /** An example of using the Unified Gen AI Java SDK to count tokens locally. */
29 | public class LocalCountTokens {
30 | public static void main(String[] args) {
31 | LocalTokenizer tokenizer = new LocalTokenizer(Constants.GEMINI_MODEL_NAME);
32 | System.out.println(
33 | "Count for 'Hello world': " + tokenizer.countTokens("Hello world").totalTokens());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/FakeApiResponse.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import okhttp3.Headers;
20 | import okhttp3.ResponseBody;
21 |
22 | /** */
23 | public final class FakeApiResponse extends ApiResponse {
24 |
25 | private final Headers headers;
26 | private final ResponseBody body;
27 |
28 | public FakeApiResponse(Headers headers, ResponseBody body) {
29 | this.headers = headers;
30 | this.body = body;
31 | }
32 |
33 | @Override
34 | public Headers getHeaders() {
35 | return headers;
36 | }
37 |
38 | @Override
39 | public ResponseBody getBody() {
40 | return body;
41 | }
42 |
43 | @Override
44 | public void close() {
45 | throw new UnsupportedOperationException("Not implemented yet.");
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/examples/src/main/java/com/google/genai/examples/LocalComputeTokens.java:
--------------------------------------------------------------------------------
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 | *
1. Compile the java package and run the sample code.
21 | *
22 | *
mvn clean compile exec:java -Dexec.mainClass="com.google.genai.examples.LocalComputeTokens"
23 | */
24 | package com.google.genai.examples;
25 |
26 | import com.google.genai.LocalTokenizer;
27 |
28 | /** An example of using the Unified Gen AI Java SDK to compute tokens locally. */
29 | public class LocalComputeTokens {
30 | public static void main(String[] args) {
31 | LocalTokenizer tokenizer = new LocalTokenizer(Constants.GEMINI_MODEL_NAME);
32 | System.out.println(
33 | "Compute tokens for 'Hello world': " + tokenizer.computeTokens("Hello world").toJson());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/errors/GenAiIOException.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | import java.io.IOException;
20 |
21 | /** IO exception raised in the GenAI SDK. */
22 | public final class GenAiIOException extends BaseException {
23 |
24 | /** Creates a new GenAiIoException with the specified message and the original IOException. */
25 | public GenAiIOException(String message, IOException cause) {
26 | super(message, cause);
27 | }
28 |
29 | /** Creates a new GenAiIoException with the specified message. */
30 | public GenAiIOException(String message) {
31 | super(message);
32 | }
33 |
34 | /** Creates a new GenAiIoException with the specified cause. */
35 | public GenAiIOException(Throwable cause) {
36 | super(cause.getMessage(), cause);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/errors/BaseException.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | /**
20 | * Base exception class for all exceptions specifically originating from the GenAI SDK.
21 | *
22 | *
This class extends {@link RuntimeException}. The GenAI SDK favors unchecked exceptions
23 | * to improve developer experience by reducing mandatory {@code try-catch} or {@code throws}
24 | * clause boilerplate for potentially unrecoverable runtime errors.
25 | */
26 | class BaseException extends RuntimeException {
27 |
28 | /** Creates a new exception with the specified message. */
29 | BaseException(String message) {
30 | super(message);
31 | }
32 |
33 | /** Creates a new exception with the specified message and cause. */
34 | BaseException(String message, Throwable cause) {
35 | super(message, cause);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/types/FunctionResponsePartTest.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.types;
18 |
19 | import static org.junit.jupiter.api.Assertions.assertEquals;
20 |
21 | import org.junit.jupiter.api.Test;
22 |
23 | public class FunctionResponsePartTest {
24 |
25 | @Test
26 | public void testFunctionResponsePartFromUri() {
27 | FunctionResponsePart part = FunctionResponsePart.fromUri("test-uri", "test-mime-type");
28 |
29 | assertEquals("test-uri", part.fileData().get().fileUri().get());
30 | assertEquals("test-mime-type", part.fileData().get().mimeType().get());
31 | }
32 |
33 | @Test
34 | public void testPartFromBytes() {
35 | byte[] bytes = new byte[] {1, 2, 3};
36 | FunctionResponsePart part =
37 | FunctionResponsePart.fromBytes(bytes, "test-mime-type");
38 |
39 | assertEquals(bytes, part.inlineData().get().data().get());
40 | assertEquals("test-mime-type", part.inlineData().get().mimeType().get());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/HttpApiResponse.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.genai.errors.ApiException;
20 | import okhttp3.Headers;
21 | import okhttp3.Response;
22 | import okhttp3.ResponseBody;
23 |
24 | /** Wraps a real HTTP response to expose the methods needed by the GenAI SDK. */
25 | final class HttpApiResponse extends ApiResponse {
26 |
27 | private final Response response;
28 |
29 | /** Constructs a HttpApiResponse instance with the response. */
30 | public HttpApiResponse(Response response) {
31 | this.response = response;
32 | }
33 |
34 | /** Returns the ResponseBody from the response. */
35 | @Override
36 | public ResponseBody getBody() {
37 | ApiException.throwFromResponse(response);
38 | return response.body();
39 | }
40 |
41 | /** Returns all of the headers from the response. */
42 | @Override
43 | public Headers getHeaders() {
44 | return response.headers();
45 | }
46 |
47 | /** Closes the Http response. */
48 | @Override
49 | public void close() {
50 | response.close();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/Chats.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.genai.types.GenerateContentConfig;
20 |
21 | /** A class for creating chat sessions. */
22 | public class Chats {
23 | private final ApiClient apiClient;
24 |
25 | Chats(ApiClient apiClient) {
26 | this.apiClient = apiClient;
27 | }
28 |
29 | /**
30 | * Creates a new chat session.
31 | *
32 | * @param model The model to use for the chat session.
33 | * @param config The configuration for the chat session.
34 | * @return The chat session.
35 | */
36 | public Chat create(String model, GenerateContentConfig config) {
37 | return privateCreate(model, config);
38 | }
39 |
40 | /**
41 | * Creates a new chat session.
42 | *
43 | * @param model The model to use for the chat session.
44 | * @return The chat session.
45 | */
46 | public Chat create(String model) {
47 | return privateCreate(model, null);
48 | }
49 |
50 | private Chat privateCreate(String model, GenerateContentConfig config) {
51 | return new Chat(apiClient, model, config);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/AsyncChats.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.genai.types.GenerateContentConfig;
20 |
21 | /** A class for creating async chat sessions. */
22 | public class AsyncChats {
23 | private final ApiClient apiClient;
24 |
25 | AsyncChats(ApiClient apiClient) {
26 | this.apiClient = apiClient;
27 | }
28 |
29 | /**
30 | * Creates a new async chat session.
31 | *
32 | * @param model The model to use for the async chat session.
33 | * @param config The configuration for the async chat session.
34 | * @return The async chat session.
35 | */
36 | public AsyncChat create(String model, GenerateContentConfig config) {
37 | return privateCreate(model, config);
38 | }
39 |
40 | /**
41 | * Creates a new async chat session.
42 | *
43 | * @param model The model to use for the async chat session.
44 | * @return The async chat session.
45 | */
46 | public AsyncChat create(String model) {
47 | return privateCreate(model, null);
48 | }
49 |
50 | private AsyncChat privateCreate(String model, GenerateContentConfig config) {
51 | return new AsyncChat(apiClient, model, config);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/TestUtils.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | public final class TestUtils {
20 | static final String API_KEY = "api-key";
21 | static final String PROJECT = "project";
22 | static final String LOCATION = "location";
23 |
24 | private TestUtils() {}
25 |
26 | /** Creates a client given the vertexAI and replayId. Can be used in replay tests. */
27 | public static Client createClient(boolean vertexAI, String replayId) {
28 | String clientMode = System.getenv("GOOGLE_GENAI_CLIENT_MODE");
29 | DebugConfig debugConfig =
30 | new DebugConfig(
31 | clientMode == null ? "replay" : clientMode,
32 | "",
33 | System.getenv("GOOGLE_GENAI_REPLAYS_DIRECTORY"));
34 |
35 | Client.Builder clientBuilder = Client.builder().vertexAI(vertexAI).debugConfig(debugConfig);
36 |
37 | if (vertexAI) {
38 | clientBuilder.project(PROJECT).location(LOCATION);
39 | } else {
40 | clientBuilder.apiKey(API_KEY);
41 | }
42 | Client client = clientBuilder.build();
43 |
44 | if (client.clientMode().equals("replay")) {
45 | client.setReplayId(replayId);
46 | }
47 | return client;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/ReplayBase64Sanitizer.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import java.util.Base64;
20 |
21 | public class ReplayBase64Sanitizer implements ReplaySanitizer.Sanitizer {
22 |
23 | @Override
24 | public Object sanitize(Object obj, String path) {
25 | if (obj instanceof String) {
26 | String str = (String) obj;
27 |
28 | try {
29 | // 1. Attempt URL-safe Base64 decoding
30 | byte[] decoded = Base64.getUrlDecoder().decode(str);
31 | // If URL-safe decoding succeeds, encode back to standard Base64
32 | return Base64.getEncoder().encodeToString(decoded);
33 |
34 | } catch (IllegalArgumentException e1) {
35 | try {
36 | // 2. If URL-safe decoding fails, attempt standard Base64 decoding
37 | byte[] decoded = Base64.getDecoder().decode(str);
38 | return Base64.getEncoder()
39 | .encodeToString(decoded); // Encode back to standard Base64 for consistency
40 |
41 | } catch (IllegalArgumentException e2) {
42 | // 3. If both fail, it's not a valid Base64 string.
43 | throw e2;
44 | }
45 | }
46 | }
47 | return obj; // If it's not a string, return the object as is
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/AsyncTokens.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.genai.Common.BuiltRequest;
20 | import com.google.genai.types.AuthToken;
21 | import com.google.genai.types.CreateAuthTokenConfig;
22 | import java.util.concurrent.CompletableFuture;
23 |
24 | /** Async module of {@link AuthToken} */
25 | public final class AsyncTokens {
26 |
27 | Tokens tokens;
28 | ApiClient apiClient;
29 |
30 | public AsyncTokens(ApiClient apiClient) {
31 | this.apiClient = apiClient;
32 | this.tokens = new Tokens(apiClient);
33 | }
34 |
35 | /**
36 | * Asynchronously creates an ephemeral auth token resource.
37 | *
38 | * @param config A {@link CreateAuthTokenConfig} for configuring the create request.
39 | * @return A {@link AuthToken} object that contains the info of the created resource.
40 | */
41 | public CompletableFuture create(CreateAuthTokenConfig config) {
42 | BuiltRequest builtRequest = tokens.buildRequestForCreate(config);
43 | return this.apiClient
44 | .asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
45 | .thenApplyAsync(
46 | response -> {
47 | try (ApiResponse res = response) {
48 | return tokens.processResponseForCreate(res, config);
49 | }
50 | });
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/UrlContext.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /** Tool to support URL context. */
27 | @AutoValue
28 | @JsonDeserialize(builder = UrlContext.Builder.class)
29 | public abstract class UrlContext extends JsonSerializable {
30 | /** Instantiates a builder for UrlContext. */
31 | @ExcludeFromGeneratedCoverageReport
32 | public static Builder builder() {
33 | return new AutoValue_UrlContext.Builder();
34 | }
35 |
36 | /** Creates a builder with the same values as this instance. */
37 | public abstract Builder toBuilder();
38 |
39 | /** Builder for UrlContext. */
40 | @AutoValue.Builder
41 | public abstract static class Builder {
42 | /** For internal usage. Please use `UrlContext.builder()` for instantiation. */
43 | @JsonCreator
44 | private static Builder create() {
45 | return new AutoValue_UrlContext.Builder();
46 | }
47 |
48 | public abstract UrlContext build();
49 | }
50 |
51 | /** Deserializes a JSON string to a UrlContext object. */
52 | @ExcludeFromGeneratedCoverageReport
53 | public static UrlContext fromJson(String jsonString) {
54 | return JsonSerializable.fromJsonString(jsonString, UrlContext.class);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/examples/resources/test.txt:
--------------------------------------------------------------------------------
1 | Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.
2 |
3 | Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.
4 |
5 | Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.
6 |
7 | Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.
8 |
9 | Lorem ipsum dolor sit amet consectetur adipiscing elit. Quisque faucibus ex sapien vitae pellentesque sem placerat. In id cursus mi pretium tellus duis convallis. Tempus leo eu aenean sed diam urna tempor. Pulvinar vivamus fringilla lacus nec metus bibendum egestas. Iaculis massa nisl malesuada lacinia integer nunc posuere. Ut hendrerit semper vel class aptent taciti sociosqu. Ad litora torquent per conubia nostra inceptos himenaeos.
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ActivityEnd.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /**
27 | * Marks the end of user activity.
28 | *
29 | * This can only be sent if automatic (i.e. server-side) activity detection is disabled.
30 | */
31 | @AutoValue
32 | @JsonDeserialize(builder = ActivityEnd.Builder.class)
33 | public abstract class ActivityEnd extends JsonSerializable {
34 | /** Instantiates a builder for ActivityEnd. */
35 | @ExcludeFromGeneratedCoverageReport
36 | public static Builder builder() {
37 | return new AutoValue_ActivityEnd.Builder();
38 | }
39 |
40 | /** Creates a builder with the same values as this instance. */
41 | public abstract Builder toBuilder();
42 |
43 | /** Builder for ActivityEnd. */
44 | @AutoValue.Builder
45 | public abstract static class Builder {
46 | /** For internal usage. Please use `ActivityEnd.builder()` for instantiation. */
47 | @JsonCreator
48 | private static Builder create() {
49 | return new AutoValue_ActivityEnd.Builder();
50 | }
51 |
52 | public abstract ActivityEnd build();
53 | }
54 |
55 | /** Deserializes a JSON string to a ActivityEnd object. */
56 | @ExcludeFromGeneratedCoverageReport
57 | public static ActivityEnd fromJson(String jsonString) {
58 | return JsonSerializable.fromJsonString(jsonString, ActivityEnd.class);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ActivityStart.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /**
27 | * Marks the start of user activity.
28 | *
29 | *
This can only be sent if automatic (i.e. server-side) activity detection is disabled.
30 | */
31 | @AutoValue
32 | @JsonDeserialize(builder = ActivityStart.Builder.class)
33 | public abstract class ActivityStart extends JsonSerializable {
34 | /** Instantiates a builder for ActivityStart. */
35 | @ExcludeFromGeneratedCoverageReport
36 | public static Builder builder() {
37 | return new AutoValue_ActivityStart.Builder();
38 | }
39 |
40 | /** Creates a builder with the same values as this instance. */
41 | public abstract Builder toBuilder();
42 |
43 | /** Builder for ActivityStart. */
44 | @AutoValue.Builder
45 | public abstract static class Builder {
46 | /** For internal usage. Please use `ActivityStart.builder()` for instantiation. */
47 | @JsonCreator
48 | private static Builder create() {
49 | return new AutoValue_ActivityStart.Builder();
50 | }
51 |
52 | public abstract ActivityStart build();
53 | }
54 |
55 | /** Deserializes a JSON string to a ActivityStart object. */
56 | @ExcludeFromGeneratedCoverageReport
57 | public static ActivityStart fromJson(String jsonString) {
58 | return JsonSerializable.fromJsonString(jsonString, ActivityStart.class);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/AudioTranscriptionConfig.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /** The audio transcription configuration in Setup. */
27 | @AutoValue
28 | @JsonDeserialize(builder = AudioTranscriptionConfig.Builder.class)
29 | public abstract class AudioTranscriptionConfig extends JsonSerializable {
30 | /** Instantiates a builder for AudioTranscriptionConfig. */
31 | @ExcludeFromGeneratedCoverageReport
32 | public static Builder builder() {
33 | return new AutoValue_AudioTranscriptionConfig.Builder();
34 | }
35 |
36 | /** Creates a builder with the same values as this instance. */
37 | public abstract Builder toBuilder();
38 |
39 | /** Builder for AudioTranscriptionConfig. */
40 | @AutoValue.Builder
41 | public abstract static class Builder {
42 | /** For internal usage. Please use `AudioTranscriptionConfig.builder()` for instantiation. */
43 | @JsonCreator
44 | private static Builder create() {
45 | return new AutoValue_AudioTranscriptionConfig.Builder();
46 | }
47 |
48 | public abstract AudioTranscriptionConfig build();
49 | }
50 |
51 | /** Deserializes a JSON string to a AudioTranscriptionConfig object. */
52 | @ExcludeFromGeneratedCoverageReport
53 | public static AudioTranscriptionConfig fromJson(String jsonString) {
54 | return JsonSerializable.fromJsonString(jsonString, AudioTranscriptionConfig.class);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/errors/GenAiIOExceptionTest.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.errors;
18 |
19 | import static org.junit.jupiter.api.Assertions.assertEquals;
20 | import static org.junit.jupiter.api.Assertions.assertTrue;
21 |
22 | import java.io.IOException;
23 | import org.junit.jupiter.api.Test;
24 |
25 | class GenAiIOExceptionTest {
26 |
27 | @Test
28 | public void testInitGenAiIOException_withMessage() {
29 | try {
30 | throw new GenAiIOException("Error message");
31 | } catch (GenAiIOException e) {
32 | assertTrue(e instanceof BaseException);
33 | assertTrue(e instanceof RuntimeException);
34 | assertEquals("Error message", e.getMessage());
35 | }
36 | }
37 |
38 | @Test
39 | public void testInitGenAiIOException_withCause() {
40 | try {
41 | throw new GenAiIOException(new IOException("i/o exception"));
42 | } catch (GenAiIOException e) {
43 | assertTrue(e instanceof BaseException);
44 | assertTrue(e instanceof RuntimeException);
45 | assertEquals("i/o exception", e.getMessage());
46 | assertTrue(e.getCause() instanceof IOException);
47 | }
48 | }
49 |
50 | @Test
51 | public void testInitGenAiIOException_withMessageAndCause() {
52 | try {
53 | throw new GenAiIOException("Error message", new IOException("i/o exception"));
54 | } catch (GenAiIOException e) {
55 | assertTrue(e instanceof BaseException);
56 | assertTrue(e instanceof RuntimeException);
57 | assertEquals("Error message", e.getMessage());
58 | assertEquals("i/o exception", e.getCause().getMessage());
59 | assertTrue(e.getCause() instanceof IOException);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/DebugConfig.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | /** Data class configuration for debugging or testing the Client. */
20 | @ExcludeFromGeneratedCoverageReport
21 | final class DebugConfig {
22 | private final String clientMode;
23 | private final String replayId;
24 | private final String replaysDirectory;
25 |
26 | DebugConfig() {
27 | String clientMode = System.getenv("GOOGLE_GENAI_CLIENT_MODE");
28 | this.clientMode = clientMode != null ? clientMode : "";
29 |
30 | String replayId = System.getenv("GOOGLE_GENAI_REPLAY_ID");
31 | this.replayId = replayId != null ? replayId : "";
32 |
33 | String replaysDirectory = System.getenv("GOOGLE_GENAI_REPLAYS_DIRECTORY");
34 | this.replaysDirectory = replaysDirectory != null ? replaysDirectory : "";
35 | }
36 |
37 | DebugConfig(String clientMode, String replayId, String replaysDirectory) {
38 | String envClientMode = System.getenv("GOOGLE_GENAI_CLIENT_MODE");
39 | this.clientMode =
40 | clientMode != null ? clientMode : (envClientMode != null ? envClientMode : "");
41 |
42 | String envReplayId = System.getenv("GOOGLE_GENAI_REPLAY_ID");
43 | this.replayId = replayId != null ? replayId : (envReplayId != null ? envReplayId : "");
44 |
45 | String envReplaysDirectory = System.getenv("GOOGLE_GENAI_REPLAYS_DIRECTORY");
46 | this.replaysDirectory =
47 | replaysDirectory != null
48 | ? replaysDirectory
49 | : (envReplaysDirectory != null ? envReplaysDirectory : "");
50 | }
51 |
52 | public String clientMode() {
53 | return this.clientMode;
54 | }
55 |
56 | public String replayId() {
57 | return this.replayId;
58 | }
59 |
60 | public String replaysDirectory() {
61 | return this.replaysDirectory;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ToolCodeExecution.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /**
27 | * Tool that executes code generated by the model, and automatically returns the result to the
28 | * model. See also [ExecutableCode]and [CodeExecutionResult] which are input and output to this
29 | * tool. This data type is not supported in Gemini API.
30 | */
31 | @AutoValue
32 | @JsonDeserialize(builder = ToolCodeExecution.Builder.class)
33 | public abstract class ToolCodeExecution extends JsonSerializable {
34 | /** Instantiates a builder for ToolCodeExecution. */
35 | @ExcludeFromGeneratedCoverageReport
36 | public static Builder builder() {
37 | return new AutoValue_ToolCodeExecution.Builder();
38 | }
39 |
40 | /** Creates a builder with the same values as this instance. */
41 | public abstract Builder toBuilder();
42 |
43 | /** Builder for ToolCodeExecution. */
44 | @AutoValue.Builder
45 | public abstract static class Builder {
46 | /** For internal usage. Please use `ToolCodeExecution.builder()` for instantiation. */
47 | @JsonCreator
48 | private static Builder create() {
49 | return new AutoValue_ToolCodeExecution.Builder();
50 | }
51 |
52 | public abstract ToolCodeExecution build();
53 | }
54 |
55 | /** Deserializes a JSON string to a ToolCodeExecution object. */
56 | @ExcludeFromGeneratedCoverageReport
57 | public static ToolCodeExecution fromJson(String jsonString) {
58 | return JsonSerializable.fromJsonString(jsonString, ToolCodeExecution.class);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/ExternalApiSimpleSearchParams.java:
--------------------------------------------------------------------------------
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.databind.annotation.JsonDeserialize;
23 | import com.google.auto.value.AutoValue;
24 | import com.google.genai.JsonSerializable;
25 |
26 | /**
27 | * The search parameters to use for SIMPLE_SEARCH spec. This data type is not supported in Gemini
28 | * API.
29 | */
30 | @AutoValue
31 | @JsonDeserialize(builder = ExternalApiSimpleSearchParams.Builder.class)
32 | public abstract class ExternalApiSimpleSearchParams extends JsonSerializable {
33 | /** Instantiates a builder for ExternalApiSimpleSearchParams. */
34 | @ExcludeFromGeneratedCoverageReport
35 | public static Builder builder() {
36 | return new AutoValue_ExternalApiSimpleSearchParams.Builder();
37 | }
38 |
39 | /** Creates a builder with the same values as this instance. */
40 | public abstract Builder toBuilder();
41 |
42 | /** Builder for ExternalApiSimpleSearchParams. */
43 | @AutoValue.Builder
44 | public abstract static class Builder {
45 | /**
46 | * For internal usage. Please use `ExternalApiSimpleSearchParams.builder()` for instantiation.
47 | */
48 | @JsonCreator
49 | private static Builder create() {
50 | return new AutoValue_ExternalApiSimpleSearchParams.Builder();
51 | }
52 |
53 | public abstract ExternalApiSimpleSearchParams build();
54 | }
55 |
56 | /** Deserializes a JSON string to a ExternalApiSimpleSearchParams object. */
57 | @ExcludeFromGeneratedCoverageReport
58 | public static ExternalApiSimpleSearchParams fromJson(String jsonString) {
59 | return JsonSerializable.fromJsonString(jsonString, ExternalApiSimpleSearchParams.class);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/types/VideoMetadataTest.java:
--------------------------------------------------------------------------------
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 | package com.google.genai.types;
18 |
19 | import static org.junit.jupiter.api.Assertions.assertEquals;
20 | import static org.junit.jupiter.api.Assertions.assertFalse;
21 | import static org.junit.jupiter.api.Assertions.assertTrue;
22 |
23 | import java.time.Duration;
24 | import org.junit.jupiter.api.Test;
25 |
26 | public class VideoMetadataTest {
27 |
28 | @Test
29 | public void testSerializationDeserialization_withDurations() {
30 | Duration start = Duration.ofSeconds(60);
31 | Duration end = Duration.ofSeconds(125);
32 |
33 | VideoMetadata original =
34 | VideoMetadata.builder().startOffset(start).endOffset(end).build();
35 |
36 | // Test serialization
37 | String json = original.toJson();
38 | assertTrue(json.contains("\"startOffset\":\"60s\""));
39 | assertTrue(json.contains("\"endOffset\":\"125s\""));
40 |
41 | // Test deserialization
42 | VideoMetadata deserialized = VideoMetadata.fromJson(json);
43 | assertTrue(deserialized.startOffset().isPresent());
44 | assertEquals(start, deserialized.startOffset().get());
45 | assertTrue(deserialized.endOffset().isPresent());
46 | assertEquals(end, deserialized.endOffset().get());
47 | }
48 |
49 | @Test
50 | public void testSerializationDeserialization_nullDurations() {
51 | VideoMetadata original = VideoMetadata.builder().build();
52 |
53 | // Test serialization
54 | String json = original.toJson();
55 | // Should not contain the fields due to JsonInclude.Include.NON_ABSENT
56 | assertFalse(json.contains("startOffset"));
57 | assertFalse(json.contains("endOffset"));
58 |
59 | // Test deserialization
60 | VideoMetadata deserialized = VideoMetadata.fromJson(json);
61 | assertFalse(deserialized.startOffset().isPresent());
62 | assertFalse(deserialized.endOffset().isPresent());
63 | }
64 | }
--------------------------------------------------------------------------------
/src/test/java/com/google/genai/NativeImageReflectionTest.java:
--------------------------------------------------------------------------------
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 | package com.google.genai;
18 |
19 | import com.google.genai.types.*;
20 | import org.junit.jupiter.api.Test;
21 |
22 | /**
23 | * Test to generate comprehensive reflection metadata for native image.
24 | * This test instantiates all builder types to ensure they're captured by the agent.
25 | */
26 | public class NativeImageReflectionTest {
27 |
28 | @Test
29 | public void testAllBuilderTypes() {
30 | // Exercise all the builder types that might be used in streaming responses
31 | try {
32 | // FunctionResponsePart and related types
33 | FunctionResponseBlob.builder().build();
34 | FunctionResponseFileData.builder().build();
35 | FunctionResponsePart.builder().build();
36 |
37 | // Content and Part types
38 | Content.builder().build();
39 | Part.builder().build();
40 |
41 | // Response types
42 | GenerateContentResponse.builder().build();
43 | Candidate.builder().build();
44 |
45 | // Safety and metadata types
46 | SafetyRating.builder().build();
47 | CitationMetadata.builder().build();
48 |
49 | // Usage metadata
50 | UsageMetadata.builder().build();
51 |
52 | // Grounding metadata
53 | GroundingMetadata.builder().build();
54 | GroundingChunk.builder().build();
55 |
56 | // Model types
57 | Model.builder().build();
58 |
59 | } catch (Exception e) {
60 | // Expected - we're just ensuring reflection metadata is generated
61 | }
62 | }
63 |
64 | @Test
65 | public void testDeserializationTypes() {
66 | // Test JSON deserialization to ensure Jackson reflection is captured
67 | try {
68 | String jsonResponse = "{\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"test\"}]}}]}";
69 | GenerateContentResponse.fromJson(jsonResponse);
70 |
71 | String jsonPart = "{\"inlineData\":{\"mimeType\":\"text/plain\",\"data\":\"test\"}}";
72 | FunctionResponsePart.fromJson(jsonPart);
73 |
74 | } catch (Exception e) {
75 | // Expected - we're just ensuring reflection metadata is generated
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/src/main/java/com/google/genai/types/Operation.java:
--------------------------------------------------------------------------------
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.JsonProperty;
22 | import com.fasterxml.jackson.databind.JsonNode;
23 | import com.google.genai.JsonSerializable;
24 | import java.util.Map;
25 | import java.util.Optional;
26 |
27 | /** A long-running operation. */
28 | public abstract class Operation> extends JsonSerializable {
29 | /**
30 | * The server-assigned name, which is only unique within the same service that originally returns
31 | * it. If you use the default HTTP mapping, the `name` should be a resource name ending with
32 | * `operations/{unique_id}`.
33 | */
34 | @JsonProperty("name")
35 | public abstract Optional name();
36 |
37 | /**
38 | * Service-specific metadata associated with the operation. It typically contains progress
39 | * information and common metadata such as create time. Some services might not provide such
40 | * metadata. Any method that returns a long-running operation should document the metadata type,
41 | * if any.
42 | */
43 | @JsonProperty("metadata")
44 | public abstract Optional