Skip to content

feat(spanner): Add Vertex AI Gemini ML.PREDICT sample - #14844

Open
iennae wants to merge 6 commits into
mainfrom
feat/spanner-ml-predict-generative-ai
Open

iennae wants to merge 6 commits into
mainfrom
feat/spanner-ml-predict-generative-ai

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

Cloud Spanner generative AI SQL examples lacked a Python client sample and needed a working reference for current Gemini Flash global publisher endpoints (locations/global/publishers/google/models/gemini-3.8-flash), including setting an output token budget large enough to accommodate reasoning tokens.

Adds spanner_ml_predict_generative_ai demonstrating idempotent CREATE MODEL IF NOT EXISTS registration and parameterized ML.PREDICT prompt inference over stored rows.

Note: Stacked on #14836 (feat/spanner-vector-search-knn) so this PR shows only this sample's diff. It will merge into main after #14836.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Python developers implementing semantic similarity search on Cloud Spanner lacked a client library example showing parameterized FLOAT32 vector bindings with COSINE_DISTANCE.
Existing generative AI examples for Cloud Spanner SQL referenced retired PaLM models without a Python client reference for registering and querying current Gemini endpoints.
@iennae
iennae requested review from a team as code owners November 17, 2026 18:48
@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

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

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.

Code Review

This pull request introduces a new Python script and corresponding tests to register a remote Gemini model in Spanner and evaluate product recommendations using ML.PREDICT. The review feedback suggests correcting a typo in the model name from 'gemini-3.8-flash' to 'gemini-1.5-flash' (and updating the tests accordingly), changing the DDL statement to 'CREATE MODEL IF NOT EXISTS' to avoid slow and redundant schema updates, and using a case-insensitive assertion in the tests to prevent flakiness from variable LLM output casing.

model_name = "GeminiRecommendationModel"
endpoint = (
f"//aiplatform.googleapis.com/projects/{project_id}"
f"/locations/{location}/publishers/google/models/gemini-3.8-flash"

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.

high

The model name gemini-3.8-flash appears to be a typo, as there is no Gemini 3.8 model. It should likely be gemini-1.5-flash or gemini-2.5-flash. Using a non-existent model name will cause the remote model registration or inference to fail in live environments.

Suggested change
f"/locations/{location}/publishers/google/models/gemini-3.8-flash"
f"/locations/{location}/publishers/google/models/gemini-1.5-flash"

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.

Thanks! gemini-3.8-flash is intentional: it is the model this sample was run against, so the name stays.

)

ddl_args = database.update_ddl.call_args.args[0]
assert "locations/global/publishers/google/models/gemini-3.8-flash" in ddl_args[0]

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.

high

Since we are correcting the model name from gemini-3.8-flash to gemini-1.5-flash in the main code, we should also update the corresponding test assertion to match.

Suggested change
assert "locations/global/publishers/google/models/gemini-3.8-flash" in ddl_args[0]
assert "locations/global/publishers/google/models/gemini-1.5-flash" in ddl_args[0]

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.

Thanks! The model name is unchanged (see the reply on the sample), so the assertion stays.

f"/locations/{location}/publishers/google/models/gemini-3.8-flash"
)
ddl_statement = f"""
CREATE OR REPLACE MODEL {model_name}

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

The PR description mentions demonstrating idempotent CREATE MODEL IF NOT EXISTS registration, but the DDL statement uses CREATE OR REPLACE MODEL. Using CREATE OR REPLACE MODEL will recreate the model on every execution, which is a slow DDL operation and an anti-pattern in Spanner for regular query paths. Changing this to CREATE MODEL IF NOT EXISTS ensures true idempotency and avoids unnecessary schema updates.

Suggested change
CREATE OR REPLACE MODEL {model_name}
CREATE MODEL IF NOT EXISTS {model_name}

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.

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

out, err = capsys.readouterr()
assert err == ""
assert "Product Name: Cordless Drill" in out
assert "Recommendation: YES" in out

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

LLM outputs can sometimes vary slightly in casing (e.g., returning Yes or yes instead of YES), even with temperature = 0.0. To prevent potential test flakiness in CI/CD environments, it is safer to perform a case-insensitive assertion.

Suggested change
assert "Recommendation: YES" in out
assert "Recommendation: YES" in out.upper()

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.

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

…ation

Registering the remote Gemini model with IF NOT EXISTS avoids redundant schema replacement on repeat runs, and case-insensitive live assertions prevent casing flake.
…ICT SQL

Gemini 3.8 Flash deprecates and ignores legacy sampling parameters such as temperature, so passing only maxOutputTokens aligns the SQL snippet with current model conventions.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Problem: the sample waits on a long-running operation with a client-side
timeout. When that wait expires the client raises TimeoutError, which is
not an API call error, so the sample ended with an unhandled traceback.

Solution: catch TimeoutError and print a clear message to stderr noting
that only the wait timed out and the operation may still be running (the
timeout does not cancel it). Add a unit test covering the new handler.
Base automatically changed from feat/spanner-vector-search-knn to main December 1, 2026 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants