opendatalab/MinerU · error · ValueError

images_mfd_res and images must have the same length.

Error message

images_mfd_res and images must have the same length.

What it means

ValueError raised by batch_predict in the PP-FormulaNet-Plus formula recognition predictor when images_mfd_res and images have different lengths. The method zips the per-image formula-detection results with the page images, so a mismatch means detection results and pages describe different documents and processing would silently skip or mispair pages.

Source

Thrown at mineru/model/mfr/pp_formulanet_plus_m/predict_formula.py:144

        return self.batch_predict(
            [mfd_res],
            [image],
            batch_size=batch_size,
            interline_enable=interline_enable,
        )[0]

    def batch_predict(
        self,
        images_mfd_res: list,
        images: list,
        batch_size: int = 64,
        interline_enable: bool = True,
    ) -> list:
        if not images_mfd_res:
            return []

        if len(images_mfd_res) != len(images):
            raise ValueError("images_mfd_res and images must have the same length.")

        images_formula_list = []
        mf_image_list = []
        backfill_list = []
        image_info = []

        for mfd_res, image in zip(images_mfd_res, images):
            formula_list, crop_targets = self._build_formula_items(
                mfd_res,
                image,
                interline_enable=interline_enable,
            )

            for formula_item, (xmin, ymin, xmax, ymax) in crop_targets:
                bbox_img = image[ymin:ymax, xmin:xmax]
                area = (xmax - xmin) * (ymax - ymin)

                curr_idx = len(mf_image_list)

View on GitHub (pinned to 4fe4bde114)

Solutions

  1. Keep both lists in lockstep: whenever a page is skipped/failed, drop its entry from both images and images_mfd_res.
  2. Rebuild pairs from a single source of truth: iterate pages once, run MFD, and append to both lists together.
  3. Add an assert len(images_mfd_res) == len(images) before calling batch_predict so mismatches surface at the call site.

Example fix

# before
mfd_res, images = [], original_images
for page in pages:
    r = run_mfd(page)
    mfd_res.append(r)          # appended even when page skipped from images
batch_predict(mfd_res, images)  # ValueError

# after
mfd_res, images = [], []
for page in pages:
    r = run_mfd(page)
    mfd_res.append(r)
    images.append(page)         # always paired
batch_predict(mfd_res, images)
Defensive patterns

Strategy: validation

Validate before calling

def run_batch(predictor, mfd_results: list, pages: list, **kw):
    if len(mfd_results) != len(pages):
        raise ValueError(
            f'mfd results ({len(mfd_results)}) and pages ({len(pages)}) out of sync; '
            'rebuild both lists from the same page loop'
        )
    return predictor.batch_predict(mfd_results, pages, **kw)

Try / catch

try:
    results = predictor.batch_predict(mfd_res, images)
except ValueError as e:
    if 'same length' in str(e):
        n = min(len(mfd_res), len(images))
        results = predictor.batch_predict(mfd_res[:n], images[:n])  # only if truncation is acceptable
    else:
        raise

Prevention

When it happens

Trigger: Calling batch_predict(mfd_results_for_3_pages, images_of_4_pages); typically caused by appending MFD outputs across runs while rebuilding the images list from scratch (or vice versa), or dropping a failed page from one list only.

Common situations: Accumulating formula-detection results in a retry loop where some pages fail and are excluded from one list; parallel workers returning mismatched result/image counts; page-level filtering (blank-page skip) applied to images but not to the detection results.

Related errors


AI-assisted analysis of opendatalab/MinerU@4fe4bde114 (2026-08-14). Data as JSON: /api/errors/7190116551b7c5a6. Report an issue: GitHub.