Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ResponseModality;
import com.google.genai.gaos.models.interactions.SpeechConfig;
import com.google.genai.gaos.models.interactions.SpeechConfigUnion;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import com.google.genai.gaos.models.operations.CreateInteractionResponse;
Expand All @@ -61,7 +62,9 @@ private static void createInteractions(Client client) {
SpeechConfig speechConfig = SpeechConfig.builder().voice("achernar").language("en-US").build();

GenerationConfig generationConfig =
GenerationConfig.builder().speechConfig(Arrays.asList(speechConfig)).build();
GenerationConfig.builder()
.speechConfig(SpeechConfigUnion.of(Arrays.asList(speechConfig)))
.build();

CreateModelInteraction params =
CreateModelInteraction.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ResponseModality;
import com.google.genai.gaos.models.interactions.SpeechConfig;
import com.google.genai.gaos.models.interactions.SpeechConfigUnion;
import com.google.genai.gaos.models.interactions.Step;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import com.google.genai.gaos.models.operations.CreateInteractionResponse;
Expand All @@ -75,7 +76,9 @@ public static void main(String[] args) {
SpeechConfig speechConfig = SpeechConfig.builder().voice("achernar").language("en-US").build();

GenerationConfig generationConfig =
GenerationConfig.builder().speechConfig(Collections.singletonList(speechConfig)).build();
GenerationConfig.builder()
.speechConfig(SpeechConfigUnion.of(Collections.singletonList(speechConfig)))
.build();

CreateModelInteraction params =
CreateModelInteraction.builder()
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/google/genai/errors/ApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public ApiException(int code, String status, String message) {
this.message = message;
}

/**
* Creates a new ApiException carrying the originating cause (e.g. a reparented gaos error). Lets
* translated interaction errors keep their original stack/message.
*/
public ApiException(int code, String status, String message, Throwable cause) {
super(String.format("%d %s. %s", code, status, message), cause);
this.code = code;
this.status = status;
this.message = message;
}


/**
* Throws an ApiException from the response if the response is not a OK status.
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/google/genai/errors/ClientException.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package com.google.genai.errors;

/** Client exception raised by the GenAI API. */
public final class ClientException extends ApiException {
public class ClientException extends ApiException {

/** Creates a new ClientException with the specified message. */
public ClientException(int code, String status, String message) {
super(code, status, message);
}

/** Creates a new ClientException carrying the originating cause. */
public ClientException(int code, String status, String message, Throwable cause) {
super(code, status, message, cause);
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/google/genai/errors/ServerException.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package com.google.genai.errors;

/** Server exception raised by the GenAI API. */
public final class ServerException extends ApiException {
public class ServerException extends ApiException {

/** Creates a new ServerException with the specified message. */
public ServerException(int code, String status, String message) {
super(code, status, message);
}

/** Creates a new ServerException carrying the originating cause. */
public ServerException(int code, String status, String message, Throwable cause) {
super(code, status, message, cause);
}
}
80 changes: 40 additions & 40 deletions src/main/java/com/google/genai/gaos/hooks/SDKHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,55 @@ private SDKHooks() {
}

public static void initialize(com.google.genai.gaos.utils.Hooks hooks) {
hooks.registerBeforeRequest(
(context, request) -> {
if (context.securitySource().isPresent()) {
HasSecurity hasSecurity = context.securitySource().get().getSecurity();
if (hasSecurity instanceof Security) {
Security security = (Security) hasSecurity;
HttpRequest.Builder builder = request.toBuilder();
hooks.registerBeforeRequest(
(context, request) -> {
if (context.securitySource().isPresent()) {
HasSecurity hasSecurity = context.securitySource().get().getSecurity();
if (hasSecurity instanceof Security) {
Security security = (Security) hasSecurity;
HttpRequest.Builder builder = request.toBuilder();

if (security.defaultHeaders().isPresent()) {
for (Map.Entry<String, String> entry : security.defaultHeaders().get().entrySet()) {
builder.setHeader(entry.getKey(), entry.getValue());
if (security.defaultHeaders().isPresent()) {
for (Map.Entry<String, String> entry : security.defaultHeaders().get().entrySet()) {
builder.setHeader(entry.getKey(), entry.getValue());
}
}
if (security.apiKey().isPresent()) {
builder.setHeader("x-goog-api-key", security.apiKey().get());
} else if (security.accessToken().isPresent()) {
builder.setHeader("Authorization", "Bearer " + security.accessToken().get());
}
return builder.build();
}
}
if (security.apiKey().isPresent()) {
builder.setHeader("x-goog-api-key", security.apiKey().get());
} else if (security.accessToken().isPresent()) {
builder.setHeader("Authorization", "Bearer " + security.accessToken().get());
}
return builder.build();
}
}
return request;
});
return request;
});
}

public static void initialize(com.google.genai.gaos.utils.AsyncHooks asyncHooks) {
asyncHooks.registerBeforeRequest(
(context, request) -> {
if (context.securitySource().isPresent()) {
HasSecurity hasSecurity = context.securitySource().get().getSecurity();
if (hasSecurity instanceof Security) {
Security security = (Security) hasSecurity;
HttpRequest.Builder builder = request.toBuilder();
asyncHooks.registerBeforeRequest(
(context, request) -> {
if (context.securitySource().isPresent()) {
HasSecurity hasSecurity = context.securitySource().get().getSecurity();
if (hasSecurity instanceof Security) {
Security security = (Security) hasSecurity;
HttpRequest.Builder builder = request.toBuilder();

if (security.defaultHeaders().isPresent()) {
for (Map.Entry<String, String> entry : security.defaultHeaders().get().entrySet()) {
builder.setHeader(entry.getKey(), entry.getValue());
if (security.defaultHeaders().isPresent()) {
for (Map.Entry<String, String> entry : security.defaultHeaders().get().entrySet()) {
builder.setHeader(entry.getKey(), entry.getValue());
}
}
if (security.apiKey().isPresent()) {
builder.setHeader("x-goog-api-key", security.apiKey().get());
} else if (security.accessToken().isPresent()) {
builder.setHeader("Authorization", "Bearer " + security.accessToken().get());
}
return CompletableFuture.completedFuture(builder.build());
}
}
if (security.apiKey().isPresent()) {
builder.setHeader("x-goog-api-key", security.apiKey().get());
} else if (security.accessToken().isPresent()) {
builder.setHeader("Authorization", "Bearer " + security.accessToken().get());
}
return CompletableFuture.completedFuture(builder.build());
}
}
return CompletableFuture.completedFuture(request);
});
return CompletableFuture.completedFuture(request);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* An exception associated with Authentication or Authorization.
*/
@SuppressWarnings("serial")
public class AuthException extends GenAiException {
public class AuthException extends GaosClientException {

public AuthException(String message, int code, byte[] body, HttpResponse<InputStream> rawResponse) {
super(message, code, body, rawResponse, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class CancelInteractionByIdClientError extends GenAiException {
public class CancelInteractionByIdClientError extends GaosClientException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class CancelInteractionByIdServerError extends GenAiException {
public class CancelInteractionByIdServerError extends GaosServerException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class CreateInteractionClientError extends GenAiException {
public class CreateInteractionClientError extends GaosClientException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class CreateInteractionServerError extends GenAiException {
public class CreateInteractionServerError extends GaosServerException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class DeleteInteractionClientError extends GenAiException {
public class DeleteInteractionClientError extends GaosClientException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Optional;

@SuppressWarnings("serial")
public class DeleteInteractionServerError extends GenAiException {
public class DeleteInteractionServerError extends GaosServerException {

@Nullable
private final Data data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
* Thrown by a service call when an error response occurs. Contains details about the response.
*/
@SuppressWarnings("serial")
public class SDKException extends GenAiException {
public class GaosApiException extends GaosBaseException {

public SDKException(
public GaosApiException(
String message,
int code,
@Nullable byte[] body,
Expand All @@ -41,18 +41,18 @@ public SDKException(
super(message, code, body, rawResponse, cause);
}

public static SDKException from(String message, HttpResponse<InputStream> rawResponse) {
public static GaosApiException from(String message, HttpResponse<InputStream> rawResponse) {
return from(message, rawResponse, null);
}

public static SDKException from(String message, HttpResponse<InputStream> rawResponse, @Nullable Throwable cause) {
public static GaosApiException from(String message, HttpResponse<InputStream> rawResponse, @Nullable Throwable cause) {
try {
return new SDKException(
return new GaosApiException(
message, rawResponse.statusCode(), Utils.extractByteArrayFromBody(rawResponse), rawResponse, cause);
} catch (IOException e) {
// Gracefully handle IOExceptions that occur while reading the body
// by returning an error without a body.
return new SDKException(
return new GaosApiException(
message, rawResponse.statusCode(), null, rawResponse, cause);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import java.util.Optional;

@SuppressWarnings("serial")
public abstract class GenAiException extends RuntimeException {
public abstract class GaosBaseException extends com.google.genai.errors.ApiException {

private int code;
private byte[] body;
private HttpResponse<?> rawResponse;

public GenAiException(String message, int code, @Nullable byte[] body, HttpResponse<?> rawResponse, @Nullable Throwable cause) {
super(message, cause);
public GaosBaseException(String message, int code, @Nullable byte[] body, HttpResponse<?> rawResponse, @Nullable Throwable cause) {
super(code, "", message, cause);
Utils.checkNotNull(message, "message");
Utils.checkNotNull(rawResponse, "rawResponse");
this.body = body;
Expand All @@ -52,6 +52,7 @@ public Optional<String> bodyAsString() {
return body().map(x -> new String(x, StandardCharsets.UTF_8));
}

@Override
public int code() {
return code;
}
Expand All @@ -76,22 +77,23 @@ public Headers headers() {
}

// present for backwards compatibility
@Override
public String message() {
return getMessage();
}

public GenAiException withCode(int code) {
public GaosBaseException withCode(int code) {
this.code = code;
return this;
}

public GenAiException withBody(@Nullable byte[] body) {
public GaosBaseException withBody(@Nullable byte[] body) {
Utils.checkNotNull(body, "body");
this.body = body;
return this;
}

public GenAiException withRawResponse(HttpResponse<?> rawResponse) {
public GaosBaseException withRawResponse(HttpResponse<?> rawResponse) {
Utils.checkNotNull(rawResponse, "rawResponse");
this.rawResponse = rawResponse;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Injected by scripts/sync_speakeasy_outputs.py DO NOT EDIT.
*/
package com.google.genai.gaos.models.errors;

import com.google.genai.gaos.utils.Headers;
import com.google.genai.gaos.utils.transport.HttpResponse;
import jakarta.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
* Carrier base bridging gaos 4xx errors onto the native ClientException hierarchy while
* preserving the gaos body/rawResponse/headers surface.
*/
@SuppressWarnings("serial")
public abstract class GaosClientException extends com.google.genai.errors.ClientException {

private byte[] body;
private HttpResponse<?> rawResponse;

public GaosClientException(String message, int code, @Nullable byte[] body, HttpResponse<?> rawResponse, @Nullable Throwable cause) {
super(code, "", message, cause);
this.body = body;
this.rawResponse = rawResponse;
}

public Optional<byte[]> body() {
return Optional.ofNullable(body);
}

public Optional<String> bodyAsString() {
return body().map(x -> new String(x, StandardCharsets.UTF_8));
}

public HttpResponse<?> rawResponse() {
return rawResponse;
}

public Headers headers() {
return new Headers(rawResponse.headers().map());
}
}
Loading
Loading