0

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

Carl
  • 2,896
  • 2
  • 32
  • 50
  • Your link isn't a valid URL. Could you perhaps link to the documentation? – Christian Reall-Fluharty Jun 21 '19 at 14:26
  • thank you, Christian. I've fixed the link. and copied in the code at fault. – Carl Jun 21 '19 at 14:28
  • nerdi.org - the documentation is wrong. I've corrected the link in my question and also copied the code that comments that it covers the use of custom attributes but then neglects to do so – Carl Jun 21 '19 at 14:29
  • Yeah, there is definitely some information lacking for non-Golang examples. The general format I can find its: map (key: string, value: object (CustomAttribute)) – Nerdi.org Jun 21 '19 at 14:38
  • Nerdi.org - any ideas how to create a CustomAttribute in Python? – Carl Jun 21 '19 at 20:44
  • Discussed in JavaScript: https://stackoverflow.com/questions/52861965/google-cloud-talent-solution-fetch-a-job-by-requisitionid – Carl Jun 22 '19 at 15:55

2 Answers2

1

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)

CircleOnCircles
  • 3,646
  • 1
  • 25
  • 30
  • 1
    Thank you @CircleOnCircles. I no longer work with any Google APIs but I've marked your answer as the accepted on. – Carl Nov 06 '20 at 12:37
0
from google.cloud.talent_v4beta1.types import CustomAttribute

job = {
    'title' : ...
}

job['custom_attributes'] = {
    'keyname0' : CustomAttributes(
        string_values=['eg0', 'eg1'],
        filterable=False),
    'keyname1' : ...
}
Carl
  • 2,896
  • 2
  • 32
  • 50