{"record":{"id":"4d171ff0527c5689","repo":"PaddlePaddle/PaddleOCR","slug":"unsupported-result-type-type-result-obj","errorCode":null,"errorMessage":"Unsupported result type: {type(result_obj)}","messagePattern":"Unsupported result type: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"deploy/ios_demo/scripts/ocr_reference_run.py","lineNumber":78,"sourceCode":"    if hasattr(obj, \"tolist\"):\n        return obj.tolist()\n    if isinstance(obj, (list, tuple)):\n        return [_numpy_to_python(x) for x in obj]\n    if isinstance(obj, dict):\n        return {k: _numpy_to_python(v) for k, v in obj.items()}\n    elif isinstance(obj, (str, int, float, bool)):\n        return obj\n    return str(obj)\n\n\ndef _extract_items(result_obj: Any) -> List[Dict[str, Any]]:\n    \"\"\"Build a list of {polygon, text, score} from an OCR pipeline result.\"\"\"\n    if isinstance(result_obj, dict):\n        res = result_obj\n    elif hasattr(result_obj, \"__getitem__\"):\n        res = dict(result_obj)\n    else:\n        raise TypeError(f\"Unsupported result type: {type(result_obj)}\")\n    return _extract_items_from_res_dict(res)\n\n\ndef _extract_items_from_res_dict(res: Dict[str, Any]) -> List[Dict[str, Any]]:\n    texts = res.get(\"rec_texts\") or []\n    scores = res.get(\"rec_scores\")\n    polys = res.get(\"rec_polys\") or res.get(\"dt_polys\") or []\n    if hasattr(scores, \"tolist\"):\n        scores = scores.tolist()\n    items: List[Dict[str, Any]] = []\n    n = min(len(texts), len(polys))\n    for i in range(n):\n        poly = polys[i]\n        if hasattr(poly, \"tolist\"):\n            poly = poly.tolist()\n        score = float(scores[i]) if scores is not None and i < len(scores) else None\n        items.append(\n            {","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/ios_demo/scripts/ocr_reference_run.py#L60-L96","documentation":"TypeError from _extract_items in ocr_reference_run.py when the OCR pipeline result is neither a dict nor an object supporting __getitem__. The reference script expects a PaddleOCR result that can be treated as a mapping containing rec_texts/rec_scores/rec_polys (or dt_polys); any other shape is rejected before field access.","triggerScenarios":"A PaddleOCR version whose predict() returns a bare list/tuple of result objects or a custom object without __getitem__; passing the wrong pipeline stage output (e.g. a detector-only result with dt_polys but consumed as a dict elsewhere); mocking the pipeline with a plain string.","commonSituations":"Upgrading PaddleOCR across major versions where the result API changed from list-of-lists to named dicts and back; switching between PP-OCRv3/v4 pipelines with different return conventions.","solutions":["Inspect `type(result)` and, if it is a list, take the first element / re-wrap: many versions return `[{'rec_texts': ...}]`.","Pin the PaddleOCR version the reference script was written against (check deploy/ios_demo docs/requirements).","Convert the result to a dict before calling: `_extract_items(dict(result[0]))` or use result[0].json['res'] style access on newer APIs.","Update _extract_items_from_res_dict if your version uses different keys (e.g. rec_polys vs dt_polys)."],"exampleFix":"// before\nresult = pipeline.predict(img)\nitems = _extract_items(result)  # TypeError on newer PaddleOCR\n\n// after\nresult = pipeline.predict(img)\nfirst = result[0] if isinstance(result, list) else result\nres = first.get(\"res\", first) if isinstance(first, dict) else dict(first)\nitems = _extract_items(res)","handlingStrategy":"type-guard","validationCode":"def is_extractable_result(result_obj) -> bool:\n    return isinstance(result_obj, dict) or hasattr(result_obj, \"__getitem__\")","typeGuard":"from typing import Any\n\ndef is_ocr_res_dict(obj: Any) -> bool:\n    \"\"\"True when obj can be treated as a mapping with OCR result keys.\"\"\"\n    if isinstance(obj, list) and len(obj) == 1:\n        obj = obj[0]\n    return isinstance(obj, dict) and any(\n        k in obj for k in (\"rec_texts\", \"rec_polys\", \"dt_polys\")\n    )","tryCatchPattern":"try:\n    items = _extract_items(result)\nexcept TypeError:\n    # common PaddleOCR shape: list with a single result element\n    first = result[0] if isinstance(result, list) else result\n    items = _extract_items(dict(first.get(\"res\", first)))","preventionTips":["Pin the PaddleOCR version used for reference generation.","Wrap pipeline.predict() results in a normalization function that unwraps lists and .res before use.","Add a unit test over a saved sample result so version upgrades surface shape changes early."],"tags":["python","paddleocr","type-error","version-compat"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}