Repository navigation
Conversation
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.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
| f"/locations/{location}/publishers/google/models/gemini-3.8-flash" | |
| f"/locations/{location}/publishers/google/models/gemini-1.5-flash" |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
| 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] |
There was a problem hiding this comment.
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} |
There was a problem hiding this comment.
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.
| CREATE OR REPLACE MODEL {model_name} | |
| CREATE MODEL IF NOT EXISTS {model_name} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| assert "Recommendation: YES" in out | |
| assert "Recommendation: YES" in out.upper() |
There was a problem hiding this comment.
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.
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_aidemonstrating idempotentCREATE MODEL IF NOT EXISTSregistration and parameterizedML.PREDICTprompt inference over stored rows.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions