@@ -437,7 +437,9 @@ void main() {
437437 'totalTokens' : 120 ,
438438 'totalBillableCharacters' : 240 ,
439439 'promptTokensDetails' : [
440- {'modality' : 'TEXT' , 'tokenCount' : 100 },
440+ {
441+ 'modality' : 'TEXT' ,
442+ },
441443 {'modality' : 'IMAGE' , 'tokenCount' : 20 }
442444 ]
443445 };
@@ -447,7 +449,7 @@ void main() {
447449 expect (response.promptTokensDetails, isNotNull);
448450 expect (response.promptTokensDetails, hasLength (2 ));
449451 expect (response.promptTokensDetails! [0 ].modality, ContentModality .text);
450- expect (response.promptTokensDetails! [0 ].tokenCount, 100 );
452+ expect (response.promptTokensDetails! [0 ].tokenCount, 0 );
451453 expect (
452454 response.promptTokensDetails! [1 ].modality, ContentModality .image);
453455 expect (response.promptTokensDetails! [1 ].tokenCount, 20 );
@@ -597,6 +599,47 @@ void main() {
597599 expect (response.candidates.first.finishMessage, isNull);
598600 });
599601
602+ test ('parses usageMetadata for no tokenCount' , () {
603+ final json = {
604+ 'candidates' : [basicCandidateJson],
605+ 'usageMetadata' : {
606+ 'promptTokenCount' : 10 ,
607+ 'candidatesTokenCount' : 20 ,
608+ 'totalTokenCount' : 30 ,
609+ 'promptTokensDetails' : [
610+ {'modality' : 'TEXT' , 'tokenCount' : 10 }
611+ ],
612+ 'candidatesTokensDetails' : [
613+ {
614+ 'modality' : 'TEXT' ,
615+ }
616+ ],
617+ }
618+ };
619+ final response = parseGenerateContentResponse (json);
620+ expect (response.candidates, hasLength (1 ));
621+ expect (response.candidates.first.text, 'Hello world' );
622+ expect (response.candidates.first.finishReason, FinishReason .stop);
623+ expect (response.candidates.first.safetyRatings, isNotNull);
624+ expect (response.candidates.first.safetyRatings, hasLength (1 ));
625+
626+ expect (response.usageMetadata, isNotNull);
627+ expect (response.usageMetadata! .promptTokenCount, 10 );
628+ expect (response.usageMetadata! .candidatesTokenCount, 20 );
629+ expect (response.usageMetadata! .totalTokenCount, 30 );
630+ expect (response.usageMetadata! .promptTokensDetails, hasLength (1 ));
631+ expect (response.usageMetadata! .promptTokensDetails! .first.modality,
632+ ContentModality .text);
633+ expect (
634+ response.usageMetadata! .promptTokensDetails! .first.tokenCount, 10 );
635+ expect (response.usageMetadata! .candidatesTokensDetails, hasLength (1 ));
636+ expect (response.usageMetadata! .candidatesTokensDetails! .first.modality,
637+ ContentModality .text);
638+ expect (
639+ response.usageMetadata! .candidatesTokensDetails! .first.tokenCount,
640+ 0 );
641+ });
642+
600643 test ('parses citationMetadata with "citationSources"' , () {
601644 final json = {
602645 'candidates' : [
0 commit comments