What This Helps With
Automate custom metadata uploads from CSV files to Visual Layer datasets with comprehensive validation, multiple field type support, and detailed error reporting.
This script demonstrates the complete Visual Layer custom metadata API workflow. You can modify the script to suit your specific needs and data formats.
Working with DICOM Medical Imaging Data?
For medical imaging workflows, check out our specialized DICOM converter and upload scripts that handle DICOM-specific field types, date/time formats, and medical metadata standards.
View Complete Script Code
Access the full custom metadata upload Python script with complete implementation, ready to copy and use.
Features
The script provides a comprehensive solution for CSV-based metadata uploads:- Multiple Field Types - Supports all six Visual Layer field types: string, float, datetime, enum, multi-enum, and link
- Comprehensive Validation - Validates all data before upload with detailed error messages showing row and column information
- Enum Limit Checking - Pre-upload validation ensures enum fields don’t exceed the 600 unique values limit
- Flexible Datetime Formats - Accepts multiple datetime formats and auto-converts to ISO 8601 UTC
- Clear Progress Tracking - Detailed status reporting throughout the upload workflow
- Auto-Detection - Automatically discovers enum options from your CSV data
How the Script Works
The script follows the complete Visual Layer custom metadata API workflow:- Export dataset - Calls Visual Layer API to get filename-to-media_id mapping
- Read and validate CSV - Validates all values match field type requirements before upload
- Check enum constraints - Verifies enum fields don’t exceed 600 unique values limit
- Create custom metadata fields - Creates each field in Visual Layer via API calls
- Upload metadata values - Uploads validated data for each field using proper JSON format
- Monitor upload progress - Tracks completion status and reports results
Prerequisites
Before using the script, ensure you have:- Visual Layer installation (cloud or on-premises)
- Cloud: Requires API key and secret (see Authentication Guide)
- On-Premises: No authentication needed
- Dataset in READY state in Visual Layer with images already uploaded
- Python environment with required packages
- CSV file formatted according to requirements below
Install Dependencies
PyJWT is required for cloud installations that use JWT authentication. On-premises installations without authentication can omit this package.
CSV Requirements
The CSV file must follow this structure to work with the script.Required Column
Your CSV must include afile_fullpath column that matches the format from Visual Layer’s export:
file_fullpath values are used to map your metadata to Visual Layer media_ids.
Optional Metadata Columns
You can include any number of metadata columns. Column names become field names in Visual Layer. Specify the type for each column using command-line arguments.How to Get file_fullpath Values
To get the correctfile_fullpath values for your dataset:
- Export your dataset from Visual Layer using the media_id export endpoint
- The export returns a CSV with
filenameandmedia_idcolumns - Use the
filenamevalues as yourfile_fullpathvalues in your metadata CSV
Supported Field Types
The script supports all six Visual Layer custom metadata field types.String (--string-fields)
Any text value.
patient_id, notes, description
Float (--float-fields)
Decimal numbers.
95.5, 0.98, 123.456
Example: score, confidence, temperature
Datetime (--datetime-fields)
Date and time values in UTC. The script accepts multiple formats and converts to ISO 8601 UTC:
Accepted formats:
2024-01-15T10:30:00Z(ISO 8601 UTC - preferred)2024-01-15(date only - converts to midnight UTC)2024-01-15 10:30:00(auto-converts to UTC)
YYYY-MM-DDTHH:MM:SSZ
created_at, updated_at, scan_date
Enum (--enum-fields)
Single-select categorical data with a maximum of 600 unique values per field.
status, category, priority
Multi-Enum (--multi-enum-fields)
Multi-select categorical data with a maximum of 600 unique values total across all lists.
Accepts two formats:
- Comma-separated:
"tag1, tag2, tag3" - JSON array:
["tag1", "tag2", "tag3"]
tags, labels, keywords
Link (--link-fields)
URL values that must include a protocol.
http:// or https://
Example: pdf_url, documentation_link, image_url
Usage
Save the script ascustom_metadata_upload_script.py and run with field type specifications.
Basic Example
Command-Line Parameters
| Parameter | Required | Description | Default |
|---|---|---|---|
--csv | ✅ | Path to CSV file with metadata | - |
--dataset-id | ✅ | Visual Layer dataset identifier | - |
--base-url | ⭕ | Visual Layer installation URL (use https://app.visual-layer.com for cloud) | http://localhost:8080 |
--api-key | ⭕ | API key for Visual Layer Cloud authentication | - |
--api-secret | ⭕ | API secret for Visual Layer Cloud authentication | - |
--string-fields | ⭕ | Space-separated list of string field names | - |
--float-fields | ⭕ | Space-separated list of float field names | - |
--datetime-fields | ⭕ | Space-separated list of datetime field names | - |
--enum-fields | ⭕ | Space-separated list of enum field names | - |
--multi-enum-fields | ⭕ | Space-separated list of multi-enum field names | - |
--link-fields | ⭕ | Space-separated list of link field names | - |
--batch-size | ⭕ | Records per upload batch | 50000 |
Cloud vs On-Premises:
- Cloud (
https://app.visual-layer.com): Requires--api-keyand--api-secretfor JWT authentication - On-Premises: No authentication needed, omit
--api-keyand--api-secretparameters
Example Use Cases
The following examples demonstrate common metadata upload scenarios.Medical Imaging
For on-premises installations, omit
--api-key and --api-secret and change --base-url to your local URL (e.g., http://localhost:8080).E-commerce Products
Document Management
Authentication (Cloud Only)
Visual Layer Cloud requires JWT authentication for all API requests. On-premises installations can skip this section.On-Premises Installations
If you’re using an on-premises deployment, you can skip authentication parameters. The script works without
--api-key and --api-secret arguments.Get Your API Credentials
To use the script with Visual Layer Cloud:- Obtain API Key and Secret - Follow the instructions in the Authentication Guide to retrieve your API credentials from Visual Layer Cloud
- Pass credentials to script - Use the
--api-keyand--api-secretparameters when running the script
Authentication Example
Validation and Error Handling
The script validates all data before uploading to catch issues early.Pre-Upload Validation Checks
The script performs these validations before any upload:- Float values are valid numbers
- Datetime values match accepted formats
- Enum fields don’t exceed 600 unique values
- Multi-enum fields don’t exceed 600 unique values total
- Multi-enum values are properly formatted (comma-separated or JSON array)
- Links include protocol (http:// or https://)
- File paths exist in Visual Layer dataset
Error Messages
The script provides clear error messages showing exactly what’s wrong:Script Output
The script provides detailed progress information throughout the workflow.Successful Upload Example
Tips and Best Practices
Follow these best practices for successful metadata uploads.Test with Sample Data First
Start with a small CSV (10-20 rows) to verify:- Field types are correct
- Data formats are valid
- Mappings work as expected
Use Datetime Flexibility
The script accepts multiple datetime formats:Multi-Enum Format Options
Both formats work equally well:Handle Empty Values
Empty values are automatically skipped. There is no need to remove rows with missing data.Use Descriptive Field Names
Column names become field names in Visual Layer. Make them descriptive and consistent:- ✅ Good:
patient_id,scan_date,confidence_score - ❌ Avoid:
field1,data,x
Troubleshooting
Common issues and solutions for metadata upload problems.”No field types specified”
Problem: Script returns error about missing field types. Solution: Add at least one--*-fields argument to specify which columns to upload and their types.
”Failed to export dataset”
Problem: Script cannot export dataset to get media_id mappings. Solution:- Check that the dataset ID is correct
- Verify the base URL is accessible
- Ensure Visual Layer is running
- Confirm the dataset is in READY state
”Validation failed with N errors”
Problem: CSV data doesn’t match specified field types. Solution:- Read error messages carefully (shows row and column)
- Fix data format issues in CSV
- Verify field type assignments match your data
- Check that datetime values use accepted formats
”Enum field exceeds 600 unique values limit”
Problem: Enum or multi-enum field has too many unique values. Solution:- Use
stringfield type instead ofenumormulti-enum - Reduce the number of unique values in your data
- Check unique count with:
cut -d',' -f2 file.csv | sort | uniq | wc -l
”Field already exists”
Problem: Script reports that a field was created previously. Solution: This is normal behavior, not an error. The script will skip creation and proceed with upload. This occurs when re-running the script or adding data to existing fields.Adding vs. Updating Metadata:
- Adding new fields: The script will create new fields if they don’t exist
- Updating existing fields: Currently updating existing metadata values is not supported. Contact Visual Layer for assistance with updating existing metadata values
Getting Help
For advanced use cases, custom metadata deletion, or technical support:Contact Visual Layer Support
Reach out to Visual Layer’s support team for assistance with complex metadata workflows, custom field requirements, or troubleshooting upload issues.
Expected Result in Visual Layer
After running the script successfully, your custom metadata fields will be visible in Visual Layer’s interface for each image.
Related Resources
- Custom Metadata API Guide - Manual API workflow and field type details
- DICOM Converter - Specialized workflow for medical imaging metadata
- Creating Datasets - Guide to creating Visual Layer datasets