Skip to content

Commit a1154c9

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: include usageMetadata in stream aggregated response
PiperOrigin-RevId: 609495567
1 parent 26c8b82 commit a1154c9

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/functions/post_fetch_processing.ts

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ function aggregateResponses(
171171
const aggregatedResponse: GenerateContentResponse = {
172172
candidates: [],
173173
promptFeedback: lastResponse.promptFeedback,
174+
usageMetadata: lastResponse.usageMetadata,
174175
};
175176
for (const response of responses) {
176177
for (let i = 0; i < response.candidates.length; i++) {

system_test/end_to_end_sample_test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ describe('generateContentStream', () => {
173173

174174
const aggregatedResp = await streamingResp.response;
175175
expect(aggregatedResp.candidates[0]).toBeTruthy(
176-
`sys test failure on generateContentStream for aggregated response: ${aggregatedResp}`
176+
`sys test failure on generateContentStream for testing candidates in aggregated response: ${aggregatedResp}`
177+
);
178+
expect(aggregatedResp.usageMetadata).toBeTruthy(
179+
`sys test failure on generateContentStream for testing usageMetadata in aggregated response: ${aggregatedResp}`
177180
);
178181
});
179182
it('in preview should should return a stream and aggregated response when passed text', async () => {
@@ -188,7 +191,10 @@ describe('generateContentStream', () => {
188191

189192
const aggregatedResp = await streamingResp.response;
190193
expect(aggregatedResp.candidates[0]).toBeTruthy(
191-
`sys test failure on generateContentStream in preview for aggregated response: ${aggregatedResp}`
194+
`sys test failure on generateContentStream in preview for testing candidates in aggregated response: ${aggregatedResp}`
195+
);
196+
expect(aggregatedResp.usageMetadata).toBeTruthy(
197+
`sys test failure on generateContentStream in preview for testing usageMetadata in aggregated response: ${aggregatedResp}`
192198
);
193199
});
194200

0 commit comments

Comments
 (0)