Skip to content

Fix/caption failure logging visibility#608

Open
Nancy-3012 wants to merge 6 commits into
param20h:devfrom
Nancy-3012:fix/caption-failure-logging-visibility
Open

Fix/caption failure logging visibility#608
Nancy-3012 wants to merge 6 commits into
param20h:devfrom
Nancy-3012:fix/caption-failure-logging-visibility

Conversation

@Nancy-3012

Copy link
Copy Markdown

📝 What does this PR do?

Improves visibility of image captioning failures in generate_captions_for_chunks
and _openai_caption. Previously, exceptions were silently swallowed with
logger.debug, meaning admins had no way to know that image data was being
permanently discarded. This PR upgrades both to logger.warning with actionable
messages and full tracebacks via exc_info=True.

Fixes #590


🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature

🧪 How was this tested?

  • Ran the frontend locally (npm run dev inside frontend/)

📸 Screenshots (if UI change)

N/A


⚠️ Anything to flag for reviewers?

No flags.


✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@Nancy-3012 Nancy-3012 requested a review from param20h as a code owner June 15, 2026 04:24
param20h
param20h previously approved these changes Jun 15, 2026
@param20h

Copy link
Copy Markdown
Owner

I'll retrieve the logs for job 81396718755 to identify the failure and provide a solution.The primary failure is a Python syntax error in the code. Let me retrieve the problematic file to see the exact issue:## Solution for Job 81396718755

The job is failing due to an indentation error in backend/app/rag/vision.py at line 185.

Root Cause

Line 185 has incorrect indentation. The except statement uses 3 spaces instead of 4, causing Python to raise an IndentationError: unindent does not match any outer indentation level.

The Problem

183|         return response.choices[0].message.content.strip()
184| 
185|    except Exception as exc:  # ❌ Only 3 spaces (should be 4)
186|         logger.warning(

The Fix

Change line 185 to use 4 spaces for indentation:

183|         return response.choices[0].message.content.strip()
184| 
185|     except Exception as exc:  # ✅ 4 spaces
186|         logger.warning(
187|             "OpenAI vision caption failed — falling back to OCR/placeholder. "
188|             "This may be a transient API error (rate-limit, timeout). Error: %s",
189|             exc,
190|             exc_info=True,
191|         )
192|         return ""

Additionally, there's duplicate code in the file (lines 234-254). The caption_image() function has redundant logic that should be removed. Here's the corrected version:

def caption_image(
    image_bytes: "bytes | List[bytes]",
    page: "int | List[int] | None" = None,
) -> "str | List[str]":
    """Generate a caption for a single image or a batch of images.

    Resolution order:
      1. Configured VLM provider (set VISION_PROVIDER in .env)
      2. Local OCR via pytesseract
      3. Placeholder string with page number and dimensions
    """
    if isinstance(image_bytes, list):
        pages = (
            page if isinstance(page, list)
            else ([page] * len(image_bytes) if page is not None else [None] * len(image_bytes))
        )
        return [caption_image(img, pg) for img, pg in zip(image_bytes, pages)]

    # Strategy: try the configured VLM provider
    provider = get_vision_provider(getattr(settings, "VISION_PROVIDER", None))
    if provider is not None:
        result = provider.caption(image_bytes)
        if result:
            return result

    # Fallback 1: local OCR
    ocr = _ocr_caption(image_bytes)
    if ocr:
        return ocr

    # Fallback 2: placeholder
    try:
        pix = fitz.Pixmap(image_bytes)
        dims = f"{pix.width}x{pix.height} px"
    except Exception:
        dims = "unknown size"

    return f"Figure on page {page} ({dims})." if page else f"Figure ({dims})."

Remove lines 235-254 as they duplicate the logic already present above.

View the file

@Nancy-3012

Copy link
Copy Markdown
Author

@param20h please merge this branch

param20h
param20h previously approved these changes Jun 15, 2026
@Nancy-3012

Copy link
Copy Markdown
Author

@param20h please merge this branch

@Nancy-3012

Copy link
Copy Markdown
Author

@param20h please merge this branch

@param20h

Copy link
Copy Markdown
Owner

@param20h please merge this branch

running the checks

param20h
param20h previously approved these changes Jun 22, 2026
@param20h

Copy link
Copy Markdown
Owner

Backend issues still

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.

[BUG] : improve visibility and handling of deleted image_bytes on caption failure

2 participants