Google Cloud Recommendations AI Client Library
The Google Cloud Recommendations AI API client library enables developers to integrate recommendation capabilities into their applications. It provides functionalities for managing product catalogs, ingesting user events, and generating personalized recommendations. The current version is `0.13.0`, and new releases typically occur frequently as part of the broader Google Cloud Python client libraries monorepo.
Warnings
- gotcha Recommendations AI offers several clients (`RecommendationsAiClient`, `CatalogServiceClient`, `UserEventServiceClient`, `PredictionServiceClient`). Use the appropriate client for your specific operation (e.g., `CatalogServiceClient` for managing items, `PredictionServiceClient` for recommendations). `RecommendationsAiClient` often acts as an orchestrator but direct access to specific service clients is common.
- breaking Google Cloud Python client libraries are progressively dropping support for older Python versions. While version `0.13.0` currently supports Python 3.9, future minor or major releases will likely drop support for Python 3.9 (and potentially 3.10) in alignment with Python's EOL schedule, potentially causing compatibility issues for older environments.
- gotcha Google Cloud APIs use strict resource name formats (e.g., `projects/{project}/locations/{location}/catalogs/{catalog}`). Incorrectly formatted resource names are a common source of `InvalidArgument` or `NotFound` errors.
- gotcha The library provides both synchronous and asynchronous client versions (e.g., `CatalogServiceClient` and `AsyncCatalogServiceClient`). Using the incorrect client in an asynchronous context (e.g., calling synchronous methods with `await`) will lead to `TypeError` or unexpected behavior.
- gotcha Many `list` or `get` operations for Recommendations AI will return empty results or `NotFound` errors if the underlying catalog, user events, or models have not been properly populated and ingested through the API or console.
Install
-
pip install google-cloud-recommendations-ai
Imports
- RecommendationsAiClient
from google.cloud.recommendations_ai_v1beta1 import RecommendationsAiClient
- CatalogServiceClient
from google.cloud.recommendations_ai_v1beta1 import CatalogServiceClient
Quickstart
import os
from google.cloud.recommendations_ai_v1beta1 import CatalogServiceClient
project_id = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')
location_id = os.environ.get('GCP_LOCATION_ID', 'global') # Recommendations AI often uses 'global'
def list_first_five_catalog_items():
"""Lists the first five catalog items in the default catalog."""
client = CatalogServiceClient()
# The parent for listing catalog items is a catalog resource.
# 'default_catalog' is the common default for Recommendations AI.
parent = f"projects/{project_id}/locations/{location_id}/catalogs/default_catalog"
request = {
"parent": parent,
"page_size": 5
}
print(f"Listing catalog items for parent: {parent}")
try:
page_iterator = client.list_catalog_items(request=request)
found_items = False
for item in page_iterator:
print(f"Catalog Item ID: {item.id}, Title: {item.title}")
found_items = True
if not found_items:
print("No catalog items found. Ensure your catalog is populated.")
except Exception as e:
print(f"An error occurred: {e}")
print("Make sure 'GCP_PROJECT_ID' and 'GCP_LOCATION_ID' environment variables are set or replaced.")
print("Also, ensure the Recommendations AI API is enabled and your service account has permissions.")
if __name__ == '__main__':
list_first_five_catalog_items()