Generate content using Vertex AI Express Mode

This sample utilizes Vertex AI Express Mode to generate text content.

Explore further

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

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Go 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 .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "google.golang.org/genai" 
 ) 
 var 
  
 newClient 
  
 = 
  
 genai 
 . 
 NewClient 
 // generateContent shows how to use Vertex AI Express mode with an API key. 
 func 
  
 generateContentWithApiKey 
 ( 
 w 
  
 io 
 . 
 Writer 
 ) 
  
 error 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // TODO(developer): Replace with your actual API key 
  
 apiKey 
  
 := 
  
 "YOUR_API_KEY" 
  
 client 
 , 
  
 err 
  
 := 
  
 newClient 
 ( 
 ctx 
 , 
  
& genai 
 . 
 ClientConfig 
 { 
  
 APIKey 
 : 
  
 apiKey 
 , 
  
 HTTPOptions 
 : 
  
 genai 
 . 
 HTTPOptions 
 { 
 APIVersion 
 : 
  
 "v1" 
 }, 
  
 }) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to create genai client: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 modelName 
  
 := 
  
 "gemini-2.5-flash" 
  
 resp 
 , 
  
 err 
  
 := 
  
 client 
 . 
 Models 
 . 
 GenerateContent 
 ( 
 ctx 
 , 
  
 modelName 
 , 
  
 [] 
 * 
 genai 
 . 
 Content 
 { 
  
 { 
 Parts 
 : 
  
 [] 
 * 
 genai 
 . 
 Part 
 { 
  
 { 
 Text 
 : 
  
 "Explain bubble sort to me." 
 }, 
  
 }, 
  
 Role 
 : 
  
 genai 
 . 
 RoleUser 
 }, 
  
 }, 
  
 nil 
 , 
  
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to generate content: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintln 
 ( 
 w 
 , 
  
 resp 
 . 
 Text 
 ()) 
  
 // Example response: 
  
 // Bubble Sort is a simple sorting algorithm that repeatedly steps through the list 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Java 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 .

  import 
  
 com.google.genai.Client 
 ; 
 import 
  
 com.google.genai.types.GenerateContentConfig 
 ; 
 import 
  
 com.google.genai.types.GenerateContentResponse 
 ; 
 public 
  
 class 
 ExpressModeWithApiKey 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 modelId 
  
 = 
  
 "gemini-2.5-flash" 
 ; 
  
 String 
  
 apiKey 
  
 = 
  
 "YOUR_API_KEY" 
 ; 
  
 generateContent 
 ( 
 modelId 
 , 
  
 apiKey 
 ); 
  
 } 
  
 // Generates content with Vertex AI Api key. 
  
 public 
  
 static 
  
 String 
  
 generateContent 
 ( 
 String 
  
 modelId 
 , 
  
 String 
  
 apiKey 
 ) 
  
 { 
  
 // Client Initialization. Once created, it can be reused for multiple requests. 
  
 try 
  
 ( 
 Client 
  
 client 
  
 = 
  
 Client 
 . 
 builder 
 (). 
 apiKey 
 ( 
 apiKey 
 ). 
 vertexAI 
 ( 
 true 
 ). 
 build 
 ()) 
  
 { 
  
 GenerateContentResponse 
  
 response 
  
 = 
  
 client 
 . 
 models 
 . 
 generateContent 
 ( 
  
 modelId 
 , 
  
 "Explain bubble sort to me." 
 , 
  
 GenerateContentConfig 
 . 
 builder 
 (). 
 build 
 ()); 
  
 System 
 . 
 out 
 . 
 print 
 ( 
 response 
 . 
 text 
 ()); 
  
 // Example response: 
  
 // Bubble sort is one of the simplest sorting algorithms. It's often used to introduce the 
  
 // concept of sorting because its logic is very straightforward. 
  
 // 
  
 // Imagine you have a list of numbers that you want to put in order, like `[5, 1, 4, 2, 8]`. 
  
 // ... 
  
 return 
  
 response 
 . 
 text 
 (); 
  
 } 
  
 } 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vertex AI quickstart using client libraries . For more information, see the Vertex AI Node.js 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 .

  const 
  
 { 
 GoogleGenAI 
 } 
  
 = 
  
 require 
 ( 
 '@google/genai' 
 ); 
 const 
  
 API_KEY 
  
 = 
  
 'YOUR_EXPRESS_MODE_API_KEY' 
 ; 
 async 
  
 function 
  
 generateWithApiKey 
 ( 
 apiKey 
  
 = 
  
 API_KEY 
 ) 
  
 { 
  
 const 
  
 client 
  
 = 
  
 new 
  
 GoogleGenAI 
 ({ 
  
 vertexai 
 : 
  
 true 
 , 
  
 apiKey 
 : 
  
 apiKey 
 , 
  
 }); 
  
 const 
  
 response 
  
 = 
  
 await 
  
 client 
 . 
 models 
 . 
 generateContentStream 
 ({ 
  
 model 
 : 
  
 'gemini-2.5-flash' 
 , 
  
 contents 
 : 
  
 'Explain bubble sort to me.' 
 , 
  
 }); 
  
 console 
 . 
 log 
 ( 
 response 
 . 
 text 
 ); 
  
 // Example response: 
  
 //  Bubble Sort is a simple sorting algorithm that repeatedly steps through the list 
  
 return 
  
 response 
 ; 
 } 
 

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 
 # TODO(developer): Update below line 
 API_KEY 
 = 
 "YOUR_API_KEY" 
 client 
 = 
 genai 
 . 
 Client 
 ( 
 vertexai 
 = 
 True 
 , 
 api_key 
 = 
 API_KEY 
 ) 
 response 
 = 
 client 
 . 
 models 
 . 
 generate_content 
 ( 
 model 
 = 
 "gemini-2.5-flash" 
 , 
 contents 
 = 
 "Explain bubble sort to me." 
 , 
 ) 
 print 
 ( 
 response 
 . 
 text 
 ) 
 # Example response: 
 # Bubble Sort is a simple sorting algorithm that repeatedly steps through the list 
 

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: