Generate text with Gemini Live API

This sample demonstrates how to generate text using Gemini Live API

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Python API reference documentation .

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google 
  
 import 
 genai 
 from 
  
 google.genai.types 
  
 import 
 ( 
 Content 
 , 
 HttpOptions 
 , 
 LiveConnectConfig 
 , 
 Modality 
 , 
 Part 
 ) 
 client 
 = 
 genai 
 . 
 Client 
 ( 
 http_options 
 = 
 HttpOptions 
 ( 
 api_version 
 = 
 "v1beta1" 
 )) 
 model_id 
 = 
 "gemini-2.0-flash-live-preview-04-09" 
 async 
 with 
 client 
 . 
 aio 
 . 
 live 
 . 
 connect 
 ( 
 model 
 = 
 model_id 
 , 
 config 
 = 
 LiveConnectConfig 
 ( 
 response_modalities 
 = 
 [ 
 Modality 
 . 
 TEXT 
 ]), 
 ) 
 as 
 session 
 : 
 text_input 
 = 
 "Hello? Gemini, are you there?" 
 print 
 ( 
 "> " 
 , 
 text_input 
 , 
 " 
 \n 
 " 
 ) 
 await 
 session 
 . 
 send_client_content 
 ( 
 turns 
 = 
 Content 
 ( 
 role 
 = 
 "user" 
 , 
 parts 
 = 
 [ 
 Part 
 ( 
 text 
 = 
 text_input 
 )]) 
 ) 
 response 
 = 
 [] 
 async 
 for 
 message 
 in 
 session 
 . 
 receive 
 (): 
 if 
 message 
 . 
 text 
 : 
 response 
 . 
 append 
 ( 
 message 
 . 
 text 
 ) 
 print 
 ( 
 "" 
 . 
 join 
 ( 
 response 
 )) 
 # Example output: 
 # >  Hello? Gemini, are you there? 
 # Yes, I'm here. What would you like to talk about? 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: