Skip to content

Fast, accurate OCR with correctness assessment.

fix(sdk): avoid generated batch output name collisions

adarshh347d6a64e613714
diff --git a/sdk/minicpmvocr/cli.py b/sdk/minicpmvocr/cli.py
index 4ff46bd..5e8a789 100644
--- a/sdk/minicpmvocr/cli.py
+++ b/sdk/minicpmvocr/cli.py
@@ -445,10 +445,24 @@ def _output_names(sources: list) -> dict:
'source_id -> file stem; a stem shared by two inputs gets its source_id in front.'
stems = [Path(source.get("path") or source.get("source_id") or "input").stem for source in sources]
shared = {stem for stem in stems if stems.count(stem) > 1}
+ preferred = [
+ f"{source.get('source_id', '')}_{stem}" if stem in shared else stem
+ for source, stem in zip(sources, stems)
+ ]
+ reserved = set(preferred)
names = {}
- for source, stem in zip(sources, stems):
- prefix = f"{source.get('source_id', '')}_" if stem in shared else ""
- names[source.get("source_id", "")] = f"{prefix}{stem}"
+ used = set()
+ for source, base in zip(sources, preferred):
+ name = base
+ if name in used:
+ suffix = 2
+ name = f"{base}_{suffix}"
+ # Do not take a later input's otherwise unambiguous filename.
+ while name in used or name in reserved:
+ suffix += 1
+ name = f"{base}_{suffix}"
+ names[source.get("source_id", "")] = name
+ used.add(name)
return names
diff --git a/sdk/tests/test_multi_input.py b/sdk/tests/test_multi_input.py
index 9cef5b6..b711bad 100644
--- a/sdk/tests/test_multi_input.py
+++ b/sdk/tests/test_multi_input.py
@@ -262,3 +262,46 @@ def test_split_images_sets_file_scope():
overrides = _build_parse_overrides(_args(inputs=[Path("/in/a.png")], split_images=True))
assert overrides["pipeline"] == {"image_document_scope": "file"}
+
+
+def test_output_names_avoid_prefix_and_suffix_collisions():
+ from minicpmvocr.cli import _output_names
+
+ sources = [
+ {"source_id": "s001", "path": "/x/a.png"},
+ {"source_id": "s002", "path": "/y/a.pdf"},
+ {"source_id": "s003", "path": "/z/s001_a.png"},
+ {"source_id": "s004", "path": "/z/s001_a_2.png"},
+ {"source_id": "s005", "path": "/z/s001_a_3.png"},
+ ]
+ names = _output_names(sources)
+ assert names == {
+ "s001": "s001_a", "s002": "s002_a", "s003": "s001_a_4",
+ "s004": "s001_a_2", "s005": "s001_a_3",
+ }
+ assert _output_names(sources) == names
+
+
+def test_parse_many_does_not_overwrite_colliding_results(tmp_path, capsys):
+ import json
+ from minicpmvocr.cli import _parse_many
+
+ run_dir = tmp_path / "run"
+ paths = ["/x/a.png", "/y/a.png", "/z/s001_a.png"]
+ texts = ["first document", "second document", "third document"]
+ _write_run(
+ run_dir,
+ [(path, "image", [f"image{i}"]) for i, path in enumerate(paths)],
+ {f"image{i}": (text, []) for i, text in enumerate(texts)},
+ scope="file",
+ )
+ out = tmp_path / "out"
+ args = SimpleNamespace(
+ stdout=False, output=str(out), stream=False, page_delimiter="\n",
+ no_merge_pages=False, json="", inputs=paths,
+ )
+ assert _parse_many(args, {"pipeline": {}, "prompt": {}}, run_dir, {}, {}) == 0
+ assert len(list(out.glob("*.md"))) == 3
+ assert {p.read_text() for p in out.glob("*.md")} == set(texts)
+ records = [json.loads(p.read_text()) for p in out.glob("*.json")]
+ assert {r["path"]: r["markdown"] for r in records} == dict(zip(paths, texts))