@@ -484,3 +484,95 @@ async def test_async_interactions_vertex_extra_headers_override():
484484 headers = mock_send .call_args [0 ][0 ].headers
485485 assert headers ['x-goog-api-key' ] == 'manual-key'
486486 assert 'authorization' not in headers
487+
488+
489+ _GENERATE_CONTENT_BODY = {
490+ 'candidates' : [{
491+ 'content' : {'role' : 'model' , 'parts' : [{'text' : 'ok' }]},
492+ 'finishReason' : 'STOP' ,
493+ }]
494+ }
495+
496+
497+ def _send_recording_auth (recorded_auth ):
498+ """Returns a fake send that records auth and answers REST or Interactions."""
499+
500+ def send (req , * args , ** kwargs ):
501+ recorded_auth .append ((req .url .path , req .headers .get ('authorization' )))
502+ if '/interactions' in req .url .path :
503+ return Response (
504+ 200 ,
505+ request = Request ('POST' , '' ),
506+ headers = {'content-type' : 'application/json' },
507+ content = '{"status": "completed"}' ,
508+ )
509+ return Response (
510+ 200 , request = Request ('POST' , '' ), json = _GENERATE_CONTENT_BODY
511+ )
512+
513+ return send
514+
515+
516+ def test_interactions_vertex_fresh_token_after_models_call ():
517+ # A REST call before the first Interactions access must not pin its token.
518+ creds = mock .Mock ()
519+ creds .quota_project_id = 'test-quota-project'
520+ client = Client (
521+ vertexai = True ,
522+ project = 'test-project' ,
523+ location = 'us-central1' ,
524+ credentials = creds ,
525+ )
526+ recorded_auth = []
527+
528+ with (
529+ mock .patch .object (
530+ BaseApiClient , '_access_token' , side_effect = ['token-1' , 'token-2' ]
531+ ),
532+ mock .patch .object (
533+ HTTPClient , 'send' , side_effect = _send_recording_auth (recorded_auth )
534+ ),
535+ ):
536+ client .models .generate_content (model = 'gemini-2.5-flash' , contents = 'hi' )
537+ client .interactions .create (model = 'gemini-2.5-flash' , input = 'Hello' )
538+
539+ assert len (recorded_auth ) == 2
540+ assert '/interactions' not in recorded_auth [0 ][0 ]
541+ assert recorded_auth [0 ][1 ] == 'Bearer token-1'
542+ assert recorded_auth [1 ][0 ].endswith ('/interactions' )
543+ assert recorded_auth [1 ][1 ] == 'Bearer token-2'
544+
545+
546+ @pytest .mark .asyncio
547+ async def test_async_interactions_vertex_fresh_token_after_models_call ():
548+ # Async twin; client.aio shares the sync client's BaseApiClient.
549+ creds = mock .Mock ()
550+ creds .quota_project_id = 'test-quota-project'
551+ client = Client (
552+ vertexai = True ,
553+ project = 'test-project' ,
554+ location = 'us-central1' ,
555+ credentials = creds ,
556+ )
557+ recorded_auth = []
558+ sync_send = _send_recording_auth (recorded_auth )
559+
560+ async def async_send (req , * args , ** kwargs ):
561+ return sync_send (req , * args , ** kwargs )
562+
563+ with (
564+ mock .patch .object (
565+ BaseApiClient , '_access_token' , side_effect = ['token-1' , 'token-2' ]
566+ ),
567+ mock .patch .object (HTTPClient , 'send' , side_effect = sync_send ),
568+ mock .patch .object (AsyncHttpxClient , 'send' , side_effect = async_send ),
569+ ):
570+ client .models .generate_content (model = 'gemini-2.5-flash' , contents = 'hi' )
571+ await client .aio .interactions .create (
572+ model = 'gemini-2.5-flash' , input = 'Hello'
573+ )
574+
575+ assert len (recorded_auth ) == 2
576+ assert recorded_auth [0 ][1 ] == 'Bearer token-1'
577+ assert recorded_auth [1 ][0 ].endswith ('/interactions' )
578+ assert recorded_auth [1 ][1 ] == 'Bearer token-2'
0 commit comments