PaddlePaddle/PaddleOCR · error · TypeError
Unsupported result type: {type(result_obj)}
Error message
Unsupported result type: {type(result_obj)} What it means
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.
Source
Thrown at deploy/ios_demo/scripts/ocr_reference_run.py:78
if hasattr(obj, "tolist"):
return obj.tolist()
if isinstance(obj, (list, tuple)):
return [_numpy_to_python(x) for x in obj]
if isinstance(obj, dict):
return {k: _numpy_to_python(v) for k, v in obj.items()}
elif isinstance(obj, (str, int, float, bool)):
return obj
return str(obj)
def _extract_items(result_obj: Any) -> List[Dict[str, Any]]:
"""Build a list of {polygon, text, score} from an OCR pipeline result."""
if isinstance(result_obj, dict):
res = result_obj
elif hasattr(result_obj, "__getitem__"):
res = dict(result_obj)
else:
raise TypeError(f"Unsupported result type: {type(result_obj)}")
return _extract_items_from_res_dict(res)
def _extract_items_from_res_dict(res: Dict[str, Any]) -> List[Dict[str, Any]]:
texts = res.get("rec_texts") or []
scores = res.get("rec_scores")
polys = res.get("rec_polys") or res.get("dt_polys") or []
if hasattr(scores, "tolist"):
scores = scores.tolist()
items: List[Dict[str, Any]] = []
n = min(len(texts), len(polys))
for i in range(n):
poly = polys[i]
if hasattr(poly, "tolist"):
poly = poly.tolist()
score = float(scores[i]) if scores is not None and i < len(scores) else None
items.append(
{View on GitHub (pinned to 2661c7c0ef)
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).
Example fix
// before
result = pipeline.predict(img)
items = _extract_items(result) # TypeError on newer PaddleOCR
// after
result = pipeline.predict(img)
first = result[0] if isinstance(result, list) else result
res = first.get("res", first) if isinstance(first, dict) else dict(first)
items = _extract_items(res) Defensive patterns
Strategy: type-guard
Validate before calling
def is_extractable_result(result_obj) -> bool:
return isinstance(result_obj, dict) or hasattr(result_obj, "__getitem__") Type guard
from typing import Any
def is_ocr_res_dict(obj: Any) -> bool:
"""True when obj can be treated as a mapping with OCR result keys."""
if isinstance(obj, list) and len(obj) == 1:
obj = obj[0]
return isinstance(obj, dict) and any(
k in obj for k in ("rec_texts", "rec_polys", "dt_polys")
) Try / catch
try:
items = _extract_items(result)
except TypeError:
# common PaddleOCR shape: list with a single result element
first = result[0] if isinstance(result, list) else result
items = _extract_items(dict(first.get("res", first))) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Detection model session is not initialized.
- Unexpected det output dims: [${dims.join(", ")}]
- Unexpected det output dims: [${od.join(", ")}]
- Detection batch output N=${String(nOut)} does not match inpu
- RecResizeImg.image_shape is required in rec inference.yml
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/4d171ff0527c5689.
Report an issue: GitHub.