---
title: "Gemini Integration with Pixeltable: generate_content as Columns"
description: "Call Gemini generate_content, embed_content, generate_images, and generate_videos from Pixeltable computed columns. AI Studio key or Vertex ADC."
tool: "Gemini"
keywords:
  - Gemini Pixeltable integration
  - generate_content Pixeltable
  - Gemini computed column
  - Gemini embeddings Pixeltable
  - Vertex AI Pixeltable
url: "https://www.pixeltable.com/integrations/gemini"
---

# Gemini Integration with Pixeltable: generate_content as Columns

Call Gemini generate_content, embed_content, generate_images, and generate_videos from Pixeltable computed columns. AI Studio key or Vertex ADC.

## The Problem

Gemini covers text, images, embeddings, Imagen, and Veo. An application that calls the Google GenAI client still has to store the prompt, the response JSON, and a rule for when to call again. Image generation returns inline bytes that you must decode. Vertex versus AI Studio is a second client setup. Pixeltable already has String and Image columns. The integration is pixeltable.functions.gemini: generate_content for text and Nano Banana images, embed_content for vectors, generate_images for Imagen (Vertex), and generate_videos for Veo.

## The Solution

pip install pixeltable google-genai. For AI Studio set GEMINI_API_KEY or GOOGLE_API_KEY. For Vertex set GOOGLE_GENAI_USE_VERTEXAI=true, GOOGLE_CLOUD_PROJECT, optional GOOGLE_CLOUD_LOCATION, and gcloud auth application-default login. Assign generate_content(body, model=gemini-2.5-flash) on the class. The return is the GenerateContent response dict; candidates[0].content.parts[0].text is the text. For gemini-2.5-flash-image, extract inline_data.data.astype(pxt.Image). embed_content.using(model=...) can sit on EmbeddingIndex. Imagen via generate_images needs Vertex. The notebook uses create_table; an app uses TableModel and pxt schema update.

## Implementation

### Authenticate AI Studio or Vertex

One environment for Google AI Studio, another for Vertex ADC. Do not mix them in one process without a restart.

```python
# pip install -U 'pixeltable[serve]' google-genai
# AI Studio:
# export GEMINI_API_KEY=...
# Vertex:
# export GOOGLE_GENAI_USE_VERTEXAI=true
# export GOOGLE_CLOUD_PROJECT=your-project-id
# export GOOGLE_CLOUD_LOCATION=us-central1
# gcloud auth application-default login
# pxt init

import pixeltable as pxt
from pixeltable.functions.gemini import generate_content

TableModel = pxt.model_base()
```

The key can also live in the [gemini] section of $PIXELTABLE_HOME/config.toml as api_key. Vertex uses Application Default Credentials, not that key.


### Generate text with gemini-2.5-flash

Pass the prompt column as contents. Cache the first candidate text.

```python
class Articles(TableModel, name='articles'):
    prompt: pxt.String
    output = generate_content(prompt, model='gemini-2.5-flash')
    response = output['candidates'][0]['content']['parts'][0]['text']

# pxt schema update app.py gemini_text -f
articles = pxt.get_table('gemini_text.articles')
articles.insert([
    {'prompt': 'Write a two-sentence story about a magic backpack.'},
])
articles.select(articles.prompt, articles.response).collect()
```

generate_content accepts a string or a list of images and text as contents. Optional config is a GenerateContentConfigDict (max_output_tokens, temperature, top_p, top_k, tools). Insert runs the model for that row only.


### Generate an image with a Gemini image model

Ask for IMAGE modalities and astype the inline bytes to pxt.Image.

```python
from google.genai.types import GenerateContentConfigDict

class Stills(TableModel, name='stills'):
    prompt: pxt.String
    output = generate_content(
        prompt,
        model='gemini-2.5-flash-image',
        config=GenerateContentConfigDict(
            response_modalities=['IMAGE'],
            image_config={'aspect_ratio': '16:9'},
        ),
    )
    generated_image = (
        output.candidates[0].content.parts[0].inline_data.data.astype(pxt.Image)
    )

# pxt schema update app.py gemini_images -f
```

inline_data.data is decoded into a PIL image. Pass response_modalities=["IMAGE"] so the response is a single image part. Imagen (generate_images, for example imagen-4.0-generate-001) is Vertex only and returns a pxt.Image directly.


### Embed and index with embed_content

Use Gemini embeddings on a String column, then rank with similarity(string=).

```python
from pixeltable.functions.gemini import embed_content

text_embed = embed_content.using(model='gemini-embedding-2')

class Notes(TableModel, name='notes'):
    text: pxt.String
    __indexes__ = [
        pxt.EmbeddingIndex(text, string_embed=text_embed),
    ]

@pxt.query
def search_notes(query: str, limit: int = 5):
    sim = Notes.text.similarity(string=query)  # string= embeds the question
    return Notes.order_by(sim, asc=False).limit(limit).select(Notes.text, sim)

# pxt schema update app.py gemini_embed -f
```

embed_content also accepts Image, Audio, Video, and Document. Use the same model at insert and at query. generate_videos(prompt, model=veo-3.1-fast-generate-preview) is the Veo path; image= passes an existing still to animate.


## google-genai client versus gemini UDFs

| Job | Client in a worker | gemini UDF column |
| --- | --- | --- |
| Text | generate_content then store JSON | generate_content on the class |
| Stills | Decode inline_data yourself | astype(pxt.Image) on that field |
| Vectors | A second embed call and a store | embed_content.using on EmbeddingIndex |
| Veo | Poll a long-running operation | generate_videos as a Video column |

## Benefits

- One module for text, image generation, embeddings, Imagen, and Veo
- AI Studio key or Vertex ADC, not two client stacks in app code
- Per-row cache on generate_content
- inline_data.data.astype(pxt.Image) for Nano Banana stills
- embed_content.using on EmbeddingIndex

## Use Cases

- Summarize stored text with gemini-2.5-flash
- Vision over an Image column in the contents list
- Generate stills from a prompt column
- Semantic search with Gemini embeddings
- Veo clips from a prompt or from a generated still

## Performance


| Metric | Value | Description |

| --- | --- | --- |

| Auth | Key or ADC | AI Studio uses GEMINI_API_KEY; Vertex uses Application Default Credentials |

| Recompute | Changed rows only | Unchanged prompts keep the cached GenerateContent response |

| Image bytes | astype(pxt.Image) | Nano Banana inline_data is decoded; Imagen returns pxt.Image directly |

## Requirements

- Python 3.11+
- pip install -U 'pixeltable[serve]' google-genai
- GEMINI_API_KEY or Vertex ADC with GOOGLE_GENAI_USE_VERTEXAI=true
- Vertex for generate_images (Imagen)

## Resources

- [Working with Gemini](https://docs.pixeltable.com/howto/providers/working-with-gemini) - AI Studio vs Vertex, generate_content, Imagen, Veo
- [Gemini functions SDK](https://docs.pixeltable.com/sdk/latest/gemini) - generate_content, embed_content, generate_images, generate_videos
- [Working with Gemini in Pixeltable](https://www.pixeltable.com/blog/working-with-gemini) - Product write-up of the Gemini path