Missing Result ID in Annotations created with the sdk

I am using the labelstudio sdk to create annotations. For example like this:

label_studio_result_list = [{
'type': 'rectanglelabels', 
'value': {
'x': x, 'y': y, 'width': w, 'height': h, 
'rotation': 0, 
'rectanglelabels': [
  result.names[result.boxes.cls.cpu().numpy()[idx]]]}, 
 'to_name': 'image', 'from_name': 'label',
 'origin': 'manual', 
 'original_width': result.orig_img.shape[1], 
 'original_height': result.orig_img.shape[0]
}]

ls_result = {"result": label_studio_result_list, 'completed_by': 2}
project.create_annotation(task_id, **ls_result)

The annotations are created without a problem. But I noticed the result id is missing in the response.

However in the UI it shows me the id. When clicking update on the task. I see that it sends a patch request to the backend with the id shown in the UI but only if I change the position of the bounding box. I need the id’s in order to make sense of relations that I add manually.

How can I solve this? Going to every task that is missing an id and clicking Update is too time consuming for me.

You might be able to add an id to each of the annotations using a function to generate a unique id, here’s a rough example below:

import uuid
from label_studio_sdk import Client

# Connect to Label Studio
LABEL_STUDIO_URL = 'http://localhost:8080'
API_KEY = 'your_api_key'
ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)

# Function to create a unique ID
def generate_unique_id():
    return str(uuid.uuid4())

# Create annotations with unique IDs
label_studio_result_list = [{
    'id': generate_unique_id(),  # Generate a unique ID for each result
    'type': 'rectanglelabels',
    'value': {
        'x': x, 'y': y, 'width': w, 'height': h,
        'rotation': 0,
        'rectanglelabels': [result.names[result.boxes.cls.cpu().numpy()[idx]]]
    },
    'to_name': 'image', 'from_name': 'label',
    'origin': 'manual',
    'original_width': result.orig_img.shape[1],
    'original_height': result.orig_img.shape[0]
}]

ls_result = {"result": label_studio_result_list, 'completed_by': 2}
project.create_annotation(task_id, **ls_result)

# Function to update annotations programmatically
def update_annotations(task_id):
    task = project.get_task(task_id)
    annotations = task['annotations']
    for annotation in annotations:
        annotation_id = annotation['id']
        project.update_annotation(annotation_id, annotation)

# Update annotations for the task
update_annotations(task_id)

The generate_unique_id function creates a unique ID for each annotation result.
When creating the annotations, the generated unique ID is included in each result.
The update_annotations function retrieves the task, iterates through its annotations, and updates each annotation using the SDK. This triggers the backend to process the annotations and generate the necessary IDs.

Let me know if this helps!

Thank you that helps. I made it work like you suggested.