11# Vertex Generative AI SDK for Python
2- The Vertex Generative AI SDK helps developers use Google's generative AI
2+ The Gen AI Modules in the Vertex SDK help developers use Google's generative AI
33[ Gemini models] ( http://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/overview )
4- to build AI-powered features and applications.
5- The SDKs support use cases like the following:
4+ to build AI-powered features and applications in Vertex.
65
7- - Generate text from texts, images and videos (multimodal generation)
8- - Build stateful multi-turn conversations (chat)
9- - Function calling
6+ The modules currently available are: Evaluation, Agent Engines, Prompt Management, and Prompt Optimization. See below for instructions on getting started with each module. For other Gemini features on Vertex, use the [ Gen AI SDK] ( https://github.com/googleapis/python-genai ) .
107
118## Installation
129
@@ -15,12 +12,253 @@ To install the
1512Python package, run the following command:
1613
1714``` shell
18- pip3 install --upgrade --user " google-cloud-aiplatform>=1.38 "
15+ pip3 install --upgrade --user " google-cloud-aiplatform>=1.114.0 "
1916```
2017
21- ## Usage
18+ #### Imports:
19+ ``` python
20+ import vertexai
21+ from vertexai import types
22+ ```
23+
24+ #### Client initialization
25+
26+ ``` python
27+ client = vertexai.Client(project = ' my-project' , location = ' us-central1' )
28+ ```
29+
30+ #### Gen AI Evaluation
31+
32+ To run evaluation, first generate model responses from a set of prompts.
33+
34+ ``` python
35+ import pandas as pd
36+
37+ prompts_df = pd.DataFrame({
38+ " prompt" : [
39+ " What is the capital of France?" ,
40+ " Write a haiku about a cat." ,
41+ " Write a Python function to calculate the factorial of a number." ,
42+ " Translate 'How are you?' to French." ,
43+ ],
44+ })
45+
46+ inference_results = client.evals.run_inference(
47+ model = " gemini-2.5-flash" ,
48+ (" def factorial(n):\n "
49+ " if n < 0:\n "
50+ " return 'Factorial does not exist for negative numbers'\n "
51+ " elif n == 0:\n "
52+ " return 1\n "
53+ " else:\n "
54+ " fact = 1\n "
55+ " i = 1\n "
56+ " while i <= n:\n "
57+ " fact *= i\n "
58+ " i += 1\n "
59+ " return fact" ),
60+ )
61+ inference_results.show()
62+ ```
63+
64+ Then run evaluation by providing the inference results and specifying the metric types.
65+
66+ ``` python
67+ eval_result = client.evals.evaluate(
68+ dataset = inference_results,
69+ metrics = [
70+ types.RubricMetric.GENERAL_QUALITY ,
71+ ]
72+ )
73+ eval_result.show()
74+ ```
75+
76+ #### Agent Engine with Agent Development Kit (ADK)
77+
78+ First, define a function that looks up the exchange rate:
79+
80+ ``` python
81+ def get_exchange_rate (
82+ currency_from : str = " USD" ,
83+ currency_to : str = " EUR" ,
84+ currency_date : str = " latest" ,
85+ ):
86+ """ Retrieves the exchange rate between two currencies on a specified date.
87+
88+ Uses the Frankfurter API (https://api.frankfurter.app/) to obtain
89+ exchange rate data.
90+
91+ Returns:
92+ dict: A dictionary containing the exchange rate information.
93+ Example: {"amount": 1.0, "base": "USD", "date": "2023-11-24",
94+ "rates": {"EUR": 0.95534}}
95+ """
96+ import requests
97+ response = requests.get(
98+ f " https://api.frankfurter.app/ { currency_date} " ,
99+ params = {" from" : currency_from, " to" : currency_to},
100+ )
101+ return response.json()
102+ ```
103+
104+ Next, define an ADK Agent:
105+
106+ ``` python
107+
108+ from google.adk.agents import Agent
109+ from vertexai.agent_engines import AdkApp
110+
111+ app = AdkApp(agent = Agent(
112+ model = " gemini-2.0-flash" , # Required.
113+ name = ' currency_exchange_agent' , # Required.
114+ tools = [get_exchange_rate], # Optional.
115+ ))
116+ ```
117+
118+ Test the agent locally using US dollars and Swedish Krona:
119+
120+ ``` python
121+ async for event in app.async_stream_query(
122+ user_id = " user-id" ,
123+ message = " What is the exchange rate from US dollars to SEK today?" ,
124+ ):
125+ print (event)
126+ ```
127+
128+ To deploy the agent to Agent Engine:
129+
130+ ``` python
131+ remote_app = client.agent_engines.create(
132+ agent = app,
133+ config = {
134+ " requirements" : [" google-cloud-aiplatform[agent_engines,adk]" ],
135+ },
136+ )
137+ ```
138+
139+ You can also run queries against the deployed agent:
140+
141+ ``` python
142+ async for event in remote_app.async_stream_query(
143+ user_id = " user-id" ,
144+ message = " What is the exchange rate from US dollars to SEK today?" ,
145+ ):
146+ print (event)
147+ ```
148+
149+ #### Prompt Optimization
150+
151+ To do a zero-shot prompt optimization, use the ` optimize_prompt `
152+ method.
153+
154+ ``` python
155+ prompt = " Generate system instructions for a question-answering assistant"
156+ response = client.prompt_optimizer.optimize_prompt(prompt = prompt)
157+ print (response.raw_text_response)
158+ if response.parsed_response:
159+ print (response.parsed_response.suggested_prompt)
160+ ```
161+
162+ To call the data-driven prompt optimization, call the ` optimize ` method.
163+ In this case however, we need to provide ` vapo_config ` . This config needs to
164+ have either service account or project ** number** and the config path.
165+ Please refer to this [ tutorial] ( https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/data-driven-optimizer )
166+ for more details on config parameter.
167+
168+ ``` python
169+ import logging
170+
171+ project_number = PROJECT_NUMBER # replace with your project number
172+ service_account = f " { project_number} -compute@developer.gserviceaccount.com "
173+
174+ vapo_config = types.PromptOptimizerVAPOConfig(
175+ config_path = " gs://your-bucket/config.json" ,
176+ service_account_project_number = project_number,
177+ wait_for_completion = False
178+ )
179+
180+ # Set up logging to see the progress of the optimization job
181+ logging.basicConfig(encoding = ' utf-8' , level = logging.INFO , force = True )
182+
183+ result = client.prompt_optimizer.optimize(method = " vapo" , config = vapo_config)
184+ ```
185+
186+ We can also call optimize method async.
187+
188+ ``` python
189+ await client.aio.prompt_optimizer.optimize(method = " vapo" , config = vapo_config)
190+ ```
191+
192+ #### Prompt Management
193+
194+ The Prompt Management module uses some types from the Gen AI SDK. First, import those types:
195+
196+ ``` python
197+ from google.genai import types as genai_types
198+ ```
199+
200+ To create and store a Prompt, first define a types.Prompt object and then run ` create_version ` to save it in Vertex.
201+
202+ ``` python
203+ prompt = types.Prompt(
204+ prompt_data = types.PromptData(
205+ contents = [genai_types.Content(parts = [genai_types.Part(text = " Hello, {name} ! How are you?" )])],
206+ system_instruction = genai_types.Content(parts = [genai_types.Part(text = " Please answer in a short sentence." )]),
207+ generation_config = genai_types.GenerationConfig(temperature = 0.1 ),
208+ safety_settings = [genai_types.SafetySetting(
209+ category = " HARM_CATEGORY_DANGEROUS_CONTENT" ,
210+ threshold = " BLOCK_MEDIUM_AND_ABOVE" ,
211+ method = " SEVERITY" ,
212+ )],
213+ variables = [
214+ {" name" : genai_types.Part(text = " Alice" )},
215+ {" name" : genai_types.Part(text = " Bob" )},
216+ ],
217+ model = " gemini-2.0-flash-001" ,
218+ ),
219+ )
220+
221+ prompt_resource = client.prompt_management.create_version(
222+ prompt = prompt,
223+ )
224+ ```
225+
226+ To retrieve a prompt, provide the ` prompt_id ` :
227+
228+ ``` python
229+ retrieved_prompt = client.prompt_management.get(prompt_id = prompt_resource.prompt_id)
230+ ```
231+
232+ After creating or retrieving a prompt, you can call ` generate_content() ` with that prompt using the Gen AI SDK.
233+
234+ The following uses a utility function available on Prompt objects to transform a Prompt object into a list of Part objects for use with ` generate_content ` . To run this you need to have the Gen AI SDK installed, which you can do via ` pip install google-genai ` .
235+
236+ ``` python
237+ from google import genai
238+ from google.genai import types as genai_types
239+
240+ # Create a Client in the Gen AI SDK
241+ genai_client = genai.Client(vertexai = True , project = " your-project" , location = " your-location" )
242+
243+ # Call generate_content() with the prompt
244+ response = genai_client.models.generate_content(
245+ model = retrieved_prompt.prompt_data.model,
246+ contents = retrieved_prompt.assemble_contents(),
247+ )
248+ ```
249+
250+ ## Warning
251+
252+ The following Generative AI modules in the Vertex AI SDK are deprecated as of
253+ June 24, 2025 and will be removed on June 24, 2026:
254+ ` vertexai.generative_models ` , ` vertexai.language_models ` ,
255+ ` vertexai.vision_models ` , ` vertexai.tuning ` , ` vertexai.caching ` . Please use the
256+ [ Google Gen AI SDK] ( https://pypi.org/project/google-genai/ ) to access these
257+ features. See
258+ [ the migration guide] ( https://cloud.google.com/vertex-ai/generative-ai/docs/deprecations/genai-vertexai-sdk )
259+ for details. You can continue using all other Vertex AI SDK modules, as they are
260+ the recommended way to use the API.
22261
23- For detailed instructions, see [ quickstart] ( http://cloud.google.com/vertex-ai/docs/generative-ai/start/quickstarts/quickstart-multimodal ) and [ Introduction to multimodal classes in the Vertex AI SDK] ( http://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/sdk-for-gemini/gemini-sdk-overview-reference ) .
24262
25263#### Imports:
26264``` python
0 commit comments