# Buggy code
def train(
    project_id,
    job_id_output_path,
    python_module=None,
    package_uris=None,
    region=None,
    args=None,
    job_dir=None,
    python_version=None,
    runtime_version=None,
    master_image_uri=None,
    worker_image_uri=None,
    training_input=None,
    job_id_prefix=None,
    job_id=None,
    wait_interval=30,
):
    """Creates a MLEngine training job.

    Args:
        project_id (str): Required. The ID of the parent project of the job.
        python_module (str): Required. The Python module name to run after
            installing the packages.
        package_uris (list): Required. The Google Cloud Storage location of
            the packages with the training program and any additional
            dependencies. The maximum number of package URIs is 100.
        region (str): Required. The Google Compute Engine region to run the
            training job in
        args (list): Command line arguments to pass to the program.
        job_dir (str): A Google Cloud Storage path in which to store training
            outputs and other data needed for training. This path is passed
            to your TensorFlow program as the '--job-dir' command-line
            argument. The benefit of specifying this field is that Cloud ML
            validates the path for use in training.
        python_version (str): Optional. The version of Python used in training.
            If not set, the default version is '2.7'. Python '3.5' is
            available when runtimeVersion is set to '1.4' and above.
            Python '2.7' works with all supported runtime versions.
        runtime_version (str): Optional. The Cloud ML Engine runtime version
            to use for training. If not set, Cloud ML Engine uses the
            default stable version, 1.0.
        master_image_uri (str): The Docker image to run on the master replica.
            This image must be in Container Registry.
        worker_image_uri (str): The Docker image to run on the worker replica.
            This image must be in Container Registry.
        training_input (dict): Input parameters to create a training job.
        job_id_prefix (str): the prefix of the generated job id.
        job_id (str): the created job_id, takes precedence over generated job
            id if set.
        wait_interval (int): optional wait interval between calls
            to get job status. Defaults to 30.
    """
    if not training_input:
        training_input = {}
    if python_module:
        training_input["pythonModule"] = python_module
    if package_uris:
        training_input["packageUris"] = package_uris
    if region:
        training_input["region"] = region
    if args:
        training_input["args"] = args
    if job_dir:
        training_input["jobDir"] = job_dir
    if python_version:
        training_input["pythonVersion"] = python_version
    if runtime_version:
        training_input["runtimeVersion"] = runtime_version
    if master_image_uri:
        if "masterConfig" not in training_input:
            training_input["masterConfig"] = {}
        training_input["masterConfig"]["imageUri"] = master_image_uri
    if worker_image_uri:
        if "workerConfig" not in training_input:
            training_input["workerConfig"] = {}
        training_input["workerConfig"]["imageUri"] = worker_image_uri
    job = {"trainingInput": training_input}
    return create_job(
        project_id,
        job,
        job_id_prefix,
        job_id,
        wait_interval,
        job_id_output_path=job_id_output_path,
    )


# Traceback (TypeError)
INFO:root:Start KFP context with ID: 858b3ff01cdeed5c0b0b7fd9d2655641.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/local/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/ml/kfp_component/launcher/__main__.py", line 45, in <module>
main()
File "/ml/kfp_component/launcher/__main__.py", line 42, in main
launch(args.file_or_module, args.args)
File "kfp_component/launcher/launcher.py", line 45, in launch
return fire.Fire(module, command=args, name=module.__name__)
File "/usr/local/lib/python2.7/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/usr/local/lib/python2.7/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/usr/local/lib/python2.7/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "kfp_component/google/ml_engine/_batch_predict.py", line 70, in batch_predict
create_job(project_id, job, job_id_prefix, wait_interval)
File "kfp_component/google/ml_engine/_create_job.py", line 48, in create_job
return CreateJobOp(project_id, job, job_id_prefix, job_id, wait_interval
File "kfp_component/google/ml_engine/_create_job.py", line 63, in execute_and_wait
self._set_job_id(ctx.context_id())
File "kfp_component/google/ml_engine/_create_job.py", line 74, in _set_job_id
job_id = gcp_common.normalize_name(job_id)
File "kfp_component/google/common/_utils.py", line 45, in normalize_name
invalid_char_placeholder, name)
File "/usr/local/lib/python2.7/re.py", line 155, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer

# Fixed code
def train(
    project_id,
    job_id_output_path,
    python_module=None,
    package_uris=None,
    region=None,
    args=None,
    job_dir=None,
    python_version=None,
    runtime_version=None,
    master_image_uri=None,
    worker_image_uri=None,
    training_input=None,
    job_id_prefix=None,
    job_id=None,
    wait_interval=30,
):
    """Creates a MLEngine training job.

    Args:
        project_id (str): Required. The ID of the parent project of the job.
        python_module (str): Required. The Python module name to run after
            installing the packages.
        package_uris (list): Required. The Google Cloud Storage location of
            the packages with the training program and any additional
            dependencies. The maximum number of package URIs is 100.
        region (str): Required. The Google Compute Engine region to run the
            training job in
        args (list): Command line arguments to pass to the program.
        job_dir (str): A Google Cloud Storage path in which to store training
            outputs and other data needed for training. This path is passed
            to your TensorFlow program as the '--job-dir' command-line
            argument. The benefit of specifying this field is that Cloud ML
            validates the path for use in training.
        python_version (str): Optional. The version of Python used in training.
            If not set, the default version is '2.7'. Python '3.5' is
            available when runtimeVersion is set to '1.4' and above.
            Python '2.7' works with all supported runtime versions.
        runtime_version (str): Optional. The Cloud ML Engine runtime version
            to use for training. If not set, Cloud ML Engine uses the
            default stable version, 1.0.
        master_image_uri (str): The Docker image to run on the master replica.
            This image must be in Container Registry.
        worker_image_uri (str): The Docker image to run on the worker replica.
            This image must be in Container Registry.
        training_input (dict): Input parameters to create a training job.
        job_id_prefix (str): the prefix of the generated job id.
        job_id (str): the created job_id, takes precedence over generated job
            id if set.
        wait_interval (int): optional wait interval between calls
            to get job status. Defaults to 30.
    """
    if not training_input:
        training_input = {}
    if python_module:
        training_input["pythonModule"] = python_module
    if package_uris:
        training_input["packageUris"] = package_uris
    if region:
        training_input["region"] = region
    if args:
        training_input["args"] = args
    if job_dir:
        training_input["jobDir"] = job_dir
    if python_version:
        training_input["pythonVersion"] = python_version
    if runtime_version:
        training_input["runtimeVersion"] = runtime_version
    if master_image_uri:
        if "masterConfig" not in training_input:
            training_input["masterConfig"] = {}
        training_input["masterConfig"]["imageUri"] = master_image_uri
    if worker_image_uri:
        if "workerConfig" not in training_input:
            training_input["workerConfig"] = {}
        training_input["workerConfig"]["imageUri"] = worker_image_uri
    job = {"trainingInput": training_input}
    return create_job(
        project_id=project_id,
        job=job,
        job_id_prefix=job_id_prefix,
        job_id=job_id,
        wait_interval=wait_interval,
        job_id_output_path=job_id_output_path,
    )