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 @@ -257,10 +257,12 @@ private Flowable<Event> callLlm(
agent.resolvedModel().modelName().get());
LlmRequest finalLlmRequest = llmRequestBuilder.build();

return llm.generateContent(
finalLlmRequest,
context.runConfig().streamingMode()
== StreamingMode.SSE)
return Flowable.defer(
() ->
llm.generateContent(
finalLlmRequest,
context.runConfig().streamingMode()
== StreamingMode.SSE))
.onErrorResumeNext(
exception ->
handleOnModelErrorCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;

import com.google.adk.agents.Callbacks;
import com.google.adk.agents.InvocationContext;
Expand Down Expand Up @@ -72,6 +76,64 @@
@RunWith(JUnit4.class)
public final class BaseLlmFlowTest {

@Test
public void run_synchronousModelError_callbackReturnsFallback() {
RuntimeException modelError = new RuntimeException("Model request failed");
Content fallbackContent = Content.fromParts(Part.fromText("Fallback response"));
AtomicInteger callbackCalls = new AtomicInteger();

TestLlm testLlm = spy(createTestLlm(createLlmResponse(fallbackContent)));
doThrow(modelError).when(testLlm).generateContent(any(LlmRequest.class), anyBoolean());

Callbacks.OnModelErrorCallback onErrorCallback =
(ctx, request, error) -> {
callbackCalls.incrementAndGet();
assertThat(error).isSameInstanceAs(modelError);
return Maybe.just(createLlmResponse(fallbackContent));
};

InvocationContext invocationContext =
createInvocationContext(
createTestAgentBuilder(testLlm).onModelErrorCallback(onErrorCallback).build());

List<Event> events =
createBaseLlmFlowWithoutProcessors().run(invocationContext).toList().blockingGet();

assertThat(callbackCalls.get()).isEqualTo(1);
assertThat(events).hasSize(1);
assertThat(getOnlyElement(events).content()).hasValue(fallbackContent);
}

@Test
public void run_synchronousModelError_emptyCallbackPropagatesOriginalError() {
RuntimeException modelError = new RuntimeException("Model request failed");
AtomicInteger callbackCalls = new AtomicInteger();

TestLlm testLlm =
spy(createTestLlm(createLlmResponse(Content.fromParts(Part.fromText("Unused")))));
doThrow(modelError).when(testLlm).generateContent(any(LlmRequest.class), anyBoolean());

Callbacks.OnModelErrorCallback onErrorCallback =
(ctx, request, error) -> {
callbackCalls.incrementAndGet();
assertThat(error).isSameInstanceAs(modelError);
return Maybe.empty();
};

InvocationContext invocationContext =
createInvocationContext(
createTestAgentBuilder(testLlm).onModelErrorCallback(onErrorCallback).build());

RuntimeException thrown =
assertThrows(
RuntimeException.class,
() ->
createBaseLlmFlowWithoutProcessors().run(invocationContext).toList().blockingGet());

assertThat(thrown).isSameInstanceAs(modelError);
assertThat(callbackCalls.get()).isEqualTo(1);
}

@Test
public void run_singleTextResponse_returnsSingleEvent() {
Content content = Content.fromParts(Part.fromText("LLM response"));
Expand Down
Loading