docling-project/docling · error · TypeError
Expected scalar-like tensor with one element, got shape={tup
Error message
Expected scalar-like tensor with one element, got shape={tuple(value.shape)} What it means
Raised as TypeError by HfVisionModelMixin._as_float when a torch.Tensor score does not contain exactly one element (numel() != 1). Same contract as the ndarray variant: score values must be scalar-like tensors before conversion to float.
Source
Thrown at docling/models/inference_engines/common/hf_vision_base.py:128
return self._id_to_label
@staticmethod
def _as_float(value: Any) -> float:
if isinstance(value, Real):
return float(value)
if isinstance(value, np.ndarray):
if value.size != 1:
raise TypeError(
f"Expected scalar-like ndarray with size 1, got shape={value.shape}"
)
return float(value.reshape(-1)[0])
import torch
if isinstance(value, torch.Tensor):
if value.numel() != 1:
raise TypeError(
f"Expected scalar-like tensor with one element, got shape={tuple(value.shape)}"
)
return float(value.item())
raise TypeError(f"Unsupported score value type: {type(value)!r}")
@staticmethod
def _as_int(value: Any) -> int:
if isinstance(value, Integral):
return int(value)
if isinstance(value, np.ndarray):
if value.size != 1:
raise TypeError(
f"Expected scalar-like ndarray with size 1, got shape={value.shape}"
)
return int(value.reshape(-1)[0])
View on GitHub (pinned to 61d76f1ff3)
Solutions
- Reduce the tensor to one element first: scores[i, labels[i]] or scores.max().
- Call .item() yourself when you know it is scalar, or squeeze and assert numel()==1 in your post-processor.
- Align your custom head output with the expected [N] scalar-score layout.
Example fix
# before conf = model._as_float(scores_tensor[i]) # shape (num_classes,) -> TypeError # after conf = model._as_float(scores_tensor[i, labels[i]]) # numel()==1
Defensive patterns
Strategy: type-guard
Validate before calling
if torch.is_tensor(score):
assert score.numel() == 1, f'score tensor must have 1 element, got {tuple(score.shape)}' Type guard
import torch
def is_scalar_tensor(value) -> bool:
return not torch.is_tensor(value) or value.numel() == 1 Prevention
- Index per detection (scores[i, labels[i]]) before conversion.
- Prefer .item() where scalarity is guaranteed by construction.
- Keep a single post-processing path shared by all model families.
When it happens
Trigger: Passing a multi-element torch tensor (e.g. a [num_classes] score row or [1, num_boxes] slice) to _as_float.
Common situations: Feeding raw model logits rows into result construction without argmax/max reduction; batch post-processing that forgot to index per detection.
Related errors
- Expected scalar-like ndarray with size 1, got shape={value.s
- Unsupported score value type: {type(value)!r}
- Unsupported label value type: {type(value)!r}
- Unsupported input type: {type(self.path_or_stream)}
- Unexpected: {type(self.path_or_stream)=}
AI-assisted analysis of docling-project/docling@61d76f1ff3 (2026-08-14).
Data as JSON: /api/errors/e665d27113ce054b.
Report an issue: GitHub.