De-identify sensitive data by replacing with infoType

Uses the Data Loss Prevention API to de-identify sensitive data in a string by replacing it with the infoType.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


To learn how to install and use the client library for Cloud DLP, see the Cloud DLP Client Libraries.

def deidentify_with_replace_infotype(project, item, info_types):
    """Uses the Data Loss Prevention API to deidentify sensitive data in a
    string by replacing it with the info type.
        project: The Google Cloud project id to use as a parent resource.
        item: The string to deidentify (will be treated as text).
        info_types: A list of strings representing info types to look for.
            A full list of info type categories can be fetched from the API.
        None; the response from the API is printed to the terminal.

    # Import the client library

    # Instantiate a client
    dlp =

    # Convert the project id into a full resource id.
    parent = f"projects/{project}"

    # Construct inspect configuration dictionary
    inspect_config = {"info_types": [{"name": info_type} for info_type in info_types]}

    # Construct deidentify configuration dictionary
    deidentify_config = {
        "info_type_transformations": {
            "transformations": [
                {"primitive_transformation": {"replace_with_info_type_config": {}}}

    # Call the API
    response = dlp.deidentify_content(
            "parent": parent,
            "deidentify_config": deidentify_config,
            "inspect_config": inspect_config,
            "item": {"value": item},

    # Print out the results.

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser