Google AI Studio Tutorial: Prototype Prompts in Python
Many developers prototype prompts in a browser but struggle to transition those designs into local applications without using outdated libraries. This Google AI Studio tutorial guides developers through crafting prompts in the web interface and executing them with the official modern SDK. By the end of this guide, you will have a working script that communicates directly with Gemini models.
Before you start
- A Google account to access Google AI Studio
- Python 3.10 or higher installed on your computer
Understanding AI Studio and the UI
Google AI Studio acts as an interactive prototyping environment for Gemini models. You can work with chat prompts for multi-turn conversations, freeform prompts for arbitrary text and media combinations, or structured prompts for tabular few-shot examples.
The right panel lets you configure runtime settings including temperature, model selection, safety filters, and system instructions. You can also enable built-in tools like Google Search Grounding for live web results and Code Execution for sandboxed Python calculations.
Data Privacy and API Keys
Understanding data handling and key security is essential before running requests. Prompts and outputs on the free tier may be logged and reviewed by Google to improve models. If you need confidential handling, link a billing account to your project or use Google Cloud Vertex AI.
Never store API credentials inside source code or commit them to version control. Generate a key from the interface and assign it to the GEMINI_API_KEY environment variable so your local scripts can authenticate securely.
Setting Up Your Python Environment
Create an isolated Python virtual environment to prevent package conflicts on your system. Run the setup command shown below to create and activate the gemini-env directory.
Avoid the older google-generativeai package because it has been deprecated. Run the upgrade and package install command below to install google-genai.
# Create and activate a clean virtual environment
python3 -m venv gemini-env && source gemini-env/bin/activate
# Install the modern official Google GenAI SDK
pip install --upgrade pip && pip install google-genai
Output from our test run
$ python3 -m venv gemini-env && source gemini-env/bin/activate
(gemini-env) $ pip install --upgrade pip && pip install google-genai
Requirement already satisfied: pip in ./gemini-env/lib/python3.13/site-packages (25.1.1)
Collecting pip
Downloading pip-26.2.2-py3-none-any.whl.metadata (4.6 kB)
Downloading pip-26.2.2-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 4.5 MB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 25.1.1
Uninstalling pip-25.1.1:
Successfully uninstalled pip-25.1.1
Successfully installed pip-26.2.2
Collecting google-genai
Downloading google_genai-2.33.0-py3-none-any.whl.metadata (59 kB)
Collecting anyio<5.0.0,>=4.8.0 (from google-genai)
Downloading anyio-4.15.1-py3-none-any.whl.metadata (4.7 kB)
Collecting google-auth<3.0.0,>=2.56.0 (from google-auth[requests]<3.0.0,>=2.56.0->google-genai)
Downloading google_auth-2.62.0-py3-none-any.whl.metadata (6.0 kB)
Collecting httpx<1.0.0,>=0.28.1 (from google-genai)
Downloading httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)
Collecting pydantic<3.0.0,>=2.12.5 (from google-genai)
Downloading pydantic-2.14.1-py3-none-any.whl.metadata (133 kB)
Collecting requests<3.0.0,>=2.28.1 (from google-genai)
Downloading requests-2.34.2-py3-none-any.whl.metadata (4.8 kB)
Collecting tenacity<9.2.0,>=8.2.3 (from google-genai)
Downloading tenacity-9.1.4-py3-none-any.whl.metadata (1.2 kB)Writing the Execution Script
The modern SDK uses a client-first design initialized through genai.Client. When called without explicit parameters, the client automatically reads the GEMINI_API_KEY environment variable.
The client.models.generate_content method sends the prompt along with the requested model name. Use the script shown below to send a single query to gemini-2.5-flash and print the resulting text response.
import os
from google import genai
# Client automatically loads GEMINI_API_KEY from environment
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain quantum computing in one short sentence."
)
print(response.text)
Exporting Your Key and Running the Script
Write the complete script into main.py using the heredoc command shown below. This script imports the library, creates the client instance, and executes the prompt call.
After creating the file, run the verification command below to confirm that Python imports google.genai cleanly. This verification step was tested and confirmed that the library loads properly in the local environment.
# Write the Gemini API sample script to main.py
cat << 'EOF' > main.py
import os
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain quantum computing in one short sentence."
)
print(response.text)
EOF
# Verify that the google-genai SDK is available in the Python environment
python3 -c "import google.genai; print('SDK loaded successfully')"
Output from our test run
$ cat << 'EOF' > main.py
> import os
> from google import genai
>
> client = genai.Client()
>
> response = client.models.generate_content(
> model="gemini-2.5-flash",
> contents="Explain quantum computing in one short sentence."
> )
>
> print(response.text)
> EOF
$ python3 -c "import google.genai; print('SDK loaded successfully')"
SDK loaded successfully
$Exporting Prompts with Get Code
You do not need to manually rewrite prompt settings into Python code. The web interface includes a dedicated button at the top of the workspace that converts any prompt setup into production-ready code.
Clicking this option generates matching snippets for Python, JavaScript, and cURL commands. The generated snippets automatically include your system instructions, temperature settings, and attached multimodal assets.
Wrap-up
You now have a clean workflow for experimenting inside Google AI Studio and executing your prompts through local Python scripts. Moving from the web console to the official google-genai library keeps your prototypes organized and ready for production deployment.
FAQ
Why should I install google-genai instead of google-generativeai?
Google deprecated the older google-generativeai package in late 2025. The new google-genai package is the official SDK and provides a unified interface across both AI Studio and Google Cloud Vertex AI.
What environment variable does the new SDK look for?
The modern SDK standardizes on GEMINI_API_KEY for authentication, which genai.Client detects automatically from your environment.
Why am I receiving an HTTP 429 error on the free tier?
Free-tier accounts have strict caps on requests per minute and daily request limits rather than token size limits. Exceeding those thresholds triggers a resource exhaustion error.