[BUG]: Incomplete chunk validation in embed_chunks causes unhandled KeyError; eager SentenceTransformer instantiation blocks startup by ~11s

Aperta
#171 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@agarwalpranav0711 ci sta già lavorando.

Dal 13/10/2026.

  • #172 di @agarwalpranav0711 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python, pytorch

Direzione di ricerca

Start in backend/app/modules/vector_store/embed.py, where embed_chunks checks only for text before calling embedder.encode, and where the module-level SentenceTransformer is created. Validation of id, text and metadata should happen before any encode call, and the model should move into a lazy get_embedder() singleton. Pull request #172 already targets this, so check it before starting. Done means a malformed chunk raises a ValueError without running the model, and importing the module no longer loads the model.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Fix Chunk Validation and Lazy-Load SentenceTransformer

Bug Description

There are two reliability and startup performance issues in backend/app/modules/vector_store/embed.py.

1. Incomplete Chunk Validation

The embed_chunks(chunks) function validates the input chunk structure before generating embeddings. However, the current validation checks only whether the text field exists.

Later, the function directly accesses chunk["id"] and chunk["metadata"]. If either field is missing, the validation passes, and the function executes the expensive model inference step using embedder.encode(texts) before raising an unhandled KeyError.

Expected behavior: Validate all required fields (id, text, and metadata) before generating embeddings. If a chunk is malformed, raise a descriptive ValueError immediately, without performing model inference.

2. Eager SentenceTransformer Initialization

The model is initialized unconditionally at module scope:

embedder = SentenceTransformer("all-MiniLM-L6-v2")

As a result, importing embed.py synchronously loads PyTorch and the model weights, adding approximately 11 seconds to cold-start import time.

This delay affects application initialization, health checks, and unit tests that import the module without immediately needing embeddings.

Expected behavior: Initialize the model lazily on the first call using a get_embedder() singleton. Subsequent calls should reuse the same model instance, preserving existing functionality and avoiding unnecessary initialization during module imports.

Steps to Reproduce
  1. Open a terminal in the backend directory with the project virtual environment activated.
  2. Run the following script:
import time

t0 = time.perf_counter()
import app.modules.vector_store.embed as embed
t1 = time.perf_counter()

print(f"Cold-start import latency: {t1 - t0:.2f}s")

malformed_chunk = [{"text": "Sample article excerpt"}]

try:
    embed.embed_chunks(malformed_chunk)
except KeyError as err:
    print(f"Unhandled KeyError: {err}")
Actual Behavior
  • Importing the module takes approximately 11 seconds on a cold start.
  • A chunk containing text but missing id or metadata passes the existing validation.
  • The embedding model runs before the function raises an unhandled KeyError.
Expected Behavior
  • Importing the module does not initialize the SentenceTransformer model.
  • The model initializes only when embeddings are first requested and is reused for subsequent calls.
  • All required chunk fields (id, text, and metadata) are validated before model inference.
  • Invalid chunks raise a descriptive ValueError without triggering embedding generation.
Logs and Screenshots
Cold-start import latency: 11.39s
Unhandled KeyError after running model inference: 'id'
Environment Details
  • OS: Windows / Linux
  • Python: 3.13
  • File: backend/app/modules/vector_store/embed.py
  • Key packages: sentence-transformers, torch
Impact

Severity: Medium — Feature works under valid inputs but has reliability and startup performance issues.

Proposed Solution
  1. Validate that each chunk contains the required id, text, and metadata fields before generating embeddings.
  2. Raise a descriptive ValueError when required fields are missing or invalid.
  3. Replace eager model initialization with a lazy get_embedder() singleton.
  4. Preserve the existing embed_chunks(chunks) interface and embedding output behavior.
Code of Conduct
Lingua principale
TypeScript
Stelle
58
Fork
90
Merge medio
11h 51m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di AOSSIE-Org/Perspective

Tutte le issue di AOSSIE-Org/Perspective

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.