import labelboxfrom labelbox.schema.data_row_metadata import DataRowMetadataKindclient = labelbox.Client(api_key="LABELBOX_API_KEY")metadata_ontology = client.get_data_row_metadata_ontology()# create a custom metadata schema (set the kind value to a supported data type)metadata_schema = metadata_ontology.create_schema(name="metadata_name", kind=DataRowMetadataKind.string)# get the schema idschema_id = metadata_schema.uid
# you can look up a schema by name.metadata_schema = metadata_ontology.get_by_name("tag")metadata_schema = metadata_ontology.get_by_name("enum_metadata_name")# check the schemaprint(metadata_schema)schema_id = metadata_schema.uid
## Fetch metadata schema ontology. A Labelbox workspace has a single metadata ontology.metadata_ontology = client.get_data_row_metadata_ontology()# List all available fieldsmetadata_ontology.fields
export_params= { "performance_details": True, "label_details": True, "metadata_fields": True}export_task = dataset.export(params=export_params)export_task.wait_till_done()# Stream the export using a callback functiondef json_stream_handler(output: labelbox.BufferedJsonConverterOutput):print(output.json)export_task.get_buffered_stream(stream_type=labelbox.StreamType.RESULT).start(stream_handler=json_stream_handler)# Collect all exported data into a listexport_json = [data_row.json for data_row in export_task.get_buffered_stream()]
You can bulk export metadata by data row with the SDK.
Copy
Ask AI
data_row_ids = ['<data_row_id>']global_keys = ['<global_key>']#The data row identifiers methods (lb.DataRowIds and lb.GlobalKeys) validate whether the provided ID is a global key or a data row ID.#Additionally, they ensure that all IDs from the list provided are uniquedatarow_identifiers = lb.DataRowIds(data_row_ids)global_key_identifiers = lb.GlobalKeys(global_keys)# Use one of the identifiersmdo.bulk_export(data_row_ids=global_key_identifiers)# mdo.bulk_export(data_row_ids=datarow_identifiers)
global_key = '<global_key>'schema_ids_to_delete =['<metadata_schema_id>']data_row_id = '<data_row_id>'deletions = [ lb.DeleteDataRowMetadata(data_row_id=lb.GlobalKey(global_key), fields=schema_ids_to_delete) ]# Delete the specified metadata on the data rowmdo.bulk_delete(deletes=deletions)
Labelbox supports individual or bulk metadata upsert of data rows. Metadata overwrites occur on a per-field basis.
Copy
Ask AI
tag_schema = metadata_ontology.get_by_name("tag")# Construct a string fieldfield = DataRowMetadataField(schema_id=tag_schema.uid, # specify the schema idvalue="updated", # typed inputs)# Completed object ready for importmetadata_payload = DataRowMetadata(global_key="<global key>", # optionally, set the argument to data_row_id to use a data row IDfields=[field])# Provide a list of DataRowMetadata objects to uploadmetadata_ontology.bulk_upsert([metadata_payload])
You can update any custom metadata schema’s name. However, the type cannot be modified. You also cannot modify the names of reserved fields.
Copy
Ask AI
# update a metadata schema's namemetadata_schema = metadata_ontology.update_schema(name="metadata_name", new_name="metadata_name_updated")# Enum metadata schema is a bit different since it contains options.# create an Enum metadata with optionsenum_schema = metadata_ontology.create_schema(name="enum_metadata_name", kind=DataRowMetadataKind.enum, options=["option 1", "option 2"])# update an Enum metadata schema's name, similar to other metadata schema typesenum_schema = metadata_ontology.update_schema(name="enum_metadata_name", new_name="enum_metadata_name_updated")# update an Enum metadata schema option's name, this only applies to Enum metadata schema.enum_schema = metadata_ontology.update_enum_option(name="enum_metadata_name_option_updated", option="option 1", new_option="option 3")