Generative AI fashions are altering how we create content material, whether or not it’s textual content, photos, video, or code. With Google’s Gen AI Python SDK, now you can entry and work together with Google’s generative AI fashions in your Python purposes extra simply, along with utilizing the Gemini Developer API and Vertex AI APIs. Meaning builders can extra readily create purposes, together with chatbots, content material mills, or artistic instruments. On this article, we’ll cowl every thing you’ll want to know to get began utilizing the Google Gen AI Python SDK.
Additionally learn: Construct an LLM Mannequin utilizing Google Gemini API
What’s the Google Gen AI Python SDK?
The Google Gen AI Python SDK is a shopper library for builders to make use of Google’s generative AI talents simply utilizing Python. It gives:
- Help for Gemini Developer API (Google’s superior textual content and multimodal generative fashions)
- Integration with Vertex AI APIs for enterprise-scale AI workloads
- Help for producing textual content, photos, movies, embeddings, chat conversations, and extra
- Instruments for file administration, caching, and async assist
- Superior operate calling and schema enforcement options
This SDK additionally abstracts a lot of the complexity round API calls and lets you deal with constructing AI-powered purposes.
Set up
Putting in the SDK is easy. Run:
pip set up google-genai
The above command will set up the Google Gen AI Python SDK bundle utilizing pip. This command downloads every thing you want for the Python atmosphere to start out up the Google generative AI providers, together with the sources and all dependencies.
Imports and Consumer Setup
Upon getting put in the SDK, create a Python file and import the SDK:
from google import genai
from google.genai import sorts
The SDK has two modules – genai and kinds. The genai module creates a shopper used for API interplay, whereas the categories module has information buildings and lessons that function helpers used to construct requests and configure request parameters.
You’ll create an occasion of the shopper for every interplay with the Google generative AI fashions. You’ll instantiate the shopper with totally different strategies relying on the API you’re utilizing.
For the Gemini Developer API, you’ll be able to instantiate the shopper by passing alongside your API key:
shopper = genai.Consumer(api_key='YOUR_GEMINI_API_KEY')
You instantiate the shopper you’ll be able to work together with the Gemini Developer API by passing in your API key. This shopper will maintain the entry token and request administration.
Non-compulsory: Utilizing Google Cloud Vertex AI
shopper = genai.Consumer(
vertexai=True,
challenge="your-project-id",
location='us-central1'
)
If you’ll use Google Cloud Vertex AI, you’ll initialise the shopper in another way by specifying the challenge ID and the placement.
Notice: Utilizing Vertex AI is optionally available. You’ll be able to create your challenge ID right here.
If you don’t use Vertex AI, you’ll be able to merely use the API key methodology above.
API Model and Configuration
By default, the SDK makes use of beta endpoints to entry beta options. Nevertheless, if you wish to use steady APIs, you’ll be able to specify the API model utilizing the http_options argument:
from google.genai import sorts
shopper = genai.Consumer(
vertexai=True,
challenge="your-project-id",
location='us-central1',
http_options=sorts.HttpOptions(api_version='v1')
)
It’s as much as you ways you wish to proceed to stability stability with cutting-edge options.
Utilizing Surroundings Variables (Non-compulsory)
As a substitute of instantly passing keys, we must always first set atmosphere variables:
Gemini Developer API:
export GEMINI_API_KEY='your-api-key'
Vertex AI:
export GOOGLE_GENAI_USE_VERTEXAI=true
export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='us-central1'
Then, initialize the shopper merely with:
shopper = genai.Consumer()
Google Gen AI Python SDK Use Circumstances
Listed below are the assorted methods you’ll be able to put Google Gen AI Python SDK’s capabilities to make use of as soon as arrange.
Content material Technology
The first operate of the SDK is to generate AI content material. You present prompts in numerous varieties, similar to easy strings, structured content material, or complicated multimodal inputs.
Primary Textual content Technology
response = shopper.fashions.generate_content(
mannequin="gemini-2.0-flash-001",
contents="Why Does the solar rises from east"
)
print(response.textual content)
Output

This sends a immediate to the mannequin and returns the generated reply.
Structured Content material Inputs
You’ll be able to insert structured content material throughout numerous roles, like consumer or mannequin for chatbot, conversational, or multi-turn contexts.
from google.genai import sorts
content material = sorts.Content material(
function="consumer",
elements=[types.Part.from_text(text="Tell me a fun fact about work.")]
)
response = shopper.fashions.generate_content(mannequin="gemini-2.0-flash-001", contents=content material)
print(response.textual content)
Output

The SDK internally interprets many various enter sorts to a structured information format for the mannequin.
File Add and Utilization
The Gemini Builders API lets you add information for the mannequin to course of. That is nice for summarization, or content material extraction:
file = shopper.information.add(file="/content material/sample_file.txt")
response = shopper.fashions.generate_content(
mannequin="gemini-2.0-flash-001",
contents=[file, 'Please summarize this file.']
)
print(response.textual content)
Output

This is a perfect method for including AI performance to document-based duties.
Perform Calling
A singular functionality is the power to cross Python features as “instruments” for the mannequin to invoke robotically whereas producing the completion.
def get_current_weather(location: str) -> str:
return 'sunny'
response = shopper.fashions.generate_content(
mannequin="gemini-2.0-flash-001",
contents="What's the climate like in Ranchi?",
config=sorts.GenerateContentConfig(instruments=[get_current_weather])
)
print(response.textual content)
Output

This allows dynamic, real-time information integration inside AI responses.
Superior Configuration
You have got the power to customise technology with parameters similar to temperature, max_output_tokens, and security settings to handle randomness, size, and filter dangerous content material.
config = sorts.GenerateContentConfig(
temperature=0.3,
max_output_tokens=100,
safety_settings=[types.SafetySetting(category='HARM_CATEGORY_HATE_SPEECH', threshold='BLOCK_ONLY_HIGH')]
)
response = shopper.fashions.generate_content(
mannequin="gemini-2.0-flash-001",
contents=""'Provide some encouraging phrases for somebody beginning a brand new journey.''',
config=config
)
print(response.textual content)
Output

This will present granularity over content material high quality and security.
Multimedia Help: Photographs and Movies
The SDK lets you generate and edit photos and generate movies (in preview).
- Generate photos utilizing textual content prompts.
- Upscale or alter photos generated.
- Generate movies from textual content or photos.
Instance of Picture Technology:
response = shopper.fashions.generate_images(
mannequin="imagen-3.0-generate-002",
immediate="A tranquil seashore with crystal-clear water and colourful seashells on the shore.",
config=sorts.GenerateImagesConfig(number_of_images=1)
)
response.generated_images[0].picture.present()
Output

Instance of Video Technology:
import time
operation = shopper.fashions.generate_videos(
mannequin="veo-2.0-generate-001",
immediate="A cat DJ spinning vinyl information at a futuristic nightclub with holographic beats.",
config=sorts.GenerateVideosConfig(number_of_videos=1, duration_seconds=5)
)
whereas not operation.finished:
time.sleep(20)
operation = shopper.operations.get(operation)
video = operation.response.generated_videos[0].video
video.present()
Output:
This enables for artistic, multimodal AI apps.
Chat and Conversations
You can begin chat classes that protect context all through your messages:
chat = shopper.chats.create(mannequin="gemini-2.0-flash-001")
response = chat.send_message('Inform me a narrative')
print(response.textual content)

response = chat.send_message('Summarize that story in a single sentence')
print(response.textual content)

That is helpful for creating conversational AI that remembers earlier dialogue.
Asynchronous Help
All predominant API strategies have async features for higher integration into async Python apps:
response = await shopper.aio.fashions.generate_content(
mannequin="gemini-2.0-flash-001",
contents="Inform a Horror story in 200 phrases."
)
print(response.textual content)

Token Counting
Token counting tracks what number of tokens (items of textual content) are in your enter. This helps you keep inside mannequin limits and make cost-effective selections.
token_count = shopper.fashions.count_tokens(
mannequin="gemini-2.0-flash-001",
contents="Why does the sky have a blue hue as a substitute of different colours?"
)
print(token_count)

Embeddings
Embeddings flip your textual content into numeric vectors that signify its that means, which can be utilized for search, clustering, and AI analysis.
embedding = shopper.fashions.embed_content(
mannequin="text-embedding-004",
contents="Why does the sky have a blue hue as a substitute of different colours?"
)
print(embedding)

Utilizing the SDK, you’ll be able to simply depend tokens and make embeddings to enhance and improve your AI purposes.
Conclusion
The Google Gen AI Python SDK is a strong, versatile instrument that enables builders to entry Google’s prime generative AI fashions of their Python tasks. From textual content technology, chat, and chatbot, to picture/video technology, operate calling, and rather more it gives a strong characteristic set with easy interfaces. With a simple bundle set up, easy shopper configuration course of, and assist for async programming and multimedia, the SDK makes constructing purposes that leverage AI considerably simpler. Whether or not you’re a newbie or seasoned developer, utilizing the SDK is comparatively painless however highly effective in the case of incorporating generative AI into your workflows.
Continuously Requested Questions
It’s a Python library for utilizing Google’s Gen AI providers and fashions in Python code
You run pip set up google-genai. If you wish to use the SDK asynchronously, run pip set up google-genai[aiohttp].
On shopper creation, you’ll be able to cross in an API Key or set the atmosphere variables GEMINI_API_KEY or set Google Cloud credentials for Vertex AI.
Sure, the SDK can carry out operations the place photos and information are involved, add and edit them, and use them in structured content material.
generate_content takes plain strings, lists of messages, structured prompts the place you assign roles, and multipart content material (textual content together with photos or information).
The operate calling characteristic permits the mannequin to name Python features dynamically through the session. Due to this fact, permitting you to have a workflow that calls exterior logic or computing.
Sure, on generate_content, you need to use the generation_config parameter with arguments similar to temperature (to manage randomness), and max_output_tokens (to restrict the mannequin response).
Login to proceed studying and revel in expert-curated content material.