Generate product text suggestions
Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to generate product text suggestions.
Java
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.merchant.samples.productstudio.v1alpha;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductTextSuggestionsRequest;
import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductTextSuggestionsResponse;
import com.google.shopping.merchant.productstudio.v1alpha.OutputSpec;
import com.google.shopping.merchant.productstudio.v1alpha.ProductInfo;
import com.google.shopping.merchant.productstudio.v1alpha.TextSuggestionsServiceClient;
import com.google.shopping.merchant.productstudio.v1alpha.TextSuggestionsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to generate product text suggestions. */
public class GenerateProductTextSuggestionsSample {
private static String getName(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void generateProductTextSuggestions(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
TextSuggestionsServiceSettings textSuggestionsServiceSettings =
TextSuggestionsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name = getName(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (TextSuggestionsServiceClient textSuggestionsServiceClient =
TextSuggestionsServiceClient.create(textSuggestionsServiceSettings)) {
ProductInfo productInfo =
ProductInfo.newBuilder()
.putProductAttributes("title", "Mens shirt")
.putProductAttributes("description", "A blue shirt for men in size S")
.build();
OutputSpec outputSpec = OutputSpec.newBuilder().setWorkflowId("title").build();
GenerateProductTextSuggestionsRequest request =
GenerateProductTextSuggestionsRequest.newBuilder()
.setName(name)
.setProductInfo(productInfo)
.setOutputSpec(outputSpec)
.build();
System.out.println("Sending GenerateProductTextSuggestions request: " + name);
GenerateProductTextSuggestionsResponse response =
textSuggestionsServiceClient.generateProductTextSuggestions(request);
System.out.println("Generated product text suggestions response below:");
System.out.println(response);
} catch (Exception e) {
System.out.println("An error has occured: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
generateProductTextSuggestions(config);
}
}
Python
# -*- coding: utf-8 -*-
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to generate product text suggestions."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_productstudio_v1alpha import GenerateProductTextSuggestionsRequest
from google.shopping.merchant_productstudio_v1alpha import OutputSpec
from google.shopping.merchant_productstudio_v1alpha import ProductInfo
from google.shopping.merchant_productstudio_v1alpha import TextSuggestionsServiceClient
# Fetches the Merchant Center account ID from the configuration file.
# This ID is used to construct the 'name' for the API request.
_ACCOUNT_ID = configuration.Configuration().read_merchant_info()
# The parent resource name for the GenerateProductTextSuggestionsRequest.
# Format: "accounts/{account}"
_PARENT_RESOURCE_NAME = f"accounts/{_ACCOUNT_ID}"
def generate_product_text_suggestions_sample():
"""Generates product text suggestions for a given product."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client for the TextSuggestionsService.
client = TextSuggestionsServiceClient(credentials=credentials)
# Defines the product information for which suggestions are needed.
# This includes attributes like title and description.
product_info = ProductInfo(
product_attributes={
"title": "Mens shirt",
"description": "A blue shirt for men in size S",
}
)
# Defines the output specification.
# The 'workflow_id' specifies the type of text suggestion, e.g., "title".
output_spec = OutputSpec(workflow_id="title")
# Creates the request object for generating product text suggestions.
# It includes the parent resource name, product information, and output
# specification.
request = GenerateProductTextSuggestionsRequest(
name=_PARENT_RESOURCE_NAME,
product_info=product_info,
output_spec=output_spec,
)
# Sends the request to the API.
print(
f"Sending GenerateProductTextSuggestions request: {_PARENT_RESOURCE_NAME}"
)
try:
response = client.generate_product_text_suggestions(request=request)
# Prints the generated suggestions.
print("Generated product text suggestions response below:")
print(response)
except RuntimeError as e:
# Catches and prints any errors that occur during the API call.
print("An error has occured: ")
print(e)
if __name__ == "__main__":
generate_product_text_suggestions_sample()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-13 UTC.
[null,null,["Last updated 2025-08-13 UTC."],[],[],null,["# Generate product text suggestions\n\nMerchant API code sample to generate product text suggestions. \n\n### Java\n\n // Copyright 2025 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.productstudio.v1alpha;\n\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductTextSuggestionsRequest;\n import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductTextSuggestionsResponse;\n import com.google.shopping.merchant.productstudio.v1alpha.OutputSpec;\n import com.google.shopping.merchant.productstudio.v1alpha.ProductInfo;\n import com.google.shopping.merchant.productstudio.v1alpha.TextSuggestionsServiceClient;\n import com.google.shopping.merchant.productstudio.v1alpha.TextSuggestionsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to generate product text suggestions. */\n public class GenerateProductTextSuggestionsSample {\n\n private static String getName(String accountId) {\n return String.format(\"accounts/%s\", accountId);\n }\n\n public static void generateProductTextSuggestions(Config config) throws Exception {\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n TextSuggestionsServiceSettings textSuggestionsServiceSettings =\n TextSuggestionsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n String name = getName(config.getAccountId().toString());\n\n // Calls the API and catches and prints any network failures/errors.\n try (TextSuggestionsServiceClient textSuggestionsServiceClient =\n TextSuggestionsServiceClient.create(textSuggestionsServiceSettings)) {\n\n ProductInfo productInfo =\n ProductInfo.newBuilder()\n .putProductAttributes(\"title\", \"Mens shirt\")\n .putProductAttributes(\"description\", \"A blue shirt for men in size S\")\n .build();\n\n OutputSpec outputSpec = OutputSpec.newBuilder().setWorkflowId(\"title\").build();\n\n GenerateProductTextSuggestionsRequest request =\n GenerateProductTextSuggestionsRequest.newBuilder()\n .setName(name)\n .setProductInfo(productInfo)\n .setOutputSpec(outputSpec)\n .build();\n\n System.out.println(\"Sending GenerateProductTextSuggestions request: \" + name);\n GenerateProductTextSuggestionsResponse response =\n textSuggestionsServiceClient.generateProductTextSuggestions(request);\n System.out.println(\"Generated product text suggestions response below:\");\n System.out.println(response);\n } catch (Exception e) {\n System.out.println(\"An error has occured: \");\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n generateProductTextSuggestions(config);\n }\n }\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/productstudio/v1alpha/GenerateProductTextSuggestionsSample.java\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2025 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n\n \"\"\"A module to generate product text suggestions.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_productstudio_v1alpha import GenerateProductTextSuggestionsRequest\n from google.shopping.merchant_productstudio_v1alpha import OutputSpec\n from google.shopping.merchant_productstudio_v1alpha import ProductInfo\n from google.shopping.merchant_productstudio_v1alpha import TextSuggestionsServiceClient\n\n\n # Fetches the Merchant Center account ID from the configuration file.\n # This ID is used to construct the 'name' for the API request.\n _ACCOUNT_ID = configuration.Configuration().read_merchant_info()\n # The parent resource name for the GenerateProductTextSuggestionsRequest.\n # Format: \"accounts/{account}\"\n _PARENT_RESOURCE_NAME = f\"accounts/{_ACCOUNT_ID}\"\n\n\n def generate_product_text_suggestions_sample():\n \"\"\"Generates product text suggestions for a given product.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client for the TextSuggestionsService.\n client = TextSuggestionsServiceClient(credentials=credentials)\n\n # Defines the product information for which suggestions are needed.\n # This includes attributes like title and description.\n product_info = ProductInfo(\n product_attributes={\n \"title\": \"Mens shirt\",\n \"description\": \"A blue shirt for men in size S\",\n }\n )\n\n # Defines the output specification.\n # The 'workflow_id' specifies the type of text suggestion, e.g., \"title\".\n output_spec = OutputSpec(workflow_id=\"title\")\n\n # Creates the request object for generating product text suggestions.\n # It includes the parent resource name, product information, and output\n # specification.\n request = GenerateProductTextSuggestionsRequest(\n name=_PARENT_RESOURCE_NAME,\n product_info=product_info,\n output_spec=output_spec,\n )\n\n # Sends the request to the API.\n print(\n f\"Sending GenerateProductTextSuggestions request: {_PARENT_RESOURCE_NAME}\"\n )\n try:\n response = client.generate_product_text_suggestions(request=request)\n # Prints the generated suggestions.\n print(\"Generated product text suggestions response below:\")\n print(response)\n except RuntimeError as e:\n # Catches and prints any errors that occur during the API call.\n print(\"An error has occured: \")\n print(e)\n\n\n if __name__ == \"__main__\":\n generate_product_text_suggestions_sample()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/productstudio/v1alpha/generate_product_text_suggestions_sample.py"]]