arrow_back

Protecting Sensitive Data in Gen AI Model Responses

Sign in Join
Get access to 700+ labs and courses

Protecting Sensitive Data in Gen AI Model Responses

Lab 1 hour 30 minutes universal_currency_alt 5 Credits show_chart Intermediate
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

GSP1283

Overview

Sensitive Data Protection is a fully managed service designed to help you discover, classify, and protect sensitive information. Key options include Sensitive Data Discovery for continuously profiling your sensitive data, de-identification of sensitive data including redaction, and Cloud Data Loss Prevention (DLP) API to let you build in discovery, inspection, and de-identification into custom workloads and applications.

In addition to identifying and protecting sensitive data in data storage options such as Cloud Storage and BigQuery, you can also identify and protect sensitive data that is returned from Generative AI models. Very useful for mitigating sensitive data concerns across your whole ecosystem!

In this lab, you leverage this ability through a Jupyter notebook that employs the Cloud Data Loss Prevention (DLP) API to classify and redact sensitive data in Gemini 2.0 Flash model responses.

What you'll learn

In this lab, you learn how to:

  • Access a pre-created Jupyter notebook in a Vertex AI Workbench instance
  • Install Python packages for Vertex AI and Cloud Data Loss Prevention (DLP) API
  • Generate example text with sensitive data using the Gemini 2.0 Flash model
  • Define and run Python functions to redact different types of sensitive data in Gemini 2.0 Flash model responses using the DLP API

Prerequisites

While not required, it is helpful to have some previous knowledge about how Vertex AI and Cloud Data Loss Prevention (DLP (API) are commonly used within Google Cloud workflows. For an introduction to these tools before you begin this lab, you can complete the following labs:

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab—remember, once you start, you cannot pause a lab.
Note: Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details pane.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details pane.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To access Google Cloud products and services, click the Navigation menu or type the service or product name in the Search field.

Task 1. Open Jupyter Lab in Vertex AI Workbench

  1. In the Google Cloud console, on the Navigation menu (), click Vertex AI > Workbench.

  2. Find the instance and click on the Open JupyterLab button.

The JupyterLab interface for your Workbench instance opens in a new browser tab.

Task 2. Set up the notebook

Note: You can run any cell in a Python notebook by clicking Run the selected cells (play arrow icon) at the top of the notebook.

Review more information about using Jupyter notebooks in Vertex AI Workbench instances in the documentation.
  1. Click on the .ipynb file.

  2. Run each cell in the Getting started with this notebook section of the notebook.

    Be sure to provide the Project ID and Location as follows:

    • For Project ID, type:
    • For Location, type:

Task 3. Redact specific sensitive data types in Gen AI model responses

The sections identified in this task follow the sections outlined in the notebook. Complete the steps in the notebook as instructed below and return to this lab page to check your progress after completing each notebook section.

Generate simple example text with personally identifiable information (full name) using Gemini 2.0 Flash model

  • Run each cell in the Generate simple example text with personally identifiable information (full name) using Gemini 2.0 Flash model section of the notebook.

Click Check my progress to verify the objective. Generate examples of text with personally identifiable information (PII).

Define and run a Python function to deidentify Gemini 2.0 Flash model responses using built-in global infotypes

  • Run each cell in the Define and run a Python function to deidentify Gemini 2.0 Flash model responses using built-in global infotypes section of the notebook.

Click Check my progress to verify the objective. Define a Python function to deidentify Gemini 2.0 Flash model responses using built-in global infotypes

Generate and de-identify example text with more personally identifiable information (date of birth) using Gemini 2.0 Flash model

  • Run each cell in the Generate and de-identify example text with more personally identifiable information (date of birth) using Gemini 2.0 Flash model section of the notebook.

Click Check my progress to verify the objective. Generate and de-identify example text with more personally identifiable information.

Generate example text with credit card information using Gemini 2.0 Flash model

  • Run each cell in the Generate example text with credit card information using Gemini 2.0 Flash model section of the notebook.

Click Check my progress to verify the objective. Generate example text with credit card information using Gemini 2.0 Flash model.

Test your skills using the built-in global infoType for credit card number

  • Run each cell in the Test your skills using the built-in global infoType for credit card number section of the notebook.

Expand the hints below for some helpful steps!

Option 1: Review the docs

Full solution (Expand to see the full code!)

Click Check my progress to verify the objective. Test your skills using the built-in global infoType for credit card number.

Task 4. Block results from Gen AI models based on sensitive document types

The sections identified in this task follow the sections outlined in the notebook. Complete the steps in the notebook as instructed below and return to this lab page to check your progress after completing each notebook section.

Revise the Python function to block Gemini 2.0 Flash model responses based on specific infotypes for documents

  • Run each cell in the Revise the Python function to block Gemini 2.0 Flash model responses based on specific infotypes for documents section of the notebook.

Click Check my progress to verify the objective. Revise the Python function to block Gemini 2.0 Flash model responses.

Generate an example with source code using Gemini 2.0 Flash model and block results

  • Run each cell in the Generate an example with source code using Gemini 2.0 Flash model and block results section of the notebook.

Click Check my progress to verify the objective. Generate an example with source code using Gemini 2.0 Flash model.

Test your skills using the built-in document infoType for patents

  • Run each cell in the Test your skills using the built-in document infoType for patents section of the notebook.

Expand the hints below for some helpful steps!

Option 1: Review the previous section in the notebook

Full solution (Expand to see the full code!)

Click Check my progress to verify the objective. Test your skills using the built-in document infoType for patents.

Congratulations!

In this lab, you used the Vertex AI and Cloud Data Loss Prevention (DLP) APIs in a Jupyter notebook to generate and redact example sensitive data in Gemini 2.0 Flash model responses using the DLP API.

Next steps / Learn more

Check out the following resources to learn more about the Cloud Data Loss Prevention (DLP) API:

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated March 31, 2025

Lab Last Tested March 31, 2025

Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.