code: def _generate_context_proto(
      metadata_handle: metadata.Metadata,
      context_spec: pipeline_pb2.ContextSpec,
  ) -> metadata_store_pb2.Context:
      """Generates metadata_pb2.Context based on the ContextSpec message.
  
      Args:
        metadata_handle: A handler to access MLMD store.
        context_spec: A pipeline_pb2.ContextSpec message that instructs registering
          of a context.
  
      Returns:
        A metadata_store_pb2.Context message.
  
      Raises:
        RuntimeError: When actual property type does not match provided metadata
          type schema.
      """
      context_type = common_utils.register_type_if_not_exist(
          metadata_handle, context_spec.type
      )
      context_name = data_types_utils.get_value(context_spec.name)
      assert isinstance(
          context_name, str
      ), "context name should be string."
      result = metadata_store_pb2.Context(
          type_id=context_type.id, name=context_name
      )
      for k, v in context_spec.properties.items():
          if k in context_type.properties:
              actual_property_type = (
                  data_types_utils.get_metadata_value_type(v)
              )
              if context_type.properties.get(k) == actual_property_type:
                  data_types_utils.set_metadata_value(
                      result.properties[k], v
                  )
              else:
                  raise RuntimeError(
                      "Property type %s different from provided metadata type property type %s for key %s"
                      % (
                          actual_property_type,
                          context_type.properties.get(k),
                          k,
                      )
                  )
          else:
              data_types_utils.set_metadata_value(
                  result.custom_properties[k], v
              )
      return result
  
file_path: tfx/tfx/orchestration/portable/mlmd/context_lib.py
imports: from typing import List, Optional
  from absl import logging
  from tfx.dsl.compiler import constants
  from tfx.orchestration import data_types_utils
  from tfx.orchestration import metadata
  from tfx.orchestration.portable.mlmd import common_utils
  from tfx.utils import metrics_utils
  from tfx.proto.orchestration import pipeline_pb2
  from tfx.utils import telemetry_utils
  import ml_metadata
  from ml_metadata import errors
  from ml_metadata.proto import metadata_store_pb2
label_desc: Use image node_id 1 for calling a global function with example usage: _generate_context_proto(metadata_handle, context_spec) and returns: result