From 8e5e9a9162cd609250708fb7510ea0dc09f8da69 Mon Sep 17 00:00:00 2001 From: raj Date: Sat, 14 Nov 2026 03:06:43 +0530 Subject: [PATCH] fix(core): replace unsafe Optional.get() calls with orElseThrow Replace Optional.get() calls in LLM flow request processors with orElseThrow() and meaningful error messages, so missing values surface as IllegalStateException with context instead of an opaque NoSuchElementException. --- .../com/google/adk/flows/llmflows/Basic.java | 16 +++++++++--- .../adk/flows/llmflows/OutputSchema.java | 9 ++++++- ...equestConfirmationLlmRequestProcessor.java | 25 +++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/google/adk/flows/llmflows/Basic.java b/core/src/main/java/com/google/adk/flows/llmflows/Basic.java index 423891001..e3a214f34 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/Basic.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/Basic.java @@ -38,9 +38,19 @@ public Single processRequest( } LlmAgent agent = (LlmAgent) context.agent(); String modelName = - agent.resolvedModel().model().isPresent() - ? agent.resolvedModel().model().get().model() - : agent.resolvedModel().modelName().get(); + agent + .resolvedModel() + .model() + .map(model -> model.model()) + .orElseGet( + () -> + agent + .resolvedModel() + .modelName() + .orElseThrow( + () -> + new IllegalStateException( + "Both model and modelName are not present in resolvedModel"))); LiveConnectConfig.Builder liveConnectConfigBuilder = LiveConnectConfig.builder().responseModalities(context.runConfig().responseModalities()); diff --git a/core/src/main/java/com/google/adk/flows/llmflows/OutputSchema.java b/core/src/main/java/com/google/adk/flows/llmflows/OutputSchema.java index d8d245bf6..ac798c077 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/OutputSchema.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/OutputSchema.java @@ -59,7 +59,14 @@ public Single processRequest( } // Add the set_model_response tool to handle structured output - SetModelResponseTool setResponseTool = new SetModelResponseTool(agent.outputSchema().get()); + SetModelResponseTool setResponseTool = + new SetModelResponseTool( + agent + .outputSchema() + .orElseThrow( + () -> + new IllegalStateException( + "outputSchema should be present when toolsUnion is not empty"))); LlmRequest.Builder builder = request.toBuilder(); return setResponseTool diff --git a/core/src/main/java/com/google/adk/flows/llmflows/RequestConfirmationLlmRequestProcessor.java b/core/src/main/java/com/google/adk/flows/llmflows/RequestConfirmationLlmRequestProcessor.java index b769c33bb..1024466d4 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/RequestConfirmationLlmRequestProcessor.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/RequestConfirmationLlmRequestProcessor.java @@ -75,9 +75,12 @@ public Single processRequest( return Single.just(RequestProcessingResult.create(llmRequest, ImmutableList.of())); } - int finalConfirmationEventIndex = confirmationResult.get().eventIndex(); + ConfirmationResult result = + confirmationResult.orElseThrow( + () -> new IllegalStateException("confirmationResult should be present when not empty")); + int finalConfirmationEventIndex = result.eventIndex(); ImmutableMap requestConfirmationFunctionResponses = - confirmationResult.get().responses(); + result.responses(); String agentName = invocationContext.agent().name(); ImmutableMap functionCallsById = functionCallsById(events, agentName); @@ -129,10 +132,22 @@ public Single processRequest( ofc, functionCallsById, confirmationRequestedIds, agentName)) .ifPresent( ofc -> { + String functionId = + ofc.id() + .orElseThrow( + () -> + new IllegalStateException( + "Function call should have an ID")); + String confirmationId = + fc.id() + .orElseThrow( + () -> + new IllegalStateException( + "Function call should have an ID")); toolsToResumeWithConfirmation.put( - ofc.id().get(), - requestConfirmationFunctionResponses.get(fc.id().get())); - toolsToResumeWithArgs.put(ofc.id().get(), ofc); + functionId, + requestConfirmationFunctionResponses.get(confirmationId)); + toolsToResumeWithArgs.put(functionId, ofc); })); // If all confirmed tools in this event have already been processed, continue