HumanSignal / label-studio-sdk

Label Studio SDK
https://api.labelstud.io
77 stars 50 forks source link

POST annotation SDK shows validation error #234

Closed StickyDoughnut closed 1 week ago

StickyDoughnut commented 2 weeks ago

Hi!

I use labelstudio 1.12.1 and labelstudio sdk 1.0.0

I wanted to create a new annotation using the SDK. A call using the current python example does not work and leads to an validation error. Also it seems like the data structure of the examples and what is actually saved when creating an annotation manually is different.

The documentation says:

"values": {"rectanglelabels": {"0": "Airplane"}}

but labelstudio saves the values like this:

"rectanglelabels": [
              "Airplane"
            ]

When I call api with curl like this:

curl -X POST https://..../api/tasks/12/annotations \
     -H "Authorization: Token ..." \
     -H "Content-Type: application/json" \
     -d '{
  "result": [
        {
          "original_width": 762,
          "original_height": 504,
          "image_rotation": 0,
          "value": {
            "x": 75,
            "y": 15,
            "width": 12,
            "height": 21,
            "rotation": 0,
            "rectanglelabels": [
              "Airplane"
            ]
          },
          "from_name": "label",
          "to_name": "image",
          "type": "rectanglelabels"
        }
      ],
      "was_cancelled": false,
      "ground_truth": false
    }'

The annotation is created properly. Doing the same with the python SDK also creates the annotation properly but leads to a validation error:

x = ls.annotations.create(
    id=12,
    result=[
        {
          "original_width": 762,
          "original_height": 504,
          "image_rotation": 0,
          "value": {
            "x": 75,
            "y": 15,
            "width": 12,
            "height": 21,
            "rotation": 0,
            "rectanglelabels": [
              "Airplane"
            ]
          },
          "from_name": "label",
          "to_name": "image",
          "type": "rectanglelabels"
        }
      ],
    was_cancelled=False,
    ground_truth=True
)
x

It returns "value is not a valid dict".

ValidationError: 1 validation error for ParsingModel[Annotation]
__root__ -> result
  value is not a valid dict (type=type_error.dict)
File <command-2907982429628373>, line 1
----> 1 x = ls.annotations.create(
      2     id=12,
      3     result=[
      4         {
      5           "original_width": 762,
      6           "original_height": 504,
      7           "image_rotation": 0,
      8           "value": {
      9             "x": 75,
     10             "y": 15,
     11             "width": 12,
     12             "height": 21,
     13             "rotation": 0,
     14             "rectanglelabels": [
     15               "Airplane"
     16             ]
     17           },
     18           "from_name": "label",
     19           "to_name": "image",
     20           "type": "rectanglelabels"
     21         }
     22       ],
     23     was_cancelled=False,
     24     ground_truth=True
     25 )
     26 x
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-d28d80db-7fa5-42c7-a02d-6197d84b6649/lib/python3.11/site-packages/label_studio_sdk/annotations/client.py:376, in AnnotationsClient.create(self, id, result, task, project, completed_by, updated_by, was_cancelled, ground_truth, lead_time, request_options)
    359 _response = self._client_wrapper.httpx_client.request(
    360     f"api/tasks/{jsonable_encoder(id)}/annotations/",
    361     method="POST",
   (...)
    373     omit=OMIT,
    374 )
    375 if 200 <= _response.status_code < 300:
--> 376     return pydantic_v1.parse_obj_as(Annotation, _response.json())  # type: ignore
    377 try:
    378     _response_json = _response.json()
File /databricks/python/lib/python3.11/site-packages/pydantic/tools.py:38, in pydantic.tools.parse_obj_as()
File /databricks/python/lib/python3.11/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
niklub commented 2 weeks ago

Thanks for reporting this. Fixed in https://github.com/HumanSignal/label-studio-sdk/pull/236

niklub commented 2 weeks ago

Please try using the latest update, pip install label-studio-sdk>=1.0.1

sajarin commented 1 week ago

@StickyDoughnut thanks for reporting the initial bug, feel free to create a new issue or message me on Slack if the issue persists (and I'll reopen this issue for more discussion)