Skip to main content
Pixeltable’s Gemini integration enables you to access the Gemini LLM via the Google Gemini API.

Prerequisites

Important Notes

  • Google AI Studio usage may incur costs based on your plan.
  • Be mindful of sensitive data and consider security measures when integrating with external services.
First you’ll need to install required libraries and enter a Gemini API key obtained via Google AI Studio.
%pip install -qU pixeltable google-genai
import os
import getpass

if 'GEMINI_API_KEY' not in os.environ:
    os.environ['GEMINI_API_KEY'] = getpass.getpass('Google AI Studio API Key:')
Now let’s create a Pixeltable directory to hold the tables for our demo.
import pixeltable as pxt

# Remove the 'gemini_demo' directory and its contents, if it exists
pxt.drop_dir('gemini_demo', force=True)
pxt.create_dir('gemini_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory 'gemini_demo'.
<pixeltable.catalog.dir.Dir at 0x304985c60>

Generate Content

Create a Table: In Pixeltable, create a table with columns to represent your input data and the columns where you want to store the results from Gemini.
from google.genai.types import GenerateContentConfigDict
from pixeltable.functions import gemini

# Create a table in Pixeltable and pick a model hosted on Google AI Studio with some parameters

t = pxt.create_table('gemini_demo.text', {'input': pxt.String})

config = GenerateContentConfigDict(
    stop_sequences=['\n'],
    max_output_tokens=300,
    temperature=1.0,
    top_p=0.95,
    top_k=40,
)
t.add_computed_column(output=gemini.generate_content(
    t.input,
    model='gemini-2.0-flash',
    config=config
))
Created table `text`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
# Ask Gemini to generate some content based on the input
t.insert([
    {'input': 'Write a story about a magic backpack.'}, 
    {'input': 'Tell me a science joke.'}
])
Inserting rows into `text`: 2 rows [00:00, 176.84 rows/s]
Inserted 2 rows with 0 errors.
UpdateStatus(num_rows=2, num_computed_values=4, num_excs=0, updated_cols=[], cols_with_excs=[])
# Parse the response into a new column
t.add_computed_column(response=t.output['candidates'][0]['content']['parts'][0]['text'])
t.select(t.input, t.response).head()
Added 2 column values with 0 errors.
input response
Tell me a science joke. Why did the chemist make a periodic table t-shirt?
Write a story about a magic backpack. Flora hated Mondays. This Monday was especially egregious. Her shoelace broke, she spilled yogurt on her favorite shirt, and now, lugging her overflowing backpack to school, she felt like a pack mule. As she slumped onto a park bench, defeated, her gaze fell upon a battered leather backpack nestled under a bush. It looked ancient, the leather cracked and faded, but strangely compelling.

Generate Images with Imagen

from google.genai.types import GenerateImagesConfigDict

images_t = pxt.create_table('gemini_demo.images', {'prompt': pxt.String})

config = GenerateImagesConfigDict(aspect_ratio='16:9')
images_t.add_computed_column(generated_image=gemini.generate_images(
    images_t.prompt,
    model='imagen-3.0-generate-002',
    config=config
))
Created table `images`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
images_t.insert([{'prompt': 'A friendly dinosaur playing tennis in a cornfield'}])
Inserting rows into `images`: 1 rows [00:00, 382.10 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
images_t.head()
prompt generated_image
A friendly dinosaur playing tennis in a cornfield

Generate Video with Veo

videos_t = pxt.create_table('gemini_demo.videos', {'prompt': pxt.String})

videos_t.add_computed_column(generated_video=gemini.generate_videos(
    videos_t.prompt,
    model='veo-2.0-generate-001',
))  
Created table `videos`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
videos_t.insert([{'prompt': 'A giant pixel floating over the open ocean in a sea of data'}])
Inserting rows into `videos`: 1 rows [00:00, 65.14 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
videos_t.head()
prompt generated_video
A giant pixel floating over the open ocean in a sea of data

Generate Video from an existing Image

We’ll add an additional computed column to our existing images_t to animate the generated images.
images_t.add_computed_column(generated_video=gemini.generate_videos(
    image=images_t.generated_image,
    model='veo-2.0-generate-001',
))
Added 1 column value with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=1, num_excs=0, updated_cols=[], cols_with_excs=[])
images_t.head()
prompt generated_image generated_video
A friendly dinosaur playing tennis in a cornfield

Learn More

To learn more about advanced techniques like RAG operations in Pixeltable, check out the RAG Operations in Pixeltable tutorial. If you have any questions, don’t hesitate to reach out.