Skip to content

Conversation

@tonylampada
Copy link
Contributor

@tonylampada tonylampada commented Dec 18, 2025

Description

A frequent IndexError: boolean index did not match indexed array was occurring during workflow execution, specifically in steps following a model prediction (like detections_filter).

thread

CleanShot 2025-12-18 at 11 07 40

Original Stacktrace:

File "/Users/tony/work/roboflow/inference/inference/core/workflows/core_steps/common/query_language/operations/detections/base.py", line 100, in filter_detections
    return detections[result]
           ~~~~~~~~~~^^^^^^^^
File "/Users/tony/work/roboflow/inference/.venv/lib/python3.12/site-packages/supervision/detection/core.py", line 2023, in __getitem__
    data=get_data_item(self.data, index),
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tony/work/roboflow/inference/.venv/lib/python3.12/site-packages/supervision/detection/utils/internal.py", line 297, in get_data_item
    subset_data[key] = value[index]
                       ~~~~~^^^^^^^
IndexError: boolean index did not match indexed array along axis 0; size of axis is 3 but size of corresponding boolean axis is 2

Root Cause

The supervision library's from_inference() method, which converts raw model outputs into sv.Detections objects, performs internal validation. For instance, it silently discards instance segmentation predictions that have a points array with fewer than 3 points, as they cannot form a valid polygon mask.

However, several components in the inference workflow engine were generating auxiliary metadata arrays (such as detection_id, parent_id, and image_dimensions) using the length of the original raw prediction list rather than the length of the resulting sv.Detections object.

This created malformed sv.Detections objects where:

  • The core arrays (e.g., xyxy, confidence) had length N.
  • The .data dictionary arrays (e.g., detection_id) had length M (where M > N).

When a subsequent workflow step (like a filter) tried to index into these detections using a boolean mask of length N, NumPy/Supervision threw an IndexError because the mask length did not match the .data array length.

Reasoning and Solution

To maintain consistency, the workflow engine must ensure that any metadata added to sv.Detections.data aligns perfectly with the detections that supervision actually accepted.

Changes:

  1. Introduced Helper Utility: Added filter_relevant_predictions(predictions: List[dict]) in inference/core/workflows/core_steps/common/utils.py. This helper replicates the filtering logic found in supervision.Detections.from_inference.
  2. Synchronized Metadata Extraction:
    • Updated convert_inference_detections_batch_to_sv_detections to filter the raw predictions list if a length mismatch is detected after conversion.
    • Updated post_process_ocr_result to ensure OCR metadata stays in sync with accepted detections.
    • Updated deserialize_detections_kind and its associated helpers in deserializers.py to ensure detections loaded from runtime inputs are correctly aligned with their metadata.

This ensures that sv.Detections objects produced by workflows are always valid and safe to filter, sort, or transform in subsequent steps.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How has this change been tested, please provide a testcase or example of how you tested the change?

  • Automated test reproduces bug, and then new code fixes it
  • Tested locally with same workflow that broke in prod

Any specific deployment considerations

need new release

Fixes IndexError when filtering detections by ensuring metadata arrays match the actual detections that supervision accepts. The supervision library silently filters invalid predictions (e.g., instance segmentation masks with < 3 points), but workflow code was creating metadata based on raw predictions, causing length mismatches.

Added filter_relevant_predictions() helper to replicate supervision's filtering logic and updated metadata extraction in deserialize_detections_kind(), convert_inference_detections_batch_to_sv_detections(), and post_process_ocr_result().

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@tonylampada tonylampada merged commit ccd55b5 into main Dec 19, 2025
51 checks passed
@tonylampada tonylampada deleted the tony/fix-detections-mismatch branch December 19, 2025 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants