How to create a job with custom attributes in Google Cloud Talent Solution

397 Views Asked by At

For Custom Attributes, Google don't provide an example of use in their example code.

Google docs with missing code in its Python example.

Google's example for creating a job and notes "Create Job with Custom Attributes" but doesn't actually include any code for Custom Attrinbutes:

def sample_create_job(project_id, tenant_id, company_name, requisition_id,
                      language_code):
    """Create Job with Custom Attributes"""

    client = talent_v4beta1.JobServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # company_name = 'Company name, e.g. projects/your-project/companies/company-id'
    # requisition_id = 'Job requisition ID, aka Posting ID. Unique per job.'
    # language_code = 'en-US'

    if isinstance(project_id, six.binary_type):
        project_id = project_id.decode('utf-8')
    if isinstance(tenant_id, six.binary_type):
        tenant_id = tenant_id.decode('utf-8')
    if isinstance(company_name, six.binary_type):
        company_name = company_name.decode('utf-8')
    if isinstance(requisition_id, six.binary_type):
        requisition_id = requisition_id.decode('utf-8')
    if isinstance(language_code, six.binary_type):
        language_code = language_code.decode('utf-8')
    parent = client.tenant_path(project_id, tenant_id)
    job = {
        'company': company_name,
        'requisition_id': requisition_id,
        'language_code': language_code
    }

    response = client.create_job(parent, job)
    print('Created job: {}'.format(response.name))

How do I define Custom Attributes for a job?

Something like the following worked for an earlier version of Talent Solution:

job['custom_attributes'] = {
    'custom_name' : {'stringValues' : ['s0', 's1', 's2']},
    ...
}

I've now tried this:

from google.cloud.talent_v4beta1.types import CustomAttribute
job['custom_attributes'] = [
    {
        'key' : 'keyname',
        'value': CustomAttribute(string_values=[valuestring], filterable=True)
    }
]

But when I try to create or update a job an exception is thrown: TypeError: {'key': 'keyname', 'value': string_values: "valuestring" filterable: true } has type dict, but expected one of: bytes, unicode

2

There are 2 best solutions below

1
On BEST ANSWER

In Nov 2020, Google doc official notes how to do just that


from google.cloud import talent
import six


def create_job(project_id, tenant_id, company_id, requisition_id):
    """Create Job with Custom Attributes"""

    client = talent.JobServiceClient()

    # project_id = 'Your Google Cloud Project ID'
    # tenant_id = 'Your Tenant ID (using tenancy is optional)'
    # company_id = 'Company name, e.g. projects/your-project/companies/company-id'
    # requisition_id = 'Job requisition ID, aka Posting ID. Unique per job.'
    # language_code = 'en-US'

    if isinstance(project_id, six.binary_type):
        project_id = project_id.decode("utf-8")
    if isinstance(tenant_id, six.binary_type):
        tenant_id = tenant_id.decode("utf-8")
    if isinstance(company_id, six.binary_type):
        company_id = company_id.decode("utf-8")

    # Custom attribute can be string or numeric value,
    # and can be filtered in search queries.
    # https://cloud.google.com/talent-solution/job-search/docs/custom-attributes
    custom_attribute = talent.CustomAttribute()
    custom_attribute.filterable = True
    custom_attribute.string_values.append("Intern")
    custom_attribute.string_values.append("Apprenticeship")

    parent = f"projects/{project_id}/tenants/{tenant_id}"

    job = talent.Job(
        company=company_id,
        title="Software Engineer",
        requisition_id=requisition_id,
        description="This is a description of this job",
        language_code="en-us",
        custom_attributes={"FOR_STUDENTS": custom_attribute}
    )

    response = client.create_job(parent=parent, job=job)
    print(f"Created job: {response.name}")
    return response.name

I also have a WIP python library to help it is based on pydantic object.

    j = Job(
            company=company.name,
            requisition_id=uuid4().hex,
            title="engineer",
            description="implement system",
            custom_attributes={
                "tags": CustomAttributes(
                    string_values=["hello"],
                    filterable=True,
                    keyword_searchable=True
                )
            }
    )
    j.create(tenant=tenant)

0
On
from google.cloud.talent_v4beta1.types import CustomAttribute

job = {
    'title' : ...
}

job['custom_attributes'] = {
    'keyname0' : CustomAttributes(
        string_values=['eg0', 'eg1'],
        filterable=False),
    'keyname1' : ...
}