This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Pixeltable’s Gemini integration supports two authentication methods:
A Google Cloud project with the Vertex AI API enabled
The Google Cloud SDK installed and configured
(gcloud auth application-default login)
%pip install -qU pixeltable google-genai
import osvertex_enabled = ( os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '').lower() == 'true')# Option 1: Google AI Studio (API key)# Set GEMINI_API_KEY or GOOGLE_API_KEY in your environment,# or add api_key to the [gemini] section of $PIXELTABLE_HOME/config.tomlif ( not vertex_enabled and 'GEMINI_API_KEY' not in os.environ and 'GOOGLE_API_KEY' not in os.environ): import getpass os.environ['GEMINI_API_KEY'] = getpass.getpass( 'Google AI Studio API Key:' )# Option 2: Vertex AI (Application Default Credentials)# Uncomment and set the following environment variables to use Vertex AI instead:# os.environ['GOOGLE_GENAI_USE_VERTEXAI'] = 'true'# os.environ['GOOGLE_CLOUD_PROJECT'] = 'your-project-id'# os.environ['GOOGLE_CLOUD_LOCATION'] = 'us-central1' # optional, defaults to us-central1# Then authenticate via: gcloud auth application-default login
Create a Pixeltable directory for this notebook’s demo tables.
import pixeltable as pxtimport pixeltable.functions as pxtfpxt.create_dir('gemini_demo', if_exists='replace_force')
Created directory ‘gemini_demo’.
<pixeltable.catalog.dir.Dir at 0x3663d9410>
Generate content
Create a table with an input column, then add a computed column that
calls Gemini on each row.
from google.genai.types import GenerateContentConfigDict# Create a table in Pixeltable and pick a model hosted on Google AI Studio with some parameterst = pxt.create_table('gemini_demo/text', {'input': pxt.String})config = GenerateContentConfigDict( max_output_tokens=500, temperature=1.0, top_p=0.95, top_k=40)t.add_computed_column( output=pxtf.gemini.generate_content( t.input, model='gemini-2.5-flash', config=config ))
Created table ‘text’.
Added 0 column values with 0 errors in 0.01 s
No rows affected.
# Ask Gemini for short responses so results are easy to inspectt.insert( [ {'input': 'Write a two-sentence story about a magic backpack.'}, {'input': 'Tell a one-line science joke.'}, ])
Inserted 2 rows with 0 errors in 4.70 s (0.43 rows/s)
2 rows inserted.
# Parse the response text into its own columnt.add_computed_column( response=t.output['candidates'][0]['content']['parts'][0]['text'], if_exists='replace',)
Added 2 column values with 0 errors in 0.01 s (184.71 rows/s)
2 rows updated.
t.select(t.input, t.response).collect()
Generate images with Nano Banana
Use pxtf.gemini.generate_content with model gemini-2.5-flash-image.This returns a structured response, so extract the image from
response['candidates'][0]['content']['parts'][0]['inline_data']['data'],
as shown below.
Use pxtf.gemini.generate_images with an Imagen model name such as
imagen-4.0-generate-001. Imagen is available only through Vertex AI,
so these cells require GOOGLE_GENAI_USE_VERTEXAI=true (see the setup
cell above).This returns a pxt.Image directly, so you can use it in computed
columns without JSON subscripting.