diff --git a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java index 6ca49ee62..549652e86 100644 --- a/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java +++ b/core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java @@ -611,7 +611,8 @@ private Flowable buildPostprocessingEvents( if (updatedResponse.content().isEmpty() && updatedResponse.errorCode().isEmpty() && !updatedResponse.interrupted().orElse(false) - && !updatedResponse.turnComplete().orElse(false)) { + && !updatedResponse.turnComplete().orElse(false) + && updatedResponse.usageMetadata().isEmpty()) { return processorEvents; } diff --git a/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java b/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java index 657d1c670..ff151a0b2 100644 --- a/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java +++ b/core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java @@ -17,6 +17,7 @@ package com.google.adk.flows.llmflows; import static com.google.adk.testing.TestUtils.assertEqualIgnoringFunctionIds; +import static com.google.adk.testing.TestUtils.createGenerateContentResponseUsageMetadata; import static com.google.adk.testing.TestUtils.createInvocationContext; import static com.google.adk.testing.TestUtils.createLlmResponse; import static com.google.adk.testing.TestUtils.createTestAgent; @@ -575,4 +576,32 @@ public Single> runAsync(Map args, ToolContex return Single.just(response); } } + + @Test + public void postprocess_noResponseProcessors_onlyUsageMetadata_returnsEvent() { + GenerateContentResponseUsageMetadata usageMetadata = + createGenerateContentResponseUsageMetadata().build(); + LlmResponse llmResponse = LlmResponse.builder().usageMetadata(usageMetadata).build(); + InvocationContext invocationContext = + createInvocationContext(createTestAgent(createTestLlm(llmResponse))); + BaseLlmFlow baseLlmFlow = createBaseLlmFlowWithoutProcessors(); + Event baseEvent = + Event.builder() + .invocationId(invocationContext.invocationId()) + .author(invocationContext.agent().name()) + .build(); + + List events = + baseLlmFlow + .postprocess(invocationContext, baseEvent, LlmRequest.builder().build(), llmResponse) + .toList() + .blockingGet(); + + assertThat(events).hasSize(1); + Event event = getOnlyElement(events); + assertThat(event.content()).isEmpty(); + assertThat(event.usageMetadata()).hasValue(usageMetadata); + assertThat(event.author()).isEqualTo(invocationContext.agent().name()); + assertThat(event.invocationId()).isEqualTo(invocationContext.invocationId()); + } } diff --git a/core/src/test/java/com/google/adk/testing/TestUtils.java b/core/src/test/java/com/google/adk/testing/TestUtils.java index df94b76b2..70ae14bf1 100644 --- a/core/src/test/java/com/google/adk/testing/TestUtils.java +++ b/core/src/test/java/com/google/adk/testing/TestUtils.java @@ -41,6 +41,7 @@ import com.google.genai.types.FunctionCall; import com.google.genai.types.FunctionDeclaration; import com.google.genai.types.FunctionResponse; +import com.google.genai.types.GenerateContentResponseUsageMetadata; import com.google.genai.types.Part; import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Single; @@ -253,6 +254,13 @@ public static LlmResponse createFunctionCallLlmResponse( return createLlmResponse(content); } + public static GenerateContentResponseUsageMetadata.Builder + createGenerateContentResponseUsageMetadata() { + return GenerateContentResponseUsageMetadata.builder() + .promptTokenCount(10) + .candidatesTokenCount(20); + } + public static class EchoTool extends BaseTool { public EchoTool() { super("echo_tool", "description");