Problems posting model results via the /predict endpoint from a flask based model in interactive mode

I would be really appreciative of any help how to format my models output data.
I get the following output from label studios instance: [ml.models::_get_predictions_from_ml_backend::300] [ERROR] ML backend returns an incorrect response, it must be a dict: {‘model_version’: ‘v1.0’, ‘predictions’: [{‘result’: [{‘from_name’: ‘Was war der ursprüngliche Auftrag?’, ‘id’: ‘75aa891b-cc29-4d30-aafe-45b02c413077’, ‘origin’: ‘prediction’, ‘to_name’: ‘text’, ‘type’: ‘labels’, ‘value’: {‘end’: ‘/p[1]/text()[1]’, ‘endOffset’: 337, ‘labels’: [‘Ursprungsprojekt’], ‘start’: ‘/p[1]/text()[1]’, ‘startOffset’: 331, ‘text’: ‘A21-39’}}], ‘score’: 1.0, ‘task’: 3879}]}

I can upload predictions via the gui and that creates this kind of task source: “predictions”: [
{
“id”: 11,
“result”: [
{
“id”: “PgON9_6f9900”,
“type”: “labels”,
“value”: {
“text”: “A21-39”,
“labels”: [
“Ursprungsprojekt”
],
“endOffset”: 30,
“startOffset”: 24,
“globalOffsets”: {
“end”: 30,
“start”: 24
}
},
“origin”: “prediction”,
“to_name”: “text”,
“from_name”: “Was war der ursprüngliche Auftrag?”
}
]
I just do not get how to adjust my model’s output accordingly.

The full task source is:
{
“id”: 3880,
“data”: {
“html”: “

Addendum zum Auftrag A21-39 is part of the project documentation.


},
“annotations”: ,
“predictions”: [
{
“id”: 11,
“result”: [
{
“id”: “PgON9_6f9900”,
“type”: “labels”,
“value”: {
“text”: “A21-39”,
“labels”: [
“Ursprungsprojekt”
],
“endOffset”: 30,
“startOffset”: 24,
“globalOffsets”: {
“end”: 30,
“start”: 24
}
},
“origin”: “prediction”,
“to_name”: “text”,
“from_name”: “Was war der ursprüngliche Auftrag?”
}
],
“model_version”: “v1.0”,
“created_ago”: “47 minutes”,
“score”: null,
“cluster”: null,
“neighbors”: null,
“mislabeling”: 0,
“created_at”: “2025-01-14T19:27:39.001665Z”,
“updated_at”: “2025-01-14T19:27:39.001671Z”,
“model”: null,
“model_run”: null,
“task”: 3880,
“project”: 1
}
]
}

okay I understood from the repository that this checked:
if not (isinstance(ml_api_result.response, dict) and ‘results’ in ml_api_result.response):
logger.info(f’ML backend returns an incorrect response, it must be a dict: {ml_api_result.response}')
result[‘errors’] = [
'Incorrect response from ML service: ’ ‘ML backend returns an incorrect response, it must be a dict.’
]
return result

So I was puzzled because my data was indeed a dictionary…the code found out that it did not include “results”, so it returned that it wasn’t a dictionary even though it was. I did not find a hint in the documentation that “results” was needed. It would be great to have an example in the documentation (if there is none and I did not just miss it)

Thank you so much, your post leads me into tehe right path. Just in case someone else is in here i will a post i made in github that’s more specific.

Blockquote
So after cracking my head out against the “Json format for preannotations” triying to find the way for my custom flask backend to get the right dict format and having the revisit the Label Studio code, i get to this json format that is NO way near the format shown in the documentation.

This was mostly because of a block of code that keept misleading my efforts:

if not (isinstance(ml_api_result.response, dict) and 'results' in ml_api_result.response): logger.info(f'ML backend returns an incorrect response, it must be a dict: {ml_api_result.response}') result['errors'] = [ 'Incorrect response from ML service: ' 'ML backend returns an incorrect response, it must be a dict.' ] return result

Its says that the correct way of interact whit LB frontend is by a “results” key, which contains 2 keys: “model_version” and “result” (in singular). Beeing the last key an array that contains each prediction.

"result": [ { "id": "{id_counter}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } }, { "id": "{id_counter + 1}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } } ]

### Here is the full json format that worked for me:

{ "data": { "image": "image_path" }, "results": [ { "model_version": "first", "result": [ { "id": "{id_counter}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } }, { "id": "{id_counter + 1}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } } ] } ] }

1 Like

So after cracking my head out against the “Json format for preannotations” triying to find the way for my custom flask backend to get the right dict format and having the revisit the Label Studio code, i get to this json format that is NO way near the format shown in the documentation.

This was mostly because of a block of code that keept misleading my efforts:

if not (isinstance(ml_api_result.response, dict) and 'results' in ml_api_result.response): logger.info(f'ML backend returns an incorrect response, it must be a dict: {ml_api_result.response}') result['errors'] = [ 'Incorrect response from ML service: ' 'ML backend returns an incorrect response, it must be a dict.' ] return result

Its says that the correct way of interact whit LB frontend is by a “results” key, which contains 2 keys: “model_version” and “result” (in singular). Beeing the last key an array that contains each prediction.

"result": [ { "id": "{id_counter}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } }, { "id": "{id_counter + 1}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } } ]

### Here is the full json format that worked for me:

{ "data": { "image": "image_path" }, "results": [ { "model_version": "first", "result": [ { "id": "{id_counter}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } }, { "id": "{id_counter + 1}", "model_version": "1.0", "type": "rectanglelabels", "from_name": "label", "to_name": "image", "original_width": "original_width", "original_height": "original_height", "image_rotation": 0, "value": { "rotation": 0, "x": "x0", "y": "y0", "width": "w", "height": "h", "rectanglelabels": "[label_name]" } } ] } ] }

You saved me a lot time. nothing on labelstudio doc could have helped me