Table of Contents
You can try Gemma 4 in Google AI Studio or access the hosted models through the Gemini API. Google currently lists two Gemma 4 models for this API: gemma-4-26b-a4b-it and gemma-4-31b-it.
This hosted route is useful for prototyping because Google runs the model infrastructure. It is different from downloading Gemma's open weights and running them on your own hardware. If local execution is your goal, see TipsMake's guide to running Gemma 4 locally in VS Code.
What you need
- A Google account with access to Google AI Studio in a supported region.
- A Gemini API key for code examples.
- Python 3 and the current
google-genaipackage.
Gemma is an open-weight model family, but its weights are distributed under Google's Gemma Terms rather than the Apache 2.0 license claimed in the original article. The hosted API also has its own terms, quotas, and pricing. Review the current conditions before using it commercially or processing sensitive data.
Try Gemma 4 in Google AI Studio
- Open Google AI Studio and sign in.
- Create a new prompt and open the model selector.
- Select
gemma-4-26b-a4b-itorgemma-4-31b-it. - Enter a small test prompt and review the response.
- Use Get code to export a starter snippet when you are ready to move into an application.
The 26B A4B model uses a mixture-of-experts design, while the 31B model is dense. Do not choose only from parameter count: test response quality, latency, quotas, and cost on your own prompts.
Set up the Python SDK
Install or update Google's SDK in a virtual environment:
python -m pip install -U google-genai
Create an API key in AI Studio, then set it as an environment variable. Never paste a real key into source code or commit it to a repository.
export GEMINI_API_KEY="your-api-key"
On PowerShell, set it for the current session with:
$env:GEMINI_API_KEY = "your-api-key"
Generate a text response
The SDK reads GEMINI_API_KEY automatically when the client is created without explicit credentials.
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents=(
"Compare ramen and udon in three bullet points: "
"broth, noodle texture, and common serving styles."
),
)
print(response.text)
Catch API errors in production, apply a request timeout, and validate model output before another system acts on it.
Turn thinking on or off
Gemma 4 supports a thinking mode. The Gemini API documentation currently exposes high to enable it and minimal to minimize it.
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="Find the smallest positive integer divisible by 12, 15, and 18.",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="high"
)
),
)
print(response.text)
Thinking can help with multi-step work but may increase latency and token use. It does not guarantee a correct answer, so verify calculations and high-impact conclusions.
Add a system instruction
A system instruction sets the assistant's role or response constraints separately from the user prompt.
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-31b-it",
contents="What should I check before deploying this change?",
config=types.GenerateContentConfig(
system_instruction=(
"You are a software release reviewer. "
"Answer with a concise checklist."
)
),
)
print(response.text)
Treat this as behavior guidance, not a security boundary. Untrusted content can still attempt prompt injection, and the application must enforce permissions itself.
Continue a multi-turn conversation
from google import genai
client = genai.Client()
chat = client.chats.create(model="gemma-4-26b-a4b-it")
reply = chat.send_message(
"Name three well-preserved castles in Japan."
)
print(reply.text)
reply = chat.send_message(
"Which one is easiest to reach from Osaka?"
)
print(reply.text)
The SDK tracks the conversation history for the chat object. Long conversations consume more context, so applications should set limits and decide what history genuinely needs to be retained.
Ask about an image
Upload an image through the Files API, then include the returned file object with the prompt:
from google import genai
client = genai.Client()
image_file = client.files.upload(file="path/to/photo.jpg")
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents=[image_file, "Write an accurate one-sentence caption."],
)
print(response.text)
Do not upload confidential images unless your organization's data policy and Google's current service terms permit it. Also verify visual claims; a fluent caption can still misidentify objects or text.
Request a function call
Function calling lets the model propose a structured tool request. Your application—not the model—must validate the arguments, authorize the action, call the real service, and return the result.
from google import genai
from google.genai import types
weather_tool = {
"name": "get_weather",
"description": "Get current weather for a location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country"
}
},
"required": ["location"]
}
}
client = genai.Client()
config = types.GenerateContentConfig(
tools=[types.Tool(function_declarations=[weather_tool])]
)
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="Do I need an umbrella in Kyoto today?",
config=config,
)
for call in response.function_calls or []:
print(call.name, call.args)
Never execute a generated function call automatically without schema checks, permission checks, rate limits, and confirmation for destructive or costly actions.
Ground an answer with Google Search
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="What are the official dates of the next Google I/O?",
config=types.GenerateContentConfig(
tools=[{"google_search": {}}]
),
)
print(response.text)
metadata = response.candidates[0].grounding_metadata
for chunk in metadata.grounding_chunks:
if chunk.web:
print(chunk.web.title, chunk.web.uri)
Search grounding provides sources, but it does not make every statement correct. Display citations to users and check whether each source actually supports the nearby claim.
Important limitations
- Model IDs, features, quotas, and availability can change; check the official Gemma API documentation before deploying.
- Do not describe hosted Gemini API use as local or offline processing.
- Do not rely on leaderboard positions as permanent product facts.
- Protect API keys, log access carefully, and avoid storing sensitive prompts by default.
- Evaluate output quality with a representative test set for your exact task.
For a broader comparison of the two Google product families, see Gemma 4 versus Gemini. Gemma gives developers more deployment flexibility, while the hosted API removes the work of running the model server yourself.
Reader Comments 0
Sign in with email or Google to join the discussion.