Skip to content

[BUG] caption_image function defined twice in vision.py — second definition completely overwrites the first #734

Description

@vipul674

Description of the Bug

In backend/app/rag/vision.py, the function caption_image is defined twice:

  1. First definition (approx line 178): def caption_image(image_bytes: bytes, page: Optional[int] = None) -> str
  2. Second definition (approx line 185): def caption_image(image_bytes: bytes | List[bytes], page: int | List[int] | None = None) -> str | List[str]

In Python, when a function is defined twice in the same module, the second definition completely replaces the first. The second definition has a different signature (accepting single or batch inputs) with its own inline OpenAI implementation.

This means:

  • The first simpler signature is entirely dead code
  • The second version duplicates logic that should have been in the first version
  • The _openai_caption helper function (defined earlier at line ~149) is defined but never called — the second caption_image has its own inline OpenAI code instead
  • The function has two code paths doing essentially the same thing in different ways

This looks like an unresolved merge conflict or a refactoring mistake.

Steps to Reproduce

  1. Read backend/app/rag/vision.py
  2. Observe that caption_image appears twice (lines ~178 and ~185)
  3. In Python, the second definition is the only one that exists at runtime
  4. Note that _openai_caption (line ~149) is never called anywhere

Expected Behavior

There should be a single consolidated caption_image function. The batch logic and single-image logic should be merged. The _openai_caption helper should either be used or removed.

Affected File

backend/app/rag/vision.py (lines ~178-184 and lines ~185+)

Suggested Fix

Consolidate into a single function. Either:

  • Keep the batch-aware signature and remove the first definition entirely, or
  • Keep the first simple signature and have _openai_caption handle both single and batch cases

Also remove _openai_caption if unused, or refactor the duplicate inline code to use it.

GSSoC '26

  • Yes, I am participating in GirlScript Summer of Code and would like to fix this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    gssocGirlScript Summer of Code 2026 issue/PR

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions