@@ -15,6 +15,7 @@ import {
1515 FunctionDeclaration ,
1616 GenerateContentResponse ,
1717 HttpOptions ,
18+ HttpRetryOptions ,
1819 Modality ,
1920 Part ,
2021} from '../../../src/types.js' ;
@@ -630,7 +631,162 @@ describe('Client Tests', () => {
630631 } ) ,
631632 ) ;
632633 } ) ;
634+ it ( 'ML Dev should generate content with retry options provided to the client' , async ( ) => {
635+ const retryOptions : HttpRetryOptions = {
636+ attempts : 3 ,
637+ initialDelay : 1000 ,
638+ maxDelay : 5000 ,
639+ httpStatusCodes : [ 500 , 503 ] ,
640+ } ;
641+ const httpOptionsWithRetryOptions : HttpOptions = httpOptions ;
642+ httpOptionsWithRetryOptions . retryOptions = retryOptions ;
643+
644+ const client = new GoogleGenAI ( {
645+ vertexai : false ,
646+ apiKey : GOOGLE_API_KEY ,
647+ httpOptions : httpOptionsWithRetryOptions ,
648+ } ) ;
649+ const response = await client . models . generateContent ( {
650+ model : MODEL ,
651+ contents : 'why is the sky blue?' ,
652+ config : {
653+ maxOutputTokens : 200 ,
654+ candidateCount : 1 ,
655+ thinkingConfig : { thinkingBudget : 50 } ,
656+ } ,
657+ } ) ;
658+ expect ( response . candidates ! . length ) . toBe (
659+ 1 ,
660+ 'Expected 1 candidate got ' + response . candidates ! . length ,
661+ ) ;
662+ expect ( response . usageMetadata ! . candidatesTokenCount ) . toBeLessThanOrEqual (
663+ 250 ,
664+ 'Expected candidatesTokenCount to be less than or equal to 250, got ' +
665+ response . usageMetadata ! . candidatesTokenCount ,
666+ ) ;
667+ console . info (
668+ 'ML Dev should generate content with retry options provided to the client\n' ,
669+ response . text ,
670+ ) ;
671+ } , 60000 ) ;
633672 } ) ;
673+ it ( 'Vertex AI should generate content with retry options provided to the client' , async ( ) => {
674+ const retryOptions : HttpRetryOptions = {
675+ attempts : 3 ,
676+ initialDelay : 1000 ,
677+ maxDelay : 5000 ,
678+ httpStatusCodes : [ 500 , 503 ] ,
679+ } ;
680+ const httpOptionsWithRetryOptions : HttpOptions = httpOptions ;
681+ httpOptionsWithRetryOptions . retryOptions = retryOptions ;
682+
683+ const client = new GoogleGenAI ( {
684+ vertexai : true ,
685+ project : GOOGLE_CLOUD_PROJECT ,
686+ location : GOOGLE_CLOUD_LOCATION ,
687+ httpOptions : httpOptionsWithRetryOptions ,
688+ } ) ;
689+ const response = await client . models . generateContent ( {
690+ model : MODEL ,
691+ contents : 'why is the sky blue?' ,
692+ config : {
693+ maxOutputTokens : 200 ,
694+ candidateCount : 1 ,
695+ thinkingConfig : { thinkingBudget : 50 } ,
696+ } ,
697+ } ) ;
698+ expect ( response . candidates ! . length ) . toBe (
699+ 1 ,
700+ 'Expected 1 candidate got ' + response . candidates ! . length ,
701+ ) ;
702+ expect ( response . usageMetadata ! . candidatesTokenCount ) . toBeLessThanOrEqual (
703+ 250 ,
704+ 'Expected candidatesTokenCount to be less than or equal to 250, got ' +
705+ response . usageMetadata ! . candidatesTokenCount ,
706+ ) ;
707+ console . info (
708+ 'Vertex AI should generate content with retry options provided to the client\n' ,
709+ response . text ,
710+ ) ;
711+ } , 30000 ) ;
712+ it ( 'ML Dev should generate content with per-request retry options' , async ( ) => {
713+ const retryOptions : HttpRetryOptions = {
714+ attempts : 3 ,
715+ initialDelay : 1000 ,
716+ maxDelay : 5000 ,
717+ httpStatusCodes : [ 500 , 503 ] ,
718+ } ;
719+ const httpOptionsWithRetryOptions : HttpOptions = httpOptions ;
720+ httpOptionsWithRetryOptions . retryOptions = retryOptions ;
721+
722+ const client = new GoogleGenAI ( {
723+ vertexai : false ,
724+ apiKey : GOOGLE_API_KEY ,
725+ httpOptions : httpOptionsWithRetryOptions ,
726+ } ) ;
727+ const response = await client . models . generateContent ( {
728+ model : MODEL ,
729+ contents : 'why is the sky blue?' ,
730+ config : {
731+ maxOutputTokens : 200 ,
732+ candidateCount : 1 ,
733+ thinkingConfig : { thinkingBudget : 50 } ,
734+ httpOptions : httpOptionsWithRetryOptions ,
735+ } ,
736+ } ) ;
737+ expect ( response . candidates ! . length ) . toBe (
738+ 1 ,
739+ 'Expected 1 candidate got ' + response . candidates ! . length ,
740+ ) ;
741+ expect ( response . usageMetadata ! . candidatesTokenCount ) . toBeLessThanOrEqual (
742+ 250 ,
743+ 'Expected candidatesTokenCount to be less than or equal to 250, got ' +
744+ response . usageMetadata ! . candidatesTokenCount ,
745+ ) ;
746+ console . info (
747+ 'ML Dev should generate content with per-request retry options\n' ,
748+ response . text ,
749+ ) ;
750+ } , 30000 ) ;
751+ it ( 'Vertex AI should generate content with per-request retry options' , async ( ) => {
752+ const retryOptions : HttpRetryOptions = {
753+ attempts : 3 ,
754+ initialDelay : 1000 ,
755+ maxDelay : 5000 ,
756+ httpStatusCodes : [ 500 , 503 ] ,
757+ } ;
758+ const httpOptionsWithRetryOptions : HttpOptions = httpOptions ;
759+ httpOptionsWithRetryOptions . retryOptions = retryOptions ;
760+
761+ const client = new GoogleGenAI ( {
762+ vertexai : false ,
763+ apiKey : GOOGLE_API_KEY ,
764+ httpOptions,
765+ } ) ;
766+ const response = await client . models . generateContent ( {
767+ model : MODEL ,
768+ contents : 'why is the sky blue?' ,
769+ config : {
770+ maxOutputTokens : 200 ,
771+ candidateCount : 1 ,
772+ thinkingConfig : { thinkingBudget : 50 } ,
773+ httpOptions : httpOptionsWithRetryOptions ,
774+ } ,
775+ } ) ;
776+ expect ( response . candidates ! . length ) . toBe (
777+ 1 ,
778+ 'Expected 1 candidate got ' + response . candidates ! . length ,
779+ ) ;
780+ expect ( response . usageMetadata ! . candidatesTokenCount ) . toBeLessThanOrEqual (
781+ 250 ,
782+ 'Expected candidatesTokenCount to be less than or equal to 250, got ' +
783+ response . usageMetadata ! . candidatesTokenCount ,
784+ ) ;
785+ console . info (
786+ 'Vertex AI should generate content with per-request retry options\n' ,
787+ response . text ,
788+ ) ;
789+ } , 30000 ) ;
634790 it ( 'Vertex AI can use JSON schema in parametersJsonSchema to build FunctionDeclaration' , async ( ) => {
635791 const stringArgument = z . object ( {
636792 firstString : z . string ( ) ,
0 commit comments