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.
Combining lexical full-text search and semantic vector similarity with Reciprocal Rank Fusion in a single Cloud Spanner query lacked a runnable Python example.
|
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 script, hybrid_search_rrf.py, which implements hybrid search in Google Cloud Spanner by combining full-text and vector search using Reciprocal Rank Fusion (RRF), along with a comprehensive test suite in hybrid_search_rrf_test.py. The feedback recommends enhancing the script's flexibility by allowing the Google Cloud project ID to be automatically inferred from Application Default Credentials (ADC) when not explicitly provided. This involves making the project_id parameter optional, resolving it via the Spanner client, updating error messages to use the resolved ID, and adjusting the command-line argument parser. Additionally, the reviewer suggests mocking the project attribute on the Spanner client throughout the unit tests to ensure consistent and clean test execution.
| def run_hybrid_search_rrf(project_id: str, instance_id: str, database_id: str) -> None: | ||
| """Combines full-text and ANN vector search via Reciprocal Rank Fusion. | ||
|
|
||
| Args: | ||
| project_id: The Google Cloud project ID. | ||
| instance_id: The Enterprise-edition Spanner instance ID. | ||
| database_id: The Spanner database ID containing CatalogItems, | ||
| CatalogEmbeddingIndex, and CatalogSearchIndex. | ||
| """ | ||
| client = spanner.Client(project=project_id) | ||
| database = client.instance(instance_id).database(database_id) |
There was a problem hiding this comment.
To allow the project ID to be automatically inferred from Application Default Credentials (ADC) when not explicitly provided, update the project_id parameter to be optional (str | None) and resolve the actual project ID using client.project.
| def run_hybrid_search_rrf(project_id: str, instance_id: str, database_id: str) -> None: | |
| """Combines full-text and ANN vector search via Reciprocal Rank Fusion. | |
| Args: | |
| project_id: The Google Cloud project ID. | |
| instance_id: The Enterprise-edition Spanner instance ID. | |
| database_id: The Spanner database ID containing CatalogItems, | |
| CatalogEmbeddingIndex, and CatalogSearchIndex. | |
| """ | |
| client = spanner.Client(project=project_id) | |
| database = client.instance(instance_id).database(database_id) | |
| def run_hybrid_search_rrf( | |
| project_id: str | None, instance_id: str, database_id: str | |
| ) -> None: | |
| """Combines full-text and ANN vector search via Reciprocal Rank Fusion. | |
| Args: | |
| project_id: The Google Cloud project ID. | |
| instance_id: The Enterprise-edition Spanner instance ID. | |
| database_id: The Spanner database ID containing CatalogItems, | |
| CatalogEmbeddingIndex, and CatalogSearchIndex. | |
| """ | |
| client = spanner.Client(project=project_id) | |
| resolved_project_id = client.project | |
| database = client.instance(instance_id).database(database_id) |
References
- When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| except exceptions.NotFound as err: | ||
| print( | ||
| f"error: database '{database_id}' on instance '{instance_id}' " | ||
| f"in project '{project_id}' was not found: {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: hybrid RRF search failed on database '{database_id}' " | ||
| f"in instance '{instance_id}' for project '{project_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
Update the error messages to use resolved_project_id instead of project_id so that the actual resolved project ID is printed when project_id is inferred from ADC.
| except exceptions.NotFound as err: | |
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{project_id}' was not found: {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: hybrid RRF search failed on database '{database_id}' " | |
| f"in instance '{instance_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.NotFound as err: | |
| print( | |
| f"error: database '{database_id}' on instance '{instance_id}' " | |
| f"in project '{resolved_project_id}' was not found: {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: hybrid RRF search failed on database '{database_id}' " | |
| f"in instance '{instance_id}' for project '{resolved_project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| parser.add_argument( | ||
| "--project-id", | ||
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | ||
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | ||
| ) |
There was a problem hiding this comment.
In accordance with the general rules, the --project-id argument should be optional and default to None (or os.environ.get("GOOGLE_CLOUD_PROJECT") without a fallback string) so that the project can be inferred from Application Default Credentials (ADC) if not provided.
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) | |
| parser.add_argument( | |
| "--project-id", | |
| default=os.environ.get("GOOGLE_CLOUD_PROJECT"), | |
| help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", | |
| ) |
References
- When using argparse for a command-line script, the --project_id argument can be optional, as the project can be inferred from Application Default Credentials (ADC) if not provided.
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) | ||
| database.name = DATABASE_NAME | ||
| snapshot = database.snapshot.return_value.__enter__.return_value |
There was a problem hiding this comment.
Mock the project attribute on the Spanner client to ensure the tests use the expected project ID and avoid printing MagicMock representations in error logs.
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| database.name = DATABASE_NAME | |
| snapshot = database.snapshot.return_value.__enter__.return_value | |
| client = mock_client_class.return_value | |
| client.project = PROJECT_ID | |
| database = client.instance.return_value.database.return_value | |
| database.name = DATABASE_NAME | |
| snapshot = database.snapshot.return_value.__enter__.return_value |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) | ||
| snapshot = database.snapshot.return_value.__enter__.return_value | ||
| snapshot.execute_sql.side_effect = error |
There was a problem hiding this comment.
Mock the project attribute on the Spanner client in the error tests as well to ensure consistent behavior and clean output.
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| snapshot = database.snapshot.return_value.__enter__.return_value | |
| snapshot.execute_sql.side_effect = error | |
| client = mock_client_class.return_value | |
| client.project = PROJECT_ID | |
| database = client.instance.return_value.database.return_value | |
| snapshot = database.snapshot.return_value.__enter__.return_value | |
| snapshot.execute_sql.side_effect = error |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
| database = ( | ||
| mock_client_class.return_value.instance.return_value.database.return_value | ||
| ) | ||
| database.name = DATABASE_NAME | ||
| snapshot = database.snapshot.return_value.__enter__.return_value |
There was a problem hiding this comment.
Mock the project attribute on the Spanner client in the CLI entrypoint test to ensure consistent behavior.
| database = ( | |
| mock_client_class.return_value.instance.return_value.database.return_value | |
| ) | |
| database.name = DATABASE_NAME | |
| snapshot = database.snapshot.return_value.__enter__.return_value | |
| client = mock_client_class.return_value | |
| client.project = PROJECT_ID | |
| database = client.instance.return_value.database.return_value | |
| database.name = DATABASE_NAME | |
| snapshot = database.snapshot.return_value.__enter__.return_value |
There was a problem hiding this comment.
Thanks for the suggestion! We are intentionally keeping project_id: str explicit in the sample signature so callers pass configuration directly rather than relying on implicit ADC resolution, and we intentionally omit project identifiers (project_id / client.project) from stdout and stderr output so project metadata is not echoed in logs.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Description
Hybrid retrieval in Cloud Spanner fuses keyword precision (
SEARCH/SCORE) with semantic vector recall (COSINE_DISTANCE) inside a single SQL query using Reciprocal Rank Fusion (1 / (k + rank)), requiring simultaneousSTRINGandARRAY<FLOAT32>parameter bindings.Adds
spanner_hybrid_search_rrfexecuting a single-round-trip hybrid RRF query from Python.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions