Label Studio SDK
The `label-studio-sdk` is the official Python client library for interacting with the Label Studio API. It provides programmatic access to manage projects, tasks, annotations, import/export data, and automate various data labeling workflows. It is currently at version 2.0.19 and maintains a regular release cadence with frequent updates adding new features and API endpoint support.
Warnings
- breaking The SDK underwent a significant overhaul from v1.x to v2.x, introducing numerous breaking changes in client initialization, method names, object structures, and pagination handling. Code written for v1.x will not work with v2.x.
- breaking Starting from v2.0.13, the `project` argument became a required parameter for all Import and Export Storage list endpoints (e.g., S3, Google Cloud Storage, Azure Blob Storage).
- gotcha Some advanced features and API endpoints, particularly those related to Finite State Management (FSM) or Workspace management, are exclusive to Label Studio Enterprise (LSE). Attempts to use them with the Community Edition will result in API errors.
- gotcha For large datasets, methods that retrieve lists of resources (e.g., tasks, annotations, members) may require explicit pagination to fetch all items. The SDK provides mechanisms to iterate through pages.
Install
-
pip install label-studio-sdk
Imports
- Client
from label_studio_sdk import Client
- Project
from label_studio_sdk.objects import Project
Quickstart
import os
from label_studio_sdk import Client
# Set your Label Studio URL and API Key
# It's recommended to set these as environment variables
LABEL_STUDIO_URL = os.environ.get('LABEL_STUDIO_URL', 'http://localhost:8080')
LABEL_STUDIO_API_KEY = os.environ.get('LABEL_STUDIO_API_KEY', 'YOUR_API_KEY_HERE') # Replace with your actual API key if not in env
if not LABEL_STUDIO_API_KEY or LABEL_STUDIO_API_KEY == 'YOUR_API_KEY_HERE':
print("Warning: LABEL_STUDIO_API_KEY not set. Please set it as an environment variable or replace 'YOUR_API_KEY_HERE'.")
exit()
try:
# Initialize the Label Studio client
ls = Client(url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
# Test connection and fetch projects
projects = ls.get_projects()
print(f"Successfully connected to Label Studio at {LABEL_STUDIO_URL}.")
print(f"Found {len(projects)} projects:")
for p in projects:
print(f" - Project ID: {p.id}, Title: {p.title}")
except Exception as e:
print(f"Error connecting to Label Studio or fetching projects: {e}")
print("Please ensure LABEL_STUDIO_URL and LABEL_STUDIO_API_KEY are correct and Label Studio is running.")