Create Metadata fields

You can create metadata from the available metadata schema in your organization. See Metadata in Schema section for details.

## Fetch metadata schema ontology. A Labelbox workspace has a single metadata ontology.
metadata_ontology = client.get_data_row_metadata_ontology()
# List all available fields

To construct a metadata field you must provide the Schema Id for the field and the value that will be uploaded. You can either construct a DataRowMetadataField object or specify the Schema Id and value in a dictionary format.

## Construct a metadata field of string kind
tag_metadata_field = DataRowMetadataField(
    name="tag",  # specify the schema name
    value="tag_string", # typed inputs

# Construct an metadata field of datetime
capture_datetime_field = DataRowMetadataField(
    name="captureDateTime",  # specify the schema name
    value=datetime.datetime.utcnow(), # typed inputs

# Construct a metadata field of Enums options
split_metadta_field = DataRowMetadataField(
    name="split", # specify the schema name
    value="train", # typed inputs

# Custom fields, must be created in UI prior to this
custome_metadta_field = DataRowMetadataField(
    name="my-custom-field",  # specify the schema name
    value="custom_field_value", # typed inputs

Upload Data Rows with metadata

dataset = client.create_dataset(name="Bulk import example")

data_row = {"row_data": "", "external_id": str(uuid4())}

# Option 1: Specify metadata with a list of DataRowMetadataField.
# This is the recommended option since it comes with validation for metadata fields.
data_row['metadata_fields'] = [tag_metadata_field, capture_datetime_field,  split_metadta_field]

# Option 2: Alternatively, you can specify the metadata fields with dictionary format 
# without declaring the DataRowMetadataField objects.
data_row['metadata_fields'] = [
  {"name": "tag", "value": "tag_string"},             
  {"name": "captureDateTime", "value": datetime.datetime.utcnow()}, 
  {"name": "split", "value": "train"}, 


task = dataset.create_data_rows([data_row])

Get metadata

data_row = next(dataset.data_rows())

Export metadata

# Export metadata from a dataset
data_rows = dataset.export_data_rows(include_metadata=True)

# Export metadata from a list of data row ids.
metadata_ontology = client.get_data_row_metadata_ontology()
metadata = metadata_ontology.bulk_export([data_row.uid])

Update or add metadata of existing Data Rows

Labelbox supports individual or bulk metadata upsert of Data Rows. Metadata will overwrite on a per-field basis.

# Construct an enum field
field = DataRowMetadataField(
    name="tag",  # specify the schema name
    value="updated", # typed inputs

# Completed object ready for import
metadata_payload = DataRowMetadata(
    data_row_id="DATAROW_ID",  # DataRow Id not ExternalId

# Provide a list of DataRowMetadata objects to upload

Delete metadata

# Specify the schemas to delete
schemas = [tag_schema, ...]

# Create a delete object
deletes = DeleteDataRowMetadata(
    fields=[s.uid for s in schemas]

mdo.bulk_delete([deletes]) # pass an array of deletes