Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/genkit-google-genai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ print(await greet('Grace'))

### Dynamic Models

The plugin automatically discovers available models from the API upon initialization. You can use any model name supported by the API via model reference constructors (e.g., `GoogleAI.gemini('gemini-flash-latest')`, `VertexAI.gemini('gemini-3.1-pro-preview')`).
The plugin registers a static catalog of known models at startup, with no network call, so a rate-limited or offline project still starts. Any other model id resolves by name on first use. The live model list is fetched only when the Dev UI lists actions, then cached for the process. You can use any model name supported by the API via model reference constructors (e.g., `GoogleAI.gemini('gemini-flash-latest')`, `VertexAI.gemini('gemini-3.1-pro-preview')`).

### Typed config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"""Google GenAI plugin for Genkit.

This plugin provides integration with Google's generative AI models through
either Google AI (Gemini API) or Google Cloud Vertex AI. It dynamically discovers
and registers available models and embedders at runtime.
either Google AI (Gemini API) or Google Cloud Vertex AI. It registers known
models and embedders at init with no network call and resolves any other model
id by name on first use.

Example:
Using GoogleAI (Gemini API) with typed flat config:
Expand Down
175 changes: 108 additions & 67 deletions packages/genkit-google-genai/src/genkit_google_genai/_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"""Google AI and Vertex AI plugin implementations for Genkit.

This module provides the GoogleAI and VertexAI plugins that enable Genkit to use
Google's generative AI models. Both plugins use dynamic model discovery via the
Google GenAI SDK to detect and register available models at runtime.
Google's generative AI models. Both plugins register a static catalog of known
models at init with no network call, resolve any other model id by name on first
use, and list the live catalog only when the Dev UI asks for it.

Supported capabilities include text generation (Gemini/Gemma), text embeddings,
image generation (Gemini native image), and video generation (Veo).
Expand All @@ -29,10 +30,10 @@
from genkit import Genkit
from genkit_google_genai import GoogleAI

# 1. Initialize Genkit with dynamic model discovery
# 1. Initialize Genkit; no model listing happens at startup
ai = Genkit(plugins=[GoogleAI()])

# 2. Generate content using any discovered Gemini model
# 2. Generate with any Gemini model id the API serves
response = await ai.generate(
model=GoogleAI.gemini('gemini-flash-latest'),
prompt='Suggest 3 names for a space-themed coffee shop.',
Expand All @@ -48,6 +49,7 @@

from __future__ import annotations

import asyncio
import os
from collections.abc import Callable, Mapping
from typing import Any, NoReturn
Expand Down Expand Up @@ -89,13 +91,16 @@
deep_research_model,
)
from genkit_google_genai._models._embedder import (
GOOGLEAI_KNOWN_EMBEDDERS,
VERTEX_KNOWN_EMBEDDERS,
Embedder as GoogleEmbedder,
EmbeddingConfig,
get_embedder_info,
)
from genkit_google_genai._models._gemini import (
GOOGLEAI_KNOWN_MODELS,
SUPPORTED_MODELS,
VERTEX_KNOWN_MODELS,
GeminiConfig,
GeminiImageConfig,
GeminiModel,
Expand Down Expand Up @@ -137,6 +142,8 @@
)
from genkit_google_genai._models._sdk_config import check_timeout_seconds, timeout_seconds_to_ms
from genkit_google_genai._models._veo import (
GOOGLEAI_KNOWN_VEO,
VERTEX_KNOWN_VEO,
KnownGoogleAIVeo,
KnownVertexAIVeo,
VeoConfig,
Expand Down Expand Up @@ -447,11 +454,13 @@ def embedder(cls, name: str, *, config: EmbeddingConfig | None = None) -> Embedd


class GoogleAI(GoogleFamilyRefs, Plugin):
"""GoogleAI plugin for Genkit with dynamic model discovery.
"""GoogleAI plugin for Genkit.

This plugin provides access to Google AI models (Gemini, embedders, Veo)
through the Google AI Studio API. Models are discovered dynamically at
initialization time, ensuring new models are available without SDK updates.
through the Google AI Studio API. Known models register at init with no
network call. Any other model id resolves by name on first use, so new
models work without an SDK update. The live catalog is listed only when
the Dev UI asks for it, then cached.

Model Types:
| Type | Action Kind | Example |
Expand All @@ -465,7 +474,7 @@ class GoogleAI(GoogleFamilyRefs, Plugin):
from genkit import Genkit
from genkit_google_genai import GoogleAI

# 1. Initialize Genkit with dynamic model discovery
# 1. Initialize Genkit; no model listing happens at startup
ai = Genkit(plugins=[GoogleAI()])

# 2. Generate text using Gemini Flash
Expand Down Expand Up @@ -696,6 +705,10 @@ def _runtime_client(self) -> genai.Client:
# One client per event loop, used everywhere in plugin runtime paths.
return _plugin_client(self._loop_client_kwargs())

@loop_local_client
def _list_actions_lock(self) -> asyncio.Lock:
return asyncio.Lock()

def _loop_client_kwargs(self) -> dict[str, Any]:
"""Client kwargs carrying this loop's client from ``http_client_factory``, if one was given."""
return _with_loop_http_client(self._client_kwargs, self._http_client_factory)
Expand All @@ -722,21 +735,29 @@ def _plugin_api_key(self) -> str | None:
return raw if isinstance(raw, str) else None

async def init(self) -> list[Action | BackgroundAction[Any, Any]]:
"""Initialize the plugin.
"""Initialize the plugin with static known models and embedders.

Registration is static-first and requires no network requests,
preventing startup rate limits (429) on unbilled/free-tier projects.
Unknown or dynamic models resolve lazily on-demand.

Returns:
Actions and background models for known/supported models.
"""
genai_models = await _list_genai_models(self._runtime_client(), is_vertex=False)
# Build this loop's http client now so a factory that returns the wrong
# type raises TypeError here, at setup, not inside the first model run.
if self._http_client_factory is not None:
self._http_client_factory()

actions: list[Action | BackgroundAction[Any, Any]] = []
# Gemini Models
for name in genai_models.gemini:

# Gemini / Gemma Models
for name in GOOGLEAI_KNOWN_MODELS:
if action := self._resolve_model(name):
actions.append(action)

# Veo Models (background models)
for name in genai_models.veo:
for name in GOOGLEAI_KNOWN_VEO:
actions.append(self._resolve_veo_model(name))

client_options = self._interactions_client_options()
Expand Down Expand Up @@ -767,7 +788,7 @@ async def init(self) -> list[Action | BackgroundAction[Any, Any]]:
)

# Embedders
for name in genai_models.embedders:
for name in GOOGLEAI_KNOWN_EMBEDDERS:
actions.append(self._resolve_embedder(name))

return actions
Expand Down Expand Up @@ -891,56 +912,58 @@ def _resolve_embedder(self, name: str) -> Action:
async def list_actions(self) -> list[Action | BackgroundAction[Any, Any]]:
"""Generate a list of available actions or models.

Dynamic discovery is performed lazily and cached across calls.

Returns:
list[Action | BackgroundAction[Any, Any]]: Actions and background models.
"""
if self._list_actions_cache is not None:
return self._list_actions_cache
genai_models = await _list_genai_models(self._runtime_client(), is_vertex=False)
actions_list: list[Action | BackgroundAction[Any, Any]] = []

for name in genai_models.gemini:
if (model_action := self._resolve_model(name)) is not None:
actions_list.append(model_action)
async with self._list_actions_lock():
if self._list_actions_cache is not None:
return self._list_actions_cache

actions_list = list(await self.init())
seen_names = {a.name for a in actions_list}

genai_models = await _list_genai_models(self._runtime_client(), is_vertex=False)

for name in genai_models.gemini:
if (model_action := self._resolve_model(name)) is not None:
if model_action.name not in seen_names:
actions_list.append(model_action)
seen_names.add(model_action.name)

for name in genai_models.veo:
actions_list.append(
_create_veo_background_model(
for name in genai_models.veo:
veo_action = _create_veo_background_model(
name,
self._runtime_client,
GOOGLEAI_PLUGIN_NAME,
client_kwargs=self._loop_client_kwargs,
)
)

for name in list_known_deep_research_models():
actions_list.append(
create_deep_research_background_model(
googleai_name(name),
plugin_api_key=self._plugin_api_key(),
client_options=self._interactions_client_options(),
)
)
for name in list_known_antigravity_models():
if (model_action := self._resolve_model(name)) is not None:
actions_list.append(model_action)
for name in list_known_lyria_models():
if (model_action := self._resolve_model(name)) is not None:
actions_list.append(model_action)
if veo_action.name not in seen_names:
actions_list.append(veo_action)
seen_names.add(veo_action.name)

for name in genai_models.embedders:
actions_list.append(_create_embedder_action(name, self._runtime_client, GOOGLEAI_PLUGIN_NAME))
for name in genai_models.embedders:
embedder_action = _create_embedder_action(name, self._runtime_client, GOOGLEAI_PLUGIN_NAME)
if embedder_action.name not in seen_names:
actions_list.append(embedder_action)
seen_names.add(embedder_action.name)

self._list_actions_cache = actions_list
return actions_list
self._list_actions_cache = actions_list
return actions_list


class VertexAI(GoogleFamilyRefs, Plugin):
"""Vertex AI plugin for Genkit with dynamic model discovery.
"""Vertex AI plugin for Genkit.

This plugin provides access to Google Cloud Vertex AI models including
Gemini, Veo, and embedders. Models are discovered dynamically,
ensuring new models are available without SDK updates.
Gemini, Veo, and embedders. Known models register at init with no network
call. Any other model id resolves by name on first use, so new models work
without an SDK update. The live catalog is listed only when the Dev UI asks
for it, then cached.

Vertex AI vs Google AI:
Vertex AI provides enterprise features including:
Expand Down Expand Up @@ -1190,25 +1213,38 @@ def _runtime_client(self) -> genai.Client:
# One client per event loop, used everywhere in plugin runtime paths.
return _plugin_client(self._loop_client_kwargs())

@loop_local_client
def _list_actions_lock(self) -> asyncio.Lock:
return asyncio.Lock()

def _loop_client_kwargs(self) -> dict[str, Any]:
"""Client kwargs carrying this loop's client from ``http_client_factory``, if one was given."""
return _with_loop_http_client(self._client_kwargs, self._http_client_factory)

async def init(self) -> list[Action | BackgroundAction[Any, Any]]:
"""Initialize the plugin.
"""Initialize the plugin with static known models and embedders.

Registration is static-first and requires no network requests,
preventing startup rate limits (429) on unbilled/free-tier projects.
Unknown or dynamic models resolve lazily on-demand.

Returns:
Actions and background models for known/supported models.
"""
genai_models = await _list_genai_models(self._runtime_client(), is_vertex=True)
# Build this loop's http client now so a factory that returns the wrong
# type raises TypeError here, at setup, not inside the first model run.
if self._http_client_factory is not None:
self._http_client_factory()

actions: list[Action | BackgroundAction[Any, Any]] = []

for name in genai_models.gemini:
# Gemini Models (Vertex does not serve rotating -latest aliases or Gemma via generate_content)
for name in VERTEX_KNOWN_MODELS:
if action := self._resolve_model(name):
actions.append(action)

# Veo Models (background models)
for name in genai_models.veo:
for name in VERTEX_KNOWN_VEO:
actions.append(self._resolve_veo_model(name))

for name in VERTEX_KNOWN_VIRTUAL_TRY_ON:
Expand Down Expand Up @@ -1353,37 +1389,42 @@ def _resolve_embedder(self, name: str) -> Action:
async def list_actions(self) -> list[Action | BackgroundAction[Any, Any]]:
"""Generate a list of available actions or models.

Dynamic discovery is performed lazily and cached across calls.

Returns:
list[Action | BackgroundAction[Any, Any]]: Actions and background models.
"""
if self._list_actions_cache is not None:
return self._list_actions_cache
genai_models = await _list_genai_models(self._runtime_client(), is_vertex=True)
actions_list: list[Action | BackgroundAction[Any, Any]] = []

for name in genai_models.gemini:
if (model_action := self._resolve_model(name)) is not None:
actions_list.append(model_action)
async with self._list_actions_lock():
if self._list_actions_cache is not None:
return self._list_actions_cache

actions_list = list(await self.init())
seen_names = {a.name for a in actions_list}

for name in genai_models.veo:
actions_list.append(
_create_veo_background_model(
genai_models = await _list_genai_models(self._runtime_client(), is_vertex=True)

for name in genai_models.gemini:
if (model_action := self._resolve_model(name)) is not None:
if model_action.name not in seen_names:
actions_list.append(model_action)
seen_names.add(model_action.name)

for name in genai_models.veo:
veo_action = _create_veo_background_model(
name,
self._runtime_client,
VERTEXAI_PLUGIN_NAME,
client_kwargs=self._loop_client_kwargs,
)
)

for name in VERTEX_KNOWN_VIRTUAL_TRY_ON:
if (action := self._resolve_model(name)) is not None:
actions_list.append(action)

for name in VERTEX_KNOWN_EMBEDDERS:
actions_list.append(_create_embedder_action(name, self._runtime_client, VERTEXAI_PLUGIN_NAME))
if veo_action.name not in seen_names:
actions_list.append(veo_action)
seen_names.add(veo_action.name)

self._list_actions_cache = actions_list
return actions_list
self._list_actions_cache = actions_list
return actions_list


def _loop_local_http_client(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ class EmbeddingConfig(GenkitConfig):
}


# Curated set of Google AI embedders registered at init without a discovery
# round-trip. text-embedding-004 is left out: the Gemini API shut it down on
# 2026-01-14. Any other id still resolves on demand.
GOOGLEAI_KNOWN_EMBEDDERS: tuple[str, ...] = ('gemini-embedding-001', 'gemini-embedding-2')


# Curated set of Vertex AI embedders that are verified to be callable.
# The Vertex catalog over-lists embedders (and returns supported_actions=None),
# so embedders are advertised from this list rather than discovered. Multimodal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,22 @@ def _add_model(model_info: ModelInfo, names: list[str]) -> None:
# resolve() writes into SUPPORTED_MODELS later.
GEMINI_CATALOG_IDS = frozenset(SUPPORTED_MODELS)

GOOGLEAI_KNOWN_MODELS: tuple[str, ...] = tuple(
sorted({
*get_args(KnownGoogleAIGemini),
*get_args(KnownGeminiTts),
*get_args(KnownGeminiImage),
*get_args(KnownGemma),
})
)
VERTEX_KNOWN_MODELS: tuple[str, ...] = tuple(
sorted({
*get_args(KnownVertexAIGemini),
*get_args(KnownGeminiTts),
*get_args(KnownGeminiImage),
})
)


DEFAULT_SUPPORTS_MODEL = ModelSupports(
multiturn=True,
Expand Down
Loading
Loading