adk-java icon indicating copy to clipboard operation
adk-java copied to clipboard

[BUG] `GoogleSearchTool.processLlmRequest` throws `NoSuchElementException` synchronously on an empty model

Open SuperCorleone opened this issue 5 months ago • 1 comments

Describe the Bug

processLlmRequest does String model = llmRequestBuilder.build().model().get(); — an unguarded Optional.get(). If the model is absent it throws NoSuchElementException synchronously, escaping the returned Completable instead of being delivered via onError. The method's own else branch is written to handle a null model gracefully (returns Completable.error(IllegalArgumentException)), and the sibling GoogleMapsTool.java:67 uses the correct model().orElse(null) — so this is an inconsistency/fragility. In the normal flow the Basic request processor sets the model before any tool runs, so it isn't reached there.

Root Cause

String model = llmRequestBuilder.build().model().get();   // NoSuchElementException if empty; thrown synchronously

Suggested Fix

Use model().orElse(null) (matching GoogleMapsTool) so the existing null-handling branch runs and errors flow through the Completable.

Corresponding Test (generated)

@Test
public void testProcessLlmRequest_Gemini2ModelWithNoModel_ThrowsException() {
    // Given
    GoogleSearchTool tool = GoogleSearchTool.INSTANCE;
    LlmRequest.Builder requestBuilder = LlmRequest.builder();

    // When & Then
    Completable result = tool.processLlmRequest(requestBuilder, null);
    
    // Use a proper observer to capture the error
    AtomicReference<Throwable> capturedException = new AtomicReference<>();
    result.subscribe(
        new CompletableObserver() {
          @Override
          public void onSubscribe(Disposable d) {}
          
          @Override
          public void onComplete() {}
          
          @Override
          public void onError(Throwable e) {
            capturedException.set(e);
          }
        }
    );
    
    assertNotNull(capturedException.get(), "Expected an exception to be thrown");
    assertTrue(capturedException.get() instanceof IllegalArgumentException);
    assertTrue(capturedException.get().getMessage().contains("Google search tool is not supported"));
}

Environment

  • Project: adk / google-adk (com.google.adk) · upstream: github.com/google/adk-java
  • Version: 0.5.1-SNAPSHOT (local) · Commit: <fill SUT commit> · Java 17 · OS macOS

This input was generated by the test case generator TestFusion developed in our STAR lab.

SuperCorleone avatar Jun 16 '26 14:06 SuperCorleone

Hi @SuperCorleone, Thank you for reporting this issue. I am able to reproduce this issue in latest version which you described and I have created this PR to resolve this problem. hope this PR will resolve your issue?

hemasekhar-p avatar Jun 19 '26 10:06 hemasekhar-p