Skip to content

Commit e872e45

Browse files
Merge pull request #1599 from roboflow/fix/ocr-reposnses-parent-id
Retain empty 'parent_id' field in OCR response
2 parents 2df44d0 + 8c6a3d2 commit e872e45

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

inference/core/entities/responses/ocr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class OCRInferenceResponse(BaseModel):
2323
description="Metadata about input image dimensions", default=None
2424
)
2525
predictions: Optional[List[ObjectDetectionPrediction]] = Field(
26-
description="List of objects detected by OCR", default=None
26+
description="List of objects detected by OCR",
27+
default=None,
2728
)
2829
time: float = Field(
2930
description="The time in seconds it took to produce the inference including preprocessing."

inference/core/interfaces/http/http_api.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@
184184
)
185185
from inference.core.interfaces.http.middlewares.cors import PathAwareCORSMiddleware
186186
from inference.core.interfaces.http.middlewares.gzip import gzip_response_if_requested
187-
from inference.core.interfaces.http.orjson_utils import orjson_response
187+
from inference.core.interfaces.http.orjson_utils import (
188+
orjson_response,
189+
orjson_response_keeping_parent_id,
190+
)
188191
from inference.core.interfaces.stream_manager.api.entities import (
189192
CommandResponse,
190193
ConsumePipelineResponse,
@@ -1994,7 +1997,9 @@ def yolo_world_infer(
19941997

19951998
@app.post(
19961999
"/doctr/ocr",
1997-
response_model=OCRInferenceResponse | List[OCRInferenceResponse],
2000+
response_model=Union[
2001+
OCRInferenceResponse, List[OCRInferenceResponse]
2002+
],
19982003
summary="DocTR OCR response",
19992004
description="Run the DocTR OCR model to retrieve text in an image.",
20002005
)
@@ -2036,13 +2041,15 @@ def doctr_retrieve_text(
20362041
"authorizer"
20372042
]["lambda"]["actor"]
20382043
trackUsage(doctr_model_id, actor)
2039-
return orjson_response(response)
2044+
return orjson_response_keeping_parent_id(response)
20402045

20412046
if CORE_MODEL_EASYOCR_ENABLED:
20422047

20432048
@app.post(
20442049
"/easy_ocr/ocr",
2045-
response_model=OCRInferenceResponse | List[OCRInferenceResponse],
2050+
response_model=Union[
2051+
OCRInferenceResponse, List[OCRInferenceResponse]
2052+
],
20462053
summary="EasyOCR OCR response",
20472054
description="Run the EasyOCR model to retrieve text in an image.",
20482055
)
@@ -2084,7 +2091,7 @@ def easy_ocr_retrieve_text(
20842091
"authorizer"
20852092
]["lambda"]["actor"]
20862093
trackUsage(easy_ocr_model_id, actor)
2087-
return orjson_response(response)
2094+
return orjson_response_keeping_parent_id(response)
20882095

20892096
if CORE_MODEL_SAM_ENABLED:
20902097

@@ -2472,7 +2479,7 @@ def trocr_retrieve_text(
24722479
"authorizer"
24732480
]["lambda"]["actor"]
24742481
trackUsage(trocr_model_id, actor)
2475-
return orjson_response(response)
2482+
return orjson_response_keeping_parent_id(response)
24762483

24772484
if not (LAMBDA or GCP_SERVERLESS):
24782485

inference/core/interfaces/http/orjson_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ def orjson_response(
4141
return ORJSONResponseBytes(content=content)
4242

4343

44+
def orjson_response_keeping_parent_id(
45+
response: Union[List[InferenceResponse], InferenceResponse, BaseModel],
46+
) -> ORJSONResponseBytes:
47+
if isinstance(response, list):
48+
content = []
49+
for r in response:
50+
serialised = r.model_dump(by_alias=True, exclude_none=True)
51+
if "parent_id" not in serialised:
52+
serialised["parent_id"] = None
53+
content.append(serialised)
54+
else:
55+
content = response.model_dump(by_alias=True, exclude_none=True)
56+
if "parent_id" not in content:
57+
content["parent_id"] = None
58+
return ORJSONResponseBytes(content=content)
59+
60+
4461
@deprecated(
4562
reason="Function serialise_workflow_result(...) will be removed from `inference` end of Q1 2025. "
4663
"Workflows ecosystem shifted towards internal serialization - see Workflows docs: "

inference/core/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.57.1"
1+
__version__ = "0.57.2"
22

33

44
if __name__ == "__main__":

0 commit comments

Comments
 (0)