fix(rag): keep a failing caption group from aborting the ingest

_describe_group handled only asyncio.TimeoutError, so any other exception
out of the batcher propagated through asyncio.gather, aborted
_load_image_nodes, and took the whole document ingest with it. Every
sibling failure path in the same function skips and logs instead: missing
or non-multimodal embedding client, unavailable LLM, reader OSError, empty
captions. The batch group was the one seam that re-raised.

The current batcher folds its own errors into [None] * len(images), so
this is defensive: the loader should not have to depend on that internal
detail to keep captions best-effort, and the batch path is opt-in and the
least exercised.

The new test replaces the batcher with one that raises and asserts the
ingest still returns.

Signed-off-by: cgycorey <cgycorey@users.noreply.github.com>
This commit is contained in:
cgycorey
2026-10-23 21:57:54 +01:00
parent a31d82624c
commit f911036964
2 changed files with 51 additions and 0 deletions

View File

@@ -510,6 +510,16 @@ class LlamaIndexDocumentLoader:
"Image caption batch exceeded its %ss deadline", timeout_seconds
)
return []
except Exception as exc: # noqa: BLE001 - captions are best-effort
# Every other image failure here skips and logs; a raising
# group must not abort the whole document ingest.
self.logger.warning(
"Image caption group failed (%s: %s); skipping %s image(s)",
type(exc).__name__,
exc,
len(group),
)
return []
finally:
for _ in group:
completed += 1

View File

@@ -320,3 +320,44 @@ async def test_cached_batch_ingestion_keeps_source_alignment_and_reports_all_pro
assert [d.text for d in docs] == ["[Image] a.png\n\nfirst", "[Image] b.png\n\nsecond"]
assert progress == [(1, 2), (2, 2)]
assert len(calls) == 1
@pytest.mark.asyncio
async def test_a_raising_caption_group_does_not_abort_the_ingest(tmp_path, monkeypatch):
"""Captions are best-effort: every other image failure skips and logs.
The batch group was the one seam that re-raised, so an unexpected error
from the batcher (or from a future client wrapper) aborted the whole
ingest instead of dropping that group's captions.
"""
pytest.importorskip("llama_index.core")
from deeptutor.services.rag.pipelines.llamaindex import document_loader as loader_module
async def never_called(*args, **kwargs):
raise AssertionError("the batcher stub owns this call")
groups: list[int] = []
class _RaisingBatcher:
halted = False
def __init__(self, *args, **kwargs):
pass
async def describe(self, images):
groups.append(len(images))
raise RuntimeError("provider contract broke")
_install_multimodal_clients(monkeypatch, complete_fn=never_called)
monkeypatch.setattr(loader_module, "image_description_batch_size", lambda: 2)
monkeypatch.setattr(loader_module, "ImageCaptionBatcher", _RaisingBatcher)
paths = _make_images(tmp_path, ["a.png", "b.png"])
text = tmp_path / "notes.txt"
text.write_text("text that must survive", encoding="utf-8")
docs = await loader_module.LlamaIndexDocumentLoader().load(
[str(p) for p in paths] + [str(text)]
)
assert groups == [2], "the failing group must actually have run"
assert [d.metadata["file_name"] for d in docs] == ["notes.txt"]