There are a few ways to call Antigravity from Python, but the google-genai SDK is the current recommended approach. It replaced the older google-generativeai package with better type safety and a cleaner interface.
This guide gets you to a working setup as quickly as possible.
Installation and Setup
pip install google-genaiAuthentication
from google import genai
import os
# Use environment variables (recommended)
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])Get your API key from Google AI Studio.
Basic Text Generation
from google import genai
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
response = client.models.generate_content(
model="gemma-4-antigravity",
contents="Explain Python decorators in one paragraph."
)
print(response.text)With System Instructions
from google.genai import types
response = client.models.generate_content(
model="gemma-4-antigravity",
config=types.GenerateContentConfig(
system_instruction="You are a Python expert. Keep explanations concise and practical.",
temperature=0.7,
max_output_tokens=1024
),
contents="What is asyncio and when should I use it?"
)
print(response.text)Multi-Turn Chat
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
chat = client.chats.create(
model="gemma-4-antigravity",
config=types.GenerateContentConfig(
system_instruction="You are a code review expert.",
temperature=0.3
)
)
messages = [
"Review this function:\ndef calc(x, y):\n return x + y",
"How would you add type hints?",
"Can you write a test for it too?"
]
for msg in messages:
response = chat.send_message(msg)
print(f"User: {msg[:50]}")
print(f"AI: {response.text[:200]}\n")Streaming
Stream responses in real time instead of waiting for the full output.
# Streaming text generation
for chunk in client.models.generate_content_stream(
model="gemma-4-antigravity",
contents="Explain Python dataclasses in detail"
):
print(chunk.text, end="", flush=True)
print()
# Streaming chat
chat = client.chats.create(model="gemma-4-antigravity")
for chunk in chat.send_message_stream("Describe the Factory design pattern"):
print(chunk.text, end="", flush=True)
print()Multimodal (Image + Text)
import PIL.Image
from google import genai
client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
# Single image
image = PIL.Image.open("screenshot.png")
response = client.models.generate_content(
model="gemma-4-antigravity",
contents=[image, "What UI issues do you see in this screenshot?"]
)
print(response.text)
# Compare two images
before = PIL.Image.open("before.png")
after = PIL.Image.open("after.png")
response = client.models.generate_content(
model="gemma-4-antigravity",
contents=["Compare these two designs:", "Before:", before, "After:", after]
)
print(response.text)Structured Output (JSON)
import json
from google.genai import types
response = client.models.generate_content(
model="gemma-4-antigravity",
config=types.GenerateContentConfig(
response_mime_type="application/json",
response_schema={
"type": "object",
"properties": {
"title": {"type": "string"},
"summary": {"type": "string"},
"tags": {"type": "array", "items": {"type": "string"}},
"difficulty": {
"type": "string",
"enum": ["beginner", "intermediate", "advanced"]
}
},
"required": ["title", "summary", "tags", "difficulty"]
}
),
contents="Generate article metadata for a post about Python decorators"
)
metadata = json.loads(response.text)
print(f"Title: {metadata['title']}")
print(f"Tags: {', '.join(metadata['tags'])}")
print(f"Difficulty: {metadata['difficulty']}")Async Parallel Requests
import asyncio
from google import genai
async def parallel_requests(prompts: list[str]) -> list[str]:
client = genai.AsyncClient(api_key=os.environ["GOOGLE_API_KEY"])
async def _request(prompt: str) -> str:
response = await client.aio.models.generate_content(
model="gemma-4-antigravity",
contents=prompt
)
return response.text
return await asyncio.gather(*[_request(p) for p in prompts])
async def main():
prompts = [
"What are list comprehensions?",
"What is a generator?",
"How do decorators work?"
]
results = await parallel_requests(prompts)
for p, r in zip(prompts, results):
print(f"Q: {p}\nA: {r[:100]}...\n")
asyncio.run(main())Error Handling
from google import genai
from google.api_core import exceptions as google_exceptions
import time
def safe_generate(client, model: str, contents, max_retries: int = 3) -> str:
for attempt in range(max_retries):
try:
response = client.models.generate_content(model=model, contents=contents)
return response.text
except google_exceptions.ResourceExhausted:
wait = 2 ** attempt
print(f"Rate limited. Waiting {wait}s...")
time.sleep(wait)
except google_exceptions.InvalidArgument as e:
print(f"Invalid input: {e}")
raise # Don't retry invalid input
except google_exceptions.ServiceUnavailable:
if attempt < max_retries - 1:
time.sleep(5)
else:
raise
raise RuntimeError(f"Failed after {max_retries} attempts")Token Counting
# Check token count before sending
token_count = client.models.count_tokens(
model="gemma-4-antigravity",
contents="Your long prompt text here..."
)
print(f"Input tokens: {token_count.total_tokens}")
# Check usage after a request
response = client.models.generate_content(
model="gemma-4-antigravity",
contents="Hello"
)
if response.usage_metadata:
print(f"Input: {response.usage_metadata.prompt_token_count} tokens")
print(f"Output: {response.usage_metadata.candidates_token_count} tokens")Three Steps to Get Started
pip install google-genai- Get an API key from Google AI Studio, set it as
GOOGLE_API_KEYenvironment variable - Call
client.models.generate_content(model="gemma-4-antigravity", contents="...")
The google-genai SDK has better type support and a cleaner interface than the previous google-generativeai package. For any new project, this is the SDK to use.
Full API reference is available in the Google AI documentation.