Translation — Best Practices

Distilled from 3 notebooks tagged Translation in the GoogleCloudPlatform/generative-ai repository. The From the notebooks section below cites the per-notebook source for grounding.

Do this

  • Initialize Vertex AI with an explicit project and location before using evaluation APIs.
  • Install google-cloud-aiplatform with the evaluation extra before importing Vertex AI evaluation APIs.
  • Use a structured pandas DataFrame with source, response, and reference columns for translation evaluation.
  • Inspect both summary metrics and row-based metrics from EvalResult to understand aggregate quality and individual failures.
  • Delete the ExperimentRun referenced by evaluation metadata when finished.
  • Use environment variables for project ID and region when notebook parameters are not provided.
  • Set source_language_code and target_language_code explicitly for translation calls.
  • Use client.common_location_path for regional Translation API resource paths.
  • Use glossaries to consistently translate domain-specific words and short phrases.
  • Check Translation API documentation for supported language codes, content formats, available regions, and advanced endpoints.
  • Remove empty lines from both source and reference documents before generating training pairs.
  • Preserve document order across paragraphs and tables when creating training data.
  • Validate source and reference paths and formats before processing.
  • Capture mismatched blocks and over-200-word lines instead of silently writing bad TSV rows.
  • Use dataset display names built from prefix, source language, target language, and optional suffix.

Avoid this

  • Forgetting to set PROJECT_ID before Vertex AI initialization.
  • Not restarting the Jupyter runtime after installing google-cloud-aiplatform[evaluation].
  • Assuming Colab authentication runs everywhere; the evaluation notebook only runs Colab auth when google.colab is present in sys.modules.
  • Overlooking that the evaluation notebook initializes Vertex AI in us-central1.
  • Deleting or losing track of the ExperimentRun referenced in eval_result.metadata during cleanup.
  • Forgetting that Cloud Translation and Cloud Storage are billable components.
  • Using glossary entries for long text instead of words or short phrases, usually fewer than five words.
  • Creating a TSV locally but passing import_data a local path instead of the required Cloud Storage URI.

From the notebooks

Evaluate a Translation Model

  • Install google-cloud-aiplatform with the evaluation extra before importing evaluation APIs.
  • Initialize vertexai with an explicit project and location.
  • Use a structured pandas DataFrame with source, response, and reference columns.
  • Inspect both summary metrics and row-based metrics from EvalResult.
  • Delete the ExperimentRun created by evaluation when finished.

Getting Started with Translation

  • Use environment variables for project ID and region when notebook parameters are not provided.
  • Set source_language_code and target_language_code explicitly.
  • Use client.common_location_path for regional Translation API resource paths.
  • Use glossaries to consistently translate domain-specific words and phrases.
  • Check supported language codes and supported content formats in the Translation API documentation.

Generate training dataset for Cloud Translation API NMT (Neural Machine Translation) model training

  • Remove empty lines from both source and reference documents before making line pairs.
  • Preserve document order across paragraphs and tables when generating training pairs.
  • Validate source and reference paths and formats before processing.
  • Capture mismatched blocks and over-200-word lines instead of silently adding bad TSV rows.
  • Use dataset display names built from prefix, source language, target language, and optional suffix.

Back to Translation · Best Practices Map