Gen AI SDK Introduction
The Google Gen AI SDK allows developers to interact with Gemini models directly from Python applications. Using this SDK, you can send prompts to Gemini models and receive AI-generated responses programmatically.
Install Gen AI SDK
To install the Google Gen AI SDK, run the following command:
Bash
pip install google-genai
Create Environment File
Create a .env file and place your Gemini API key in the format shown below:
.env
GEMINI_API_KEY = "Your_API_Key_here"
Create Python Script
Create a main.py file that contains the main logic for interacting with the Gemini model.
Python
# Import necessary libraries
from google import genai
from dotenv import load_dotenv
import os
# Load environment variables from the .env file
load_dotenv()
# Get the Gemini API key from environment variables
API_Key = os.getenv('GEMINI_API_KEY')
# Initialize the Gemini Developer API client
client = genai.Client(api_key=API_Key)
# Generate content using the Gemini model
response = client.models.generate_content(
model='gemini-3-flash-preview',
contents='Tell me about the capabilities of the Gemini 3 Flash model.'
)
# Print the response generated by the AI model
print("Response: ", response.text)
# Close the client to free up resources
client.close()
Project Folder Structure
The project folder structure should look like the following:
Folder Structure
📁 MyProject ├── 📄 .env └── 📄 main.py
Execute the Code
Run the Python script using the following command:
Bash
python main.py
The above code will send a prompt to the Gemini model and print the response generated by the AI.