From 8145f1d5274d12ca3bdedddc3d983e8b47eb194b Mon Sep 17 00:00:00 2001 From: Roland Tannous Date: Wed, 27 May 2026 17:02:36 +0400 Subject: [PATCH] Studio: splice VLM figure captions next to their 'Figure N:' line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Captions were appended at the bottom of the page text, so the chunk containing 'Figure 1: Asymmetries ...' got chunked separately from '**Figure**: Flowchart with ...' on the same page. Retrieval surfaced the caption-text chunk but the VLM description landed in a different chunk, leaving the LLM without the visual content right next to the figure label. Splice each VLM caption right after the matching 'Figure N:' (or 'Table N:') line as '**Figure N description**: ...', so: - The figure-boundary chunker now keeps both the original in-PDF caption AND the VLM description in the same chunk (which starts with 'Figure N:'). - Multi-figure pages get per-figure attribution — the prefix 'Figure N description' lets the LLM tell two figures on the same page apart, even though the bbox renderer still emits one image per page today (multi-figure clustering is a follow-up). - When the page text has no figure lines (DOCX/HTML/TXT or rare PDF layouts) the old end-of-page appendix is kept as a fallback. --- studio/backend/core/rag/parsers/__init__.py | 88 ++++++++++++++++++--- 1 file changed, 77 insertions(+), 11 deletions(-) diff --git a/studio/backend/core/rag/parsers/__init__.py b/studio/backend/core/rag/parsers/__init__.py index bc0b613b1e..99e565a123 100644 --- a/studio/backend/core/rag/parsers/__init__.py +++ b/studio/backend/core/rag/parsers/__init__.py @@ -3,9 +3,20 @@ from __future__ import annotations +import re from dataclasses import dataclass, field from pathlib import Path +# Same shape as the chunker's figure-boundary regex but captures the +# figure label (Figure / Fig. / Table / Tab.) AND the number so we can +# attribute a VLM caption back to a specific figure on a multi-figure +# page. +_FIGURE_LINE_RE = re.compile( + r"^(?P\**)(?P