Repository navigation
fix(core): replace unsafe Optional.get() calls with orElseThrow - #1809
Rajkumar7633 wants to merge 1 commit into
Conversation
|
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.
7c5f3bd to
8e5e9a9
Compare
|
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. |
There was a problem hiding this comment.
Hello @Rajkumar7633, thank you for your contribution. Apart from the comments left in code, there are two additional comments:
- I think that the
fixis the wrong prefix for the PR title.refactoris a more appropriate term. - 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)) { |
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
This filters out empty fc.id()s so later check is never executed.
| && requestConfirmationFunctionResponses.containsKey(fc.id().get())) | ||
| .forEach( | ||
| fc -> | ||
| getOriginalFunctionCall(fc) |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
should be present when not empty says the same thing twice.
| agent | ||
| .resolvedModel() |
There was a problem hiding this comment.
LlmAgent.resolveModelInternal always builds a Model with both modelName and model set, or throws itself, so the later check is never executed.
|
Thanks for the thorough review — you're right on all counts, and I've verified each point:
Conclusion: every |
|
Closing as the changes are cosmetic — all Optional.get() call sites are already guarded, so there is no behavioral difference worth merging. |
Description
Replaces unsafe
Optional.get()calls in the LLM flow request processors withorElseThrow()and meaningful error messages. Previously, a missing value would surface as an opaqueNoSuchElementException; now it throwsIllegalStateExceptionwith 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 usesmap/orElseGet/orElseThrowinstead ofisPresent()/get()OutputSchema.java:outputSchema()accessed viaorElseThrowwith a descriptive messageRequestConfirmationLlmRequestProcessor.java:confirmationResultand function call IDs accessed viaorElseThrowinstead ofget()Testing
./mvnw test -pl core -Dtest='BasicTest,OutputSchemaTest,RequestConfirmationLlmRequestProcessorTest'— all 50 tests pass