roboflow/supervision · error · ValueError
Invalid metric type
Error message
Invalid metric type
What it means
Raised by get_size_category() in metrics/utils/object_size.py when the metric_target argument is not one of MetricTarget.BOXES, MASKS, or ORIENTED_BOUNDING_BOXES. The function dispatches on the enum to pick the right size-category computation (bbox area, mask pixel count, or OBB shoelace area). This is the final fall-through for an unknown enum value.
Source
Thrown at src/supervision/metrics/utils/object_size.py:87
... [0, 0, 10, 10], # 100 (Small)
... [0, 0, 50, 50], # 2500 (Medium)
... [0, 0, 100, 100] # 10000 (Large)
... ])
>>> get_object_size_category(xyxy, MetricTarget.BOXES)
array([1, 2, 3])
```
"""
if metric_target == MetricTarget.BOXES:
bbox_data = cast(npt.NDArray[np.number], data)
return get_bbox_size_category(bbox_data)
if metric_target == MetricTarget.MASKS:
mask_data = cast(npt.NDArray[np.bool_], data)
return get_mask_size_category(mask_data)
if metric_target == MetricTarget.ORIENTED_BOUNDING_BOXES:
obb_data = cast(npt.NDArray[np.number], data)
return get_obb_size_category(obb_data)
raise ValueError("Invalid metric type")
def get_bbox_size_category(xyxy: npt.NDArray[np.number]) -> npt.NDArray[np.int_]:
"""
Get the size category of a bounding boxes array.
Args:
xyxy: The bounding boxes array shaped (N, 4).
Returns:
The size category of each bounding box, matching
the enum values of ObjectSizeCategory. Shaped (N,).
Example:
```pycon
>>> import numpy as np
>>> from supervision.metrics.utils.object_size import get_bbox_size_category
>>> xyxy = np.array([View on GitHub (pinned to 7f254d9784)
Solutions
- Convert config values to the enum: MetricTarget(config_value) wrapped in try/except, or map strings explicitly
- Check the enum members supported in your supervision version before calling
Example fix
# before
cats = get_size_category(data, metric_target="masks") # str not handled
# after
from supervision.metrics.metric_target import MetricTarget
cats = get_size_category(data, MetricTarget("masks")) Defensive patterns
Strategy: type-guard
Validate before calling
from supervision.metrics.metric_target import MetricTarget
if not isinstance(metric_target, MetricTarget):
metric_target = MetricTarget(metric_target) # raises cleanly on bad values Type guard
from supervision.metrics.metric_target import MetricTarget
def is_valid_metric_target(value: object) -> bool:
"""True when value is a MetricTarget enum member."""
return isinstance(value, MetricTarget) Try / catch
try:
cats = get_size_category(data, metric_target)
except ValueError as e:
if 'Invalid metric type' in str(e):
cats = get_size_category(data, MetricTarget.BOXES)
else:
raise Prevention
- Normalize config strings to MetricTarget at the boundary: MetricTarget(str(cfg['target']).lower())
- Log the resolved enum member once at startup to catch bad config early
When it happens
Trigger: Calling get_size_category(data, metric_target) with a value outside the handled MetricTarget members (e.g. a raw int/string, or a newly added enum member like class-agnostic variants).
Common situations: Passing metric_target loaded from a YAML/JSON config without converting to the enum; forward-compatibility mismatch when supervision adds a new MetricTarget that this helper does not yet support; passing None.
Related errors
- Invalid metric target: {self._metric_target}
- Invalid metric target: {self._metric_target}
- Detection area metadata must be shaped (N,) and aligned with
- Invalid vlm value: {vlm}. Must be one of {[e.value for e in
- Invalid value type: {type(value)}. Must be an instance of {c
AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15).
Data as JSON: /api/errors/6c165a6d4c5c0e87.
Report an issue: GitHub.