Search code examples
pythongoogle-cloud-platformgoogle-cloud-dlp

Google DLP: "ValueError: Protocol message Value has no "stringValue" field."


I have a method where I build a table for multiple items for Google's DLP inspect API which can take either a ContentItem, or a table of values

Here is how the request is constructed:

def redact_text(text_list):
    dlp = google.cloud.dlp.DlpServiceClient()
    project = 'my-project'
    parent = dlp.project_path(project)
    items = build_item_table(text_list)

    info_types = [{'name': 'EMAIL_ADDRESS'}, {'name': 'PHONE_NUMBER'}]
    inspect_config = {
        'min_likelihood': "LIKELIHOOD_UNSPECIFIED",
        'include_quote': True,
        'info_types': info_types
    }
    response = dlp.inspect_content(parent, inspect_config, items)

    return response


def build_item_table(text_list):
    rows = []
    for item in text_list:
        row = {"values": [{"stringValue": item}]}
        rows.append(row)
    table = {"table": {"headers": [{"name": "something"}], "rows": rows}}
    return table

When I run this I get back the error ValueError: Protocol message Value has no "stringValue" field. Even though the this example and the docs say otherwise.

Is there something off in how I build the request?

Edit: Here's the output from build_item_table

{
    'table':
    {
        'headers': 
        [
            {'name': 'value'}
        ], 
        'rows': 
        [
            {
                'values': 
                [
                    {
                        'stringValue': 'My name is Jenny and my number is (555) 867-5309, you can also email me at [email protected], another email you can reach me at is [email protected].  '
                    }
                ]
            }, 
            {
                'values': 
                [
                    {
                        'stringValue': 'Jimbob Doe (555) 111-1233, that one place down the road [email protected]'
                    }
                ]
            }
        ]
    }
}

Solution

  • Try string_value .... python uses the field names, not the type name.