Skip to content

fix(core): replace unsafe Optional.get() calls with orElseThrow - #1809

Closed
Rajkumar7633 wants to merge 1 commit into
google:mainfrom
Rajkumar7633:fix/unsafe-optional-get
Closed

Rajkumar7633 wants to merge 1 commit into
google:mainfrom
Rajkumar7633:fix/unsafe-optional-get

Conversation

@Rajkumar7633

Copy link
Copy Markdown

Description

Replaces unsafe Optional.get() calls in the LLM flow request processors with orElseThrow() and meaningful error messages. Previously, a missing value would surface as an opaque NoSuchElementException; now it throws IllegalStateException with context about which value was expected.

This was split out of #1386 per review feedback, since it changes core behavior and is unrelated to the code assistant tutorial.

Changes

  • Basic.java: model name resolution now uses map/orElseGet/orElseThrow instead of isPresent()/get()
  • OutputSchema.java: outputSchema() accessed via orElseThrow with a descriptive message
  • RequestConfirmationLlmRequestProcessor.java: confirmationResult and function call IDs accessed via orElseThrow instead of get()

Testing

  • ./mvnw test -pl core -Dtest='BasicTest,OutputSchemaTest,RequestConfirmationLlmRequestProcessorTest' — all 50 tests pass

@google-cla

google-cla Bot commented Nov 13, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.
@hemasekhar-p

Copy link
Copy Markdown
Contributor

Hi @Rajkumar7633, Thank you for your contribution and for taking the time to submit this pull request. Our team is currently reviewing your changes, and we will reach out if we need any further information.

@MiloszSobczyk MiloszSobczyk left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Rajkumar7633, thank you for your contribution. Apart from the comments left in code, there are two additional comments:

  1. I think that the fix is the wrong prefix for the PR title. refactor is a more appropriate term.
  2. Is there a case where your changes modify the existing behaviour? It seems to me that the changes are purely cosmetic. If possible, please explain what this PR is trying to achieve, or provide a code example, so I can examine it correctly. There is a chance that I am not seeing something obvious here. :)

LlmAgent agent = (LlmAgent) context.agent();
String modelName = request.model().orElse("");

if (agent.outputSchema().isEmpty() || !needsSetModelResponseTool(agent, modelName)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line checks whether agent.OutputSchema() is empty and in case it is, it exits the method early, so the newly added lines will never execute.

}

Optional<ConfirmationResult> confirmationResult = findMostRecentConfirmations(events);
if (confirmationResult.isEmpty()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line checks whether confirmationResult is empty and in case it is, it exits the method early, so the newly added lines will never execute.

event.functionCalls().stream()
.filter(
fc ->
fc.id().isPresent()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filters out empty fc.id()s so later check is never executed.

&& requestConfirmationFunctionResponses.containsKey(fc.id().get()))
.forEach(
fc ->
getOriginalFunctionCall(fc)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOriginalFunctionCall returns empty when id is missing, so later check is never executed.

int finalConfirmationEventIndex = confirmationResult.get().eventIndex();
ConfirmationResult result =
confirmationResult.orElseThrow(
() -> new IllegalStateException("confirmationResult should be present when not empty"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be present when not empty says the same thing twice.

Comment on lines +41 to +42
agent
.resolvedModel()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LlmAgent.resolveModelInternal always builds a Model with both modelName and model set, or throws itself, so the later check is never executed.

@Rajkumar7633

Copy link
Copy Markdown
Author

Thanks for the thorough review — you're right on all counts, and I've verified each point:

  • OutputSchema.processRequest returns early when agent.outputSchema().isEmpty(), so the value is always present at the get() site.
  • RequestConfirmationLlmRequestProcessor returns early when confirmationResult.isEmpty(), the stream filters on fc.id().isPresent(), and getOriginalFunctionCall returns empty when the id is missing — so every get() there is guarded too.
  • LlmAgent.resolveModelInternal indeed always populates both model and modelName or throws itself.

Conclusion: every Optional.get() in these files is already guarded, so the orElseThrow replacements only change the exception type on paths that can't be reached — the changes are cosmetic and add dead error paths. Since this PR solves no real problem, I'm closing it rather than merging noise into core. The original unsafe-.get() concern from #1386 doesn't apply here.

@Rajkumar7633

Copy link
Copy Markdown
Author

Closing as the changes are cosmetic — all Optional.get() call sites are already guarded, so there is no behavioral difference worth merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants