label-studio-sdk icon indicating copy to clipboard operation
label-studio-sdk copied to clipboard

fix: Updates type for `completed_by` field

Open Burhan-Q opened this issue 10 months ago • 2 comments

Problem

Annotation type field is currently only valid for int types, however API returns dict for completed_by field. Dictionary returned by API can't be used directly with Annotation object.

label-studio-sdk==1.0.10
pydantic==2.10.6

Changes

  • Added import for UserSimple model.
  • Updated type annotation to use typing.Union with int and UserSimple

Impact

  • Now direct output from API can be accepted by Annotation type.
  • Maintains compatibility with accepting int type as well.

Checked with

Simple test to validate that dict and int values are accepted by Annotation type for completed_by field.


from label_studio_sdk.client import LabelStudio
from label_studio_sdk.types import Annotation

client = LabelStudio()

annotations = client.annotations.get(id=12345)
first = next(annotations)

isinstance(first.get("completed_by"), dict)
>>> True

as_int = {
    k: v for k,v in first.items() if k != "completed_by"
} | {"completed_by": 09876}

annotation_01 = Annotation(**first)
annotation_02 = Annotation(**as_int)

isinstance(annotation_01.completed_by, UserSimple)
>>> True

isinstance(annotation_02.completed_by, UserSimple)
>>> True

[!NOTE] It appears that these might be generated from API documentation, which might mean this change might not integrate permanently if merged. Unclear to me at the moment if there is a better way to integrate a change like this into the current workflow, but it seemed like it was worthwhile to open a PR as a proposal for a QOL improvement.

Burhan-Q avatar Feb 20 '25 19:02 Burhan-Q

Hi Burhan, Samir here. Thanks for your PR. Engineering has evaluated your proposal and agrees with the idea. However, as you might have suspected, this is an auto-generated class definition, so we need to make the change internally elsewhere in order to effect the change that you proposed. I have created a Jira ticket referencing your PR and very good change notes. Max will likely comment here once the work has been completed as well as actioning on the PR itself. Of course, you're likely to see the update only in a subsequent version of the SDK, so please use your local modified copy for now. Thanks for your submission, it's a great idea and was agreed upon immediately on our side.

smohan123 avatar Feb 27 '25 02:02 smohan123

@smohan123 thanks! I did open a PR for the generation of the API/SDK https://github.com/HumanSignal/label-studio-client-generator/pull/64 but it only reflects my best guess of what's needed to make it work.

Burhan-Q avatar Feb 27 '25 16:02 Burhan-Q

Thank you for you PR! The main PR was merged, so your update will appear a bit later in the SDK automatically!

makseq avatar Mar 31 '25 21:03 makseq