Skip to content

Commit 8137d66

Browse files
tilgalascopybara-github
authored andcommitted
fix: include usage_metadata events in live postprocessing
PiperOrigin-RevId: 871751282
1 parent 4ac1dd2 commit 8137d66

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

core/src/main/java/com/google/adk/flows/llmflows/BaseLlmFlow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ private Flowable<Event> buildPostprocessingEvents(
611611
if (updatedResponse.content().isEmpty()
612612
&& updatedResponse.errorCode().isEmpty()
613613
&& !updatedResponse.interrupted().orElse(false)
614-
&& !updatedResponse.turnComplete().orElse(false)) {
614+
&& !updatedResponse.turnComplete().orElse(false)
615+
&& updatedResponse.usageMetadata().isEmpty()) {
615616
return processorEvents;
616617
}
617618

core/src/test/java/com/google/adk/flows/llmflows/BaseLlmFlowTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.adk.flows.llmflows;
1818

1919
import static com.google.adk.testing.TestUtils.assertEqualIgnoringFunctionIds;
20+
import static com.google.adk.testing.TestUtils.createGenerateContentResponseUsageMetadata;
2021
import static com.google.adk.testing.TestUtils.createInvocationContext;
2122
import static com.google.adk.testing.TestUtils.createLlmResponse;
2223
import static com.google.adk.testing.TestUtils.createTestAgent;
@@ -575,4 +576,32 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
575576
return Single.just(response);
576577
}
577578
}
579+
580+
@Test
581+
public void postprocess_noResponseProcessors_onlyUsageMetadata_returnsEvent() {
582+
GenerateContentResponseUsageMetadata usageMetadata =
583+
createGenerateContentResponseUsageMetadata().build();
584+
LlmResponse llmResponse = LlmResponse.builder().usageMetadata(usageMetadata).build();
585+
InvocationContext invocationContext =
586+
createInvocationContext(createTestAgent(createTestLlm(llmResponse)));
587+
BaseLlmFlow baseLlmFlow = createBaseLlmFlowWithoutProcessors();
588+
Event baseEvent =
589+
Event.builder()
590+
.invocationId(invocationContext.invocationId())
591+
.author(invocationContext.agent().name())
592+
.build();
593+
594+
List<Event> events =
595+
baseLlmFlow
596+
.postprocess(invocationContext, baseEvent, LlmRequest.builder().build(), llmResponse)
597+
.toList()
598+
.blockingGet();
599+
600+
assertThat(events).hasSize(1);
601+
Event event = getOnlyElement(events);
602+
assertThat(event.content()).isEmpty();
603+
assertThat(event.usageMetadata()).hasValue(usageMetadata);
604+
assertThat(event.author()).isEqualTo(invocationContext.agent().name());
605+
assertThat(event.invocationId()).isEqualTo(invocationContext.invocationId());
606+
}
578607
}

core/src/test/java/com/google/adk/testing/TestUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.genai.types.FunctionCall;
4242
import com.google.genai.types.FunctionDeclaration;
4343
import com.google.genai.types.FunctionResponse;
44+
import com.google.genai.types.GenerateContentResponseUsageMetadata;
4445
import com.google.genai.types.Part;
4546
import io.reactivex.rxjava3.core.Flowable;
4647
import io.reactivex.rxjava3.core.Single;
@@ -253,6 +254,13 @@ public static LlmResponse createFunctionCallLlmResponse(
253254
return createLlmResponse(content);
254255
}
255256

257+
public static GenerateContentResponseUsageMetadata.Builder
258+
createGenerateContentResponseUsageMetadata() {
259+
return GenerateContentResponseUsageMetadata.builder()
260+
.promptTokenCount(10)
261+
.candidatesTokenCount(20);
262+
}
263+
256264
public static class EchoTool extends BaseTool {
257265
public EchoTool() {
258266
super("echo_tool", "description");

0 commit comments

Comments
 (0)