Skip to content

feat(py): add Gemini 3.1 preview support to python #5032

Draft
cabljac wants to merge 2 commits intogenkit-ai:mainfrom
invertase:feat/py-gemini-3-1-preview-support
Draft

feat(py): add Gemini 3.1 preview support to python #5032
cabljac wants to merge 2 commits intogenkit-ai:mainfrom
invertase:feat/py-gemini-3-1-preview-support

Conversation

@cabljac
Copy link
Copy Markdown
Contributor

@cabljac cabljac commented Mar 31, 2026

Summary

  • Adds Python support for new Gemini 3.1 preview models:

    • googleai/gemini-3.1-flash-lite-preview
image
  • googleai/gemini-3.1-pro-preview-customtools
image
  • googleai/gemini-3.1-pro-preview
image
  • vertexai/gemini-3.1-flash-lite-preview
image
  • vertexai/gemini-3.1-pro-preview
image
  • Updates Vertex plugin routing so Gemini 3.1 preview models default to global location (while non-3.1-preview models keep configured region).

  • Updates samples/Dev UI flows so all 5 models can be exercised directly:

    • prompts sample now includes explicit GoogleAI Pro preview flows.
    • vertexai-imagen sample includes explicit Vertex Gemini 3.1 preview flows.
  • Adds/updates tests for enum support and Vertex global-routing behavior.

Issue

Checklist:

Construct Google GenAI clients with explicit typed keyword arguments instead of unpacked kwargs maps, and apply Ruff formatting to plugin tests.

Made-with: Cursor
@github-actions github-actions bot added docs Improvements or additions to documentation python Python labels Mar 31, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for Gemini 3.1 preview models (Flash Lite, Pro, and Pro with Custom Tools) across the Google AI and Vertex AI plugins. A key architectural change includes a routing mechanism in the Vertex AI plugin to ensure Gemini 3.1 preview models use the 'global' location, while maintaining configured locations for other models. The PR also updates documentation, enums, and samples to reflect these additions. Feedback was provided regarding the newly defined ModelInfo constants, which are currently not registered in the SUPPORTED_MODELS dictionary, potentially causing them to fall back to generic metadata in the UI.

Comment on lines +725 to +762
GEMINI_3_1_FLASH_LITE_PREVIEW = ModelInfo(
label='Google AI - Gemini 3.1 Flash Lite Preview',
supports=Supports(
multiturn=True,
media=True,
tools=True,
tool_choice=True,
system_role=True,
constrained=Constrained.NO_TOOLS,
output=['text', 'json'],
),
)

GEMINI_3_1_PRO_PREVIEW_CUSTOMTOOLS = ModelInfo(
label='Google AI - Gemini 3.1 Pro Preview Custom Tools',
supports=Supports(
multiturn=True,
media=True,
tools=True,
tool_choice=True,
system_role=True,
constrained=Constrained.NO_TOOLS,
output=['text', 'json'],
),
)

GEMINI_3_1_PRO_PREVIEW = ModelInfo(
label='Google AI - Gemini 3.1 Pro Preview',
supports=Supports(
multiturn=True,
media=True,
tools=True,
tool_choice=True,
system_role=True,
constrained=Constrained.NO_TOOLS,
output=['text', 'json'],
),
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While these ModelInfo constants are correctly defined, they are currently not being used for model lookup because they aren't added to the SUPPORTED_MODELS dictionary. This means that when these models are resolved, they will fall back to a generic ModelInfo with a default label and support flags, rather than using the specific metadata defined here.

Consider populating the SUPPORTED_MODELS dictionary with these constants (and existing ones) to ensure the correct metadata is surfaced in the Genkit UI and used during execution.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andyma-star can we address this?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can.

@cabljac cabljac changed the title Feat/py gemini 3 1 preview support feat: python gemini 3.1 preview support Mar 31, 2026
@cabljac cabljac changed the title feat: python gemini 3.1 preview support feat(python): python gemini 3.1 preview support Mar 31, 2026
@cabljac cabljac changed the title feat(python): python gemini 3.1 preview support feat(python): add Gemini 3.1 preview support to python Mar 31, 2026
@huangjeff5 huangjeff5 changed the title feat(python): add Gemini 3.1 preview support to python feat(py): add Gemini 3.1 preview support to python Mar 31, 2026
ai = Genkit(
plugins=[GoogleAI()],
model=f'googleai/{gemini.GoogleAIGeminiVersion.GEMINI_3_FLASH_PREVIEW}',
model=f'googleai/{gemini.GoogleAIGeminiVersion.GEMINI_3_1_PRO_PREVIEW_CUSTOMTOOLS}',
Copy link
Copy Markdown
Contributor

@huangjeff5 huangjeff5 Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need it to be the CUSTOMTOOLS preview? Is tool calling not available in the regular 3.1 pro model?

try:
response = await draw_image_with_imagen()
print(response.model_dump_json(indent=2)) # noqa: T201
print(await summarize_with_gemini_31_pro()) # noqa: T201
Copy link
Copy Markdown
Contributor

@huangjeff5 huangjeff5 Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we can skip adding these flows in the vertexai sample since it seems to be showing off basic generate usage, and this is the Imagen sample.

In general I think updating existing samples to use the latest model is good enough, unless there are some major new features to demo.

@huangjeff5
Copy link
Copy Markdown
Contributor

Do the new models show up correctly in dev UI?

@jeffdh5
Copy link
Copy Markdown
Contributor

jeffdh5 commented Apr 1, 2026

The two-client approach is going to get messy since we have to duplicate all those arguments. I'd suggest a location-keyed client cache instead:

# __init__: store typed args, drop _global_runtime_client
self._api_key = api_key
self._credentials = credentials
self._debug_config = debug_config
self._http_options = _inject_attribution_headers(http_options, base_url, api_version)
self._client_by_location: dict[str, Callable[[], genai.client.Client]] = {}
self._runtime_client = self._client_getter(self._location)

def _make_client(self, location: str) -> genai.client.Client:
    return genai.client.Client(
        vertexai=True,
        api_key=self._api_key,
        credentials=self._credentials,
        project=self._project,
        location=location,
        debug_config=self._debug_config,
        http_options=self._http_options,
    )

def _client_getter(self, location: str) -> Callable[[], genai.client.Client]:
    if location not in self._client_by_location:
        self._client_by_location[location] = loop_local_client(
            lambda loc=location: self._make_client(loc)
        )
    return self._client_by_location[location]

Rename _is_global_vertex_gemini_model → returns the actual location so it's reusable:

def _preferred_vertex_location(name: str, default: str) -> str:
    lower = name.lower()
    if lower.startswith('gemini-3.1-') and 'preview' in lower:
        return 'global'
    return default

And there's a subtle bug in the current _runmodel_client_getter is computed but ImagenModel/VeoModel still hardcode self._runtime_client(). With the cache approach that goes away naturally:

async def _run(request: ModelRequest, ctx: ActionRunContext) -> ModelResponse:
    client = self._client_getter(_preferred_vertex_location(clean_name, self._location))()
    if clean_name.lower().startswith('image'):
        model = ImagenModel(clean_name, client)
    elif is_veo_model(clean_name):
        model = VeoModel(clean_name, client)
    else:
        model = GeminiModel(clean_name, client)
    return await model.generate(request, ctx)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants