Skip to content
Merged
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 @@ -611,7 +611,8 @@ private Flowable<Event> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -575,4 +576,32 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> 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<Event> 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());
}
}
8 changes: 8 additions & 0 deletions core/src/test/java/com/google/adk/testing/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down